GCS for Team
Kern supports using Google Cloud Storage (GCS) as a storage backend for Teams using the GcsJsonDb class. This storage backend stores session data as JSON blobs in a GCS bucket.
Usage
Configure your team with GCS storage to enable cloud-based session persistence.
1"""2Run: `uv pip install openai newspaper4k lxml_html_clean kern-ai` to install the dependencies3"""45import uuid6import google.auth7from typing import List89from kern.agent import Agent10from kern.db.gcs_json import GcsJsonDb11from kern.models.openai import OpenAIResponses12from kern.team import Team13from kern.tools.hackernews import HackerNewsTools14from kern.tools.hackernews import HackerNewsTools15from pydantic import BaseModel1617# Obtain the default credentials and project id from your gcloud CLI session.18credentials, project_id = google.auth.default()1920# Generate a unique bucket name using a base name and a UUID4 suffix.21base_bucket_name = "example-gcs-bucket"22unique_bucket_name = f"{base_bucket_name}-{uuid.uuid4().hex[:12]}"23print(f"Using bucket: {unique_bucket_name}")2425# Setup the JSON database26db = GcsJsonDb(27 bucket_name=unique_bucket_name,28 prefix="team/",29 project=project_id,30 credentials=credentials,31)3233class Article(BaseModel):34 title: str35 summary: str36 reference_links: List[str]3738hn_researcher = Agent(39 name="HackerNews Researcher",40 model=OpenAIResponses(id="gpt-5.2"),41 role="Gets top stories from hackernews.",42 tools=[HackerNewsTools()],43)4445web_searcher = Agent(46 name="Web Searcher",47 model=OpenAIResponses(id="gpt-5.2"),48 role="Searches the web for information on a topic",49 tools=[HackerNewsTools()],50 add_datetime_to_context=True,51)5253hn_team = Team(54 name="HackerNews Team",55 model=OpenAIResponses(id="gpt-5.2"),56 members=[hn_researcher, web_searcher],57 db=db,58 instructions=[59 "First, search hackernews for what the user is asking about.",60 "Then, ask the web searcher to search for each story to get more information.",61 "Finally, provide a thoughtful and engaging summary.",62 ],63 output_schema=Article,64 markdown=True,65 show_members_responses=True,66)6768hn_team.print_response("Write an article about the top 2 stories on hackernews")Prerequisites
Google Cloud SDK Setup
- Install the Google Cloud SDK
- Run
gcloud initto configure your account and project
GCS Permissions
Ensure your account has sufficient permissions (e.g., Storage Admin) to create and manage GCS buckets:
1gcloud projects add-iam-policy-binding YOUR_PROJECT_ID \2 --member="user:YOUR_EMAIL@example.com" \3 --role="roles/storage.admin"Authentication
Use default credentials from your gcloud CLI session:
1gcloud auth application-default loginAlternatively, if using a service account, set the GOOGLE_APPLICATION_CREDENTIALS environment variable to the path of your service account JSON file.
Python Dependencies
Install the required Python packages:
1pip install google-auth google-cloud-storage openai ddgsSetup with Docker
For local testing without using real GCS, you can use fake-gcs-server.
Create a docker-compose.yml file:
1version: '3.8'2services:3 fake-gcs-server:4 image: fsouza/fake-gcs-server:latest5 ports:6 - "4443:4443"7 command: ["-scheme", "http", "-port", "4443", "-public-host", "localhost"]8 volumes:9 - ./fake-gcs-data:/dataStart the fake GCS server:
1docker-compose up -dUsing Fake GCS with Docker
Set the environment variable to direct API calls to the emulator:
1export STORAGE_EMULATOR_HOST="http://localhost:4443"2python gcs_for_agent.pyWhen using Fake GCS, authentication isn't enforced and the client will automatically detect the emulator endpoint.
Params
| Parameter | Type | Default | Description |
|---|---|---|---|
id | Optional[str] | - | The ID of the database instance. UUID by default. |
bucket_name | str | - | Name of the GCS bucket where JSON files will be stored. |
prefix | Optional[str] | - | Path prefix for organizing files in the bucket. Defaults to "kern/". |
session_table | Optional[str] | - | Name of the JSON file to store sessions (without .json extension). |
memory_table | Optional[str] | - | Name of the JSON file to store user memories. |
metrics_table | Optional[str] | - | Name of the JSON file to store metrics. |
eval_table | Optional[str] | - | Name of the JSON file to store evaluation runs. |
knowledge_table | Optional[str] | - | Name of the JSON file to store knowledge content. |
traces_table | Optional[str] | - | Name of the JSON file to store traces. |
spans_table | Optional[str] | - | Name of the JSON file to store spans. |
project | Optional[str] | - | GCP project ID. If None, uses default project. |
credentials | Optional[Any] | - | GCP credentials. If None, uses default credentials. |