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 dependencies
3"""
4
5import uuid
6import google.auth
7from typing import List
8
9from kern.agent import Agent
10from kern.db.gcs_json import GcsJsonDb
11from kern.models.openai import OpenAIResponses
12from kern.team import Team
13from kern.tools.hackernews import HackerNewsTools
14from kern.tools.hackernews import HackerNewsTools
15from pydantic import BaseModel
16
17# Obtain the default credentials and project id from your gcloud CLI session.
18credentials, project_id = google.auth.default()
19
20# 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}")
24
25# Setup the JSON database
26db = GcsJsonDb(
27 bucket_name=unique_bucket_name,
28 prefix="team/",
29 project=project_id,
30 credentials=credentials,
31)
32
33class Article(BaseModel):
34 title: str
35 summary: str
36 reference_links: List[str]
37
38hn_researcher = Agent(
39 name="HackerNews Researcher",
40 model=OpenAIResponses(id="gpt-5.2"),
41 role="Gets top stories from hackernews.",
42 tools=[HackerNewsTools()],
43)
44
45web_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)
52
53hn_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)
67
68hn_team.print_response("Write an article about the top 2 stories on hackernews")

Prerequisites

Google Cloud SDK Setup

  1. Install the Google Cloud SDK
  2. Run gcloud init to 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 login

Alternatively, 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 ddgs

Setup 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:latest
5 ports:
6 - "4443:4443"
7 command: ["-scheme", "http", "-port", "4443", "-public-host", "localhost"]
8 volumes:
9 - ./fake-gcs-data:/data

Start the fake GCS server:

1docker-compose up -d

Using 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.py

When using Fake GCS, authentication isn't enforced and the client will automatically detect the emulator endpoint.

Params

ParameterTypeDefaultDescription
idOptional[str]-The ID of the database instance. UUID by default.
bucket_namestr-Name of the GCS bucket where JSON files will be stored.
prefixOptional[str]-Path prefix for organizing files in the bucket. Defaults to "kern/".
session_tableOptional[str]-Name of the JSON file to store sessions (without .json extension).
memory_tableOptional[str]-Name of the JSON file to store user memories.
metrics_tableOptional[str]-Name of the JSON file to store metrics.
eval_tableOptional[str]-Name of the JSON file to store evaluation runs.
knowledge_tableOptional[str]-Name of the JSON file to store knowledge content.
traces_tableOptional[str]-Name of the JSON file to store traces.
spans_tableOptional[str]-Name of the JSON file to store spans.
projectOptional[str]-GCP project ID. If None, uses default project.
credentialsOptional[Any]-GCP credentials. If None, uses default credentials.