aiml

Lesson 1 of 20Module 1

Serve an agent over HTTP and drive it from the CLI

Serve a Google ADK 2.0 agent with adk api_server, call /run and /run_sse from a Python client, and script adk run with replay files and JSONL output.

Prerequisites

Python, HTTP and JSON, and the docs assistant from the Building Agents with Google ADK 2.0 course.

By the end, you can

  • Serve an agent folder with adk api_server and choose its session store and allowed origins.
  • Create sessions and run turns over HTTP with /run and /run_sse, and parse the events each returns.
  • Script the same agent with adk run --replay and --jsonl, and handle 404, 403 and nested-state mistakes.
Runtime
Python 3.12+, a Gemini API key (or Ollama with gemma4:e2b)
Dependencies
google-adk, httpx and FastAPI; litellm for an existing Ollama model

Lab code, tests and fixtures (ZIP, 21 KB)Quiz check your understanding

Progress is saved in this browser.

A docs portal needs a request/response interface for an agent. An HTTP endpoint accepts a request at a URL; a session connects successive turns. ADK’s adk api_server exposes session operations plus /run for a collected result and /run_sse for streamed events.

Follow the question “How do I undo the last production deploy?”: the Python client creates a session, sends the question and reads the agent events. Then compare the collected response with the streamed version. Last, a CLI replay file sends fixed questions to the same agent without a browser. The outputs on this page are recorded runs: they show what each call returns, not how reliably the model answers.

Docuctl is a demo deployment CLI whose manual ships with this lab; the services here use local sample data.

When you need this. Say your team’s internal docs portal gets a “Ask the docs” box. The portal’s backend sends each question to the docs assistant and shows the answer and the page it read. That backend needs a stable HTTP interface with sessions, streaming and clear errors, and your CI needs a way to run fixed questions against the agent without a browser. This lesson gives you both.

client.py · adk api_server · gemini-3.8-flash · recorded in recorded-runs.json
data flowreturnartifact hand-offfailureclient.pyhttpxrun(), run_sse()adk api_server · one processPOST /runPOST /run_ssemessageevent listdata: linesRunnerone per app,session per requestnew_messagestreaminggemini-3.8-flashrequestreplyread_pageargssessions.db--session_service_uri sqlite://append eventsunknown sessionHTTP 404origin not allowedHTTP 403Recorded /run_sse stream · 9 events streamed · 4 stored, the user's message included1.76 spartial · call read_pagestreamed only1.83 spartial · empty chunkstreamed only1.84 sfinal · call read_pagestored in the session1.84 sfinal · result read_pagestored in the session4.71 spartial · text chunkstreamed only4.91 spartial · text chunkstreamed only5.06 spartial · text chunkstreamed only5.07 spartial · empty chunkstreamed only5.08 sfinal · answerstored in the sessiondata flowreturnartifact hand-offfailureclient.pyhttpxoriginHTTP 403servermessageevents/run, /run_sselist or SSE streamRunnernew_messageno sessionHTTP 404sessions.dbSQLiteappend eventsGeminiread_pagerequestargsRecorded /run_sse stream9 streamed, 4 stored1.76partial · call read_page1.83partial · empty chunk1.84final · call read_page1.84final · result read_page4.71partial · text chunk4.91partial · text chunk5.06partial · text chunk5.07partial · empty chunk5.08final · answer
One question to the docs assistant through the API server. /run waits for the whole turn and returns the events as one list; /run_sse sends each event as it is yielded, partial text chunks included. The session database keeps the user's message and the complete events, never the dashed chunks. Times are seconds from the request on one machine. A browser request from an origin missing from --allow_origins is refused before it reaches a route.
Recorded run: the server and one /run turnRecorded output (excerpt) · textFull output
$ adk api_server --port 63492 --session_service_uri sqlite:///sessions.db --allow_origins http://localhost:3000 . --host 127.0.0.1
$ python client.py 'How do I undo the last production deploy?'
apps: ['docs_assistant']
docs_assistant  final    call read_page({"page": "rollback"})
docs_assistant  final    result read_page
docs_assistant  final    'To undo the last production deploy, run:'
answer: To undo the last production deploy, run:

`docuctl rollback --env production`

Without the `--to` flag, `docuctl rollback` automatically rolls back to the previous release. Note that database migrations are not rolled back; if the release ran a migration, you must write a new migration to reverse it.

These runtime and deployment lessons share the docs-assistant scenario, but they do not build one package step by step. Each download holds the configuration and recorded output for its own experiment. If you combine session, trigger, tracing and hosting changes from several lessons, test that combination separately.

1. Serve the agent folder

Download the complete lab ZIP, not just client.py. Extract it and run every command from adk-api-server/: the sqlite:///sessions.db URI and the . agents folder in the commands below are relative to that folder. The folder contains an importable docs_assistant package with one root_agent and a read_page tool.

Prepare the projectTerminal · bash
unzip serve-an-agent-over-http-and-drive-it-from-the-cli.zip
cd adk-api-server
python3.12 -m venv .venv
source .venv/bin/activate
python -m pip install -r requirements-lock.txt
cp docs_assistant/.env.example docs_assistant/.env

Edit docs_assistant/.env and set GOOGLE_API_KEY to your Gemini API key. This selects the Gemini API, not a cloud project. Keep the file private. If your shell already exports another backend or model, unset those variables first: ADK does not replace existing environment variables with .env values. To use a local model you already installed, export DOCS_MODEL=ollama_chat/gemma4:e2b in the server terminal; the agent uses LiteLlm for that value. These commands do not download a model.

docs_assistant/agent.pySource · pythonDownload
"""The docs assistant with one tool, so its event stream has calls, results and an answer."""
import os
from pathlib import Path

from google.adk import Agent
from google.adk.models.lite_llm import LiteLlm
from google.genai import types

# Gemini through ADK's native client; set DOCS_MODEL=ollama_chat/gemma4:e2b to run Gemma locally.
MODEL = os.environ.get("DOCS_MODEL", "gemini-3.8-flash")
DOCS = Path(__file__).resolve().parent.parent / "docs"
PAGES = sorted(p.stem for p in DOCS.glob("*.md"))


def read_page(page: str) -> dict:
    """Read one page of the docuctl documentation.

    Args:
        page: The page name: config, deploy, rollback or tokens.
    """
    if page not in PAGES:
        return {"error": f"No page {page!r}. Pages: {', '.join(PAGES)}."}
    return {"page": page, "text": (DOCS / f"{page}.md").read_text(encoding="utf-8")}


root_agent = Agent(
    name="docs_assistant",
    model=LiteLlm(model=MODEL) if MODEL.startswith("ollama_chat/") else MODEL,
    description="Answers developer questions about docuctl from its documentation.",
    instruction="""You answer questions from developers about docuctl, the team's deploy CLI.
Read the relevant page with read_page before you answer, and answer only from it.
Copy commands exactly, in backticks. Keep the answer under 100 words.""",
    tools=[read_page],
    generate_content_config=types.GenerateContentConfig(
        temperature=0.2,
        max_output_tokens=1024,
        thinking_config=types.ThinkingConfig(thinking_level="low"),
    ),
)

Keep this first terminal open. . means the project that contains the docs_assistant/ agent folder:

Terminal 1: start the ADK API serverTerminal · bash
adk api_server --host 127.0.0.1 --port 8765 \
  --session_service_uri sqlite:///sessions.db \
  --allow_origins http://localhost:3000 .

Starting the server does not ask the model a question. The HTTP and terminal activities below do. Stop the server with Ctrl-C when you finish.

Three flags decide how the server behaves in front of real traffic. --session_service_uri sqlite:///sessions.db keeps sessions in a SQLite file, so a restart does not lose conversations; without it the server uses local .adk storage or memory. --allow_origins lists the browser origins allowed to call it, which matters as soon as a web page calls the server directly. --port picks the port. adk web starts the same server plus the Dev UI. The help text for both commands says the endpoints are unauthenticated, so put them behind your own gateway or identity proxy before anything outside your machine can reach them.

A browser origin not allowed. A request with Origin: https://docs-portal.example gets HTTP 403 Forbidden: origin not allowed before it reaches a route. Add the portal’s origin to --allow_origins. The preflight for an allowed origin returned access-control-allow-origin: http://localhost:3000.

2. Create a session and run a turn with /run

Open a second terminal, replace /path/to/unpacked with your extraction directory, and activate the same environment. client.py asks one question, creates a session and prints the actual events; --stream uses the streaming endpoint. Each command calls the model you configured.

Terminal 2: ask one questionTerminal · bash
cd /path/to/unpacked/adk-api-server
source .venv/bin/activate
python client.py "How do I undo the last production deploy?"
python client.py --stream "Which page explains tokens?"

Look for a read_page call, its result and the final answer. The “Recorded run: the server and one /run turn” panel above shows a saved trace; your model response and timings can differ.

client.pySource · pythonDownload
"""Drive the docs assistant through the ADK API server over HTTP.

Start the server from this folder first:
    adk api_server --port 8765 --session_service_uri sqlite:///sessions.db .
Then: python client.py "How do I undo the last production deploy?"
"""
import argparse
import json
import os
import uuid

import httpx

BASE_URL = os.environ.get("ADK_URL", "http://127.0.0.1:8765")
APP, USER = "docs_assistant", "dev-1"


def create_session(client: httpx.Client, session_id: str, state: dict | None = None) -> dict:
    """POST /apps/{app}/users/{user}/sessions with the session ID in the body."""
    r = client.post(f"/apps/{APP}/users/{USER}/sessions", json={"session_id": session_id, "state": state or {}})
    r.raise_for_status()
    return r.json()


def message(text: str) -> dict:
    return {"role": "user", "parts": [{"text": text}]}


def run(client: httpx.Client, session_id: str, text: str) -> list[dict]:
    """POST /run: the server runs the whole turn and returns every event at once."""
    body = {"app_name": APP, "user_id": USER, "session_id": session_id, "new_message": message(text)}
    r = client.post("/run", json=body)
    r.raise_for_status()
    return r.json()


def run_sse(client: httpx.Client, session_id: str, text: str, streaming: bool = True):
    """POST /run_sse: one server-sent event per ADK event, as the runner yields it."""
    body = {"app_name": APP, "user_id": USER, "session_id": session_id,
            "new_message": message(text), "streaming": streaming}
    with client.stream("POST", "/run_sse", json=body) as r:
        r.raise_for_status()
        for line in r.iter_lines():
            if line.startswith("data: "):
                yield json.loads(line[len("data: "):])


def describe(event: dict) -> str:
    """One line per event: author, partial or final, and what the parts carry."""
    parts = (event.get("content") or {}).get("parts") or []
    what = []
    for p in parts:
        if "functionCall" in p:
            what.append(f"call {p['functionCall']['name']}({json.dumps(p['functionCall']['args'])})")
        elif "functionResponse" in p:
            what.append(f"result {p['functionResponse']['name']}")
        elif p.get("text") and not p.get("thought"):
            what.append(repr(p["text"][:40]))
    if not what:
        what.append("(no text)")
    kind = "partial" if event.get("partial") else "final"
    return f"{event['author']:<15} {kind:<8} {' + '.join(what)}"


def answer(events: list[dict]) -> str:
    """The text of the agent's last complete event."""
    for event in reversed(events):
        if event["author"] != "user" and not event.get("partial"):
            texts = [p["text"] for p in (event.get("content") or {}).get("parts", []) if p.get("text") and not p.get("thought")]
            if texts:
                return "".join(texts)
    return ""


def main() -> None:
    parser = argparse.ArgumentParser()
    parser.add_argument("question")
    parser.add_argument("--stream", action="store_true", help="use /run_sse with streaming")
    args = parser.parse_args()
    with httpx.Client(base_url=BASE_URL, timeout=120) as client:
        print("apps:", client.get("/list-apps").json())
        sid = f"s-{uuid.uuid4().hex[:8]}"
        create_session(client, sid, {"team": "payments"})
        events = list(run_sse(client, sid, args.question)) if args.stream else run(client, sid, args.question)
        for event in events:
            print(describe(event))
        print("answer:", answer(events))


if __name__ == "__main__":
    main()

create_session posts to /apps/{app}/users/{user}/sessions with the session ID and initial state in the body. The server returns the stored session; in the recorded run its state is {"team":"payments"}. run posts a RunAgentRequest with the app name, user ID, session ID and the new message, a types.Content in JSON. /run blocks until the turn ends, then returns every event in one list. These are the same Event objects a Runner yields in Python, serialized in camelCase. The output above shows 3 events: the model’s read_page call, the tool result and the answer.

An unknown session ID. /run returns HTTP 404 with Session not found: no-such-session; nothing runs. Create the session first, or start the server with --auto_create_session when clients pick their own IDs.

State nested under the deprecated path. Posting {"state": {...}} to the older /sessions/{session_id} path stores {"state":{"team":"payments"}}: that path reads the whole body as the state. ADK 2.12 marks it deprecated. Post session_id and state to /apps/{app}/users/{user}/sessions, as create_session does.

Recorded failuresRecorded output · textDownload
$ curl -X POST /run with session_id no-such-session
HTTP 404 {"detail": "Session not found: no-such-session"}
$ curl -X POST /run -H 'Origin: https://docs-portal.example'
HTTP 403 Forbidden: origin not allowed
$ curl -X POST /apps/docs_assistant/users/dev-1/sessions/s-old -d '{"state": {"team": "payments"}}'
HTTP 200 state: {"state": {"team": "payments"}}

The origin check stops a browser page on another site from using the server; it does not authenticate anyone. A client that sends no Origin header, such as client.py, passes it.

3. Stream the turn with /run_sse

A UI should show text as it arrives. /run_sse takes the same body plus "streaming": true and answers with server-sent events: one data: {...} line per event. run_sse reads them with client.stream and iter_lines.

Recorded /run_sse turnRecorded output (excerpt) · textFull output
$ python client.py --stream 'How do I undo the last production deploy?'
docs_assistant  partial  call read_page({"page": "rollback"})
docs_assistant  partial  (no text)
docs_assistant  final    call read_page({"page": "rollback"})
docs_assistant  final    result read_page
docs_assistant  partial  'To roll back to the previous production '
docs_assistant  partial  'to` flag, `docuctl rollback` automatical'
docs_assistant  partial  ' if the release ran a migration, you mus'
docs_assistant  partial  (no text)
docs_assistant  final    'To roll back to the previous production '
yielded 9 events; the session stored 4

The partial events are chunks of model output, and some carry no visible text. The session keeps only complete events. A client renders partials as they arrive, then replaces them with the final event. This is the same rule you used with Runner.run_async.

4. Drive the agent from the CLI

Two adk run flags make the CLI useful in scripts. --replay FILE takes initial state and a list of queries, creates a fresh session and runs each query in order. It suits a smoke test after a deploy, because the questions live in version control:

replay.jsonSource · jsonDownload
{
  "state": {"team": "payments"},
  "queries": [
    "How do I roll back production to release r41?",
    "Does that also undo the database migration?"
  ]
}
Recorded replay runRecorded output · textDownload
$ adk run --in_memory --replay replay.json docs_assistant
[user]: How do I roll back production to release r41?
[docs_assistant]: To roll back production to release r41, run:
`docuctl rollback --env production --to r41`
[user]: Does that also undo the database migration?
[docs_assistant]: No. Database migrations are not rolled back. If the release you leave ran a migration, write a new migration that reverses it.

--jsonl prints one JSON event per line instead of the [author]: text view, so a script can check which tools ran. This is the first record of the recorded run, the function call event, with its signature and text trimmed:

recorded-runs.json: one adk run --jsonl recordRecorded output · jsonDownload
{
  "author": "docs_assistant",
  "session_id": "2d3d3a6e-a7af-4303-9ae7-6b380f137717",
  "node_path": "docs_assistant@1",
  "id": "d776d691-2d74-47d4-be98-44cdd11c332f",
  "modelVersion": "gemini-3.8-flash",
  "content": {
    "parts": [
      {
        "functionCall": {
          "id": "call_2449579",
          "args": {
            "page": "tokens"
          },
          "name": "read_page"
        }
      }
    ]
  },
  "finishReason": "STOP",
  "usageMetadata": {
    "candidatesTokenCount": 16,
    "promptTokenCount": 175,
    "promptTokensDetails": [
      {
        "modality": "TEXT",
        "tokenCount": 175
      }
    ],
    "thoughtsTokenCount": 78,
    "totalTokenCount": 269
  },
  "invocationId": "e-32c34dc4-251d-459e-a515-bd41bd060861",
  "nodeInfo": {
    "path": "docs_assistant@1"
  },
  "longRunningToolIds": [],
  "timestamp": 1792979049.9273632
}

--in_memory keeps these runs out of the local session store. --save_session with --session_id saves a session to JSON, and --resume FILE reopens it later. Both are for a developer at the terminal, not for scripts.

Stop the API server before reusing its port. From the first terminal you can inspect the same package with ADK Web instead:

Inspect the agent in ADK WebTerminal · bash
adk web --host 127.0.0.1 --port 8765 \
  --session_service_uri sqlite:///sessions.db .

Open http://127.0.0.1:8765, select docs_assistant, start a session and send one question. Inspect its events and state after the response. ADK Web is for development only.

How the server loads agents and streams events

adk api_server calls get_fast_api_app(agents_dir=..., web=False, ...) and serves the FastAPI app with uvicorn. Each folder in agents_dir with a root_agent is an app; GET /list-apps returned ["docs_assistant"]. On the first request for an app, the server loads the agent and builds one Runner for it with the configured session service. Every request then names its session, so many clients share one runner while each conversation keeps its own history and state in sessions.db.

/run and /run_sse differ only in delivery. /run collects the events and returns them when the turn ends. /run_sse checks the session first, so a missing session still gets a clean 404 instead of a broken stream, then writes each event as the runner yields it. In the recorded stream, on one machine, the first event arrived at 1.76 s and the last at 5.08 s. Most of that wait is the two model calls. Streaming changes when the client sees text, not how long the turn takes. Use /run when a script needs the whole turn at once, and /run_sse when a person reads the answer as it arrives.

Test the server without a model

test_server.py builds the same app with get_fast_api_app and calls it through FastAPI’s TestClient. The model is a BaseLlm subclass registered with LLMRegistry for names matching replay-.*. Setting DOCS_MODEL=replay-model makes agent.py resolve to it. The server, runner, session service and tool all run for real; only the model replies come from the recording.

Terminal commandTerminal · bash
python -m unittest -v test_server.py test_recording_opt_in.py
Recorded test outputRecorded output · textDownload
test_public_record_omits_opaque_signatures_without_changing_semantic_fields (test_recording_opt_in.RecordingOptIn.test_public_record_omits_opaque_signatures_without_changing_semantic_fields) ... ok
test_recorder_refuses_without_explicit_permission (test_recording_opt_in.RecordingOptIn.test_recorder_refuses_without_explicit_permission) ... ok
test_a_browser_origin_not_in_allow_origins_gets_403 (test_server.ApiServer.test_a_browser_origin_not_in_allow_origins_gets_403) ... ok
test_an_unknown_session_returns_404 (test_server.ApiServer.test_an_unknown_session_returns_404) ... ok
test_answer_skips_contentless_end_or_error_events (test_server.ApiServer.test_answer_skips_contentless_end_or_error_events) ... ok
test_auto_create_session_accepts_a_new_id (test_server.ApiServer.test_auto_create_session_accepts_a_new_id) ... ok
test_run_returns_the_whole_turn_as_one_list (test_server.ApiServer.test_run_returns_the_whole_turn_as_one_list) ... ok
test_run_sse_streams_partials_the_session_does_not_store (test_server.ApiServer.test_run_sse_streams_partials_the_session_does_not_store) ... ok
test_the_deprecated_per_id_path_nests_the_state (test_server.ApiServer.test_the_deprecated_per_id_path_nests_the_state) ... ok

----------------------------------------------------------------------
Ran 9 tests in 0.383s

OK

The saved output reports 9 tests across both files. test_recording_opt_in.py checks that the recording script refuses to run without --record.

Read the tests and optional recording script

python record.py --record makes new model calls and overwrites the saved run files. You do not need it for setup or for the one-question activity. Copy the original records first if you want to compare runs.

test_server.pySource · pythonDownload
"""Replay the recorded model output through the real ADK API server app, offline.

Run: python -m unittest -v test_server.py
get_fast_api_app builds the same FastAPI app that `adk api_server` serves; only the model is replaced.
"""
import json
import logging
import os
import unittest
import warnings
from pathlib import Path
from unittest import mock

warnings.filterwarnings("ignore")
logging.disable(logging.CRITICAL)
os.environ["ADK_DISABLE_LOAD_DOTENV"] = "1"  # keep ancestor .env files out of offline tests
os.environ["DOCS_MODEL"] = "replay-model"  # agent.py passes this string to ADK's model registry

from fastapi.testclient import TestClient  # noqa: E402
from google.adk.cli.fast_api import get_fast_api_app  # noqa: E402
from google.adk.models.base_llm import BaseLlm  # noqa: E402
from google.adk.models.llm_response import LlmResponse  # noqa: E402
from google.adk.models.registry import LLMRegistry  # noqa: E402
from google.genai import types  # noqa: E402

import client as c  # noqa: E402

HERE = Path(__file__).resolve().parent
RECORD = json.loads((HERE / "recorded-runs.json").read_text(encoding="utf-8"))


class ReplayLlm(BaseLlm):
    """Yields the recorded responses of one model call per request, partial chunks included."""
    queue: list = []

    @classmethod
    def supported_models(cls):
        return [r"replay-.*"]

    async def generate_content_async(self, llm_request, stream=False):
        while ReplayLlm.queue:
            turn = ReplayLlm.queue.pop(0)
            yield LlmResponse(content=types.Content.model_validate(turn["content"]), partial=turn["partial"] or None)
            if not turn["partial"]:
                return


LLMRegistry.register(ReplayLlm)
# unittest discovery may import agent packages before this test module sets DOCS_MODEL.
from docs_assistant import agent as loaded_agent
loaded_agent.root_agent.model = ReplayLlm(model="replay-model")


def server(**kwargs) -> TestClient:
    app = get_fast_api_app(agents_dir=str(HERE), web=False, session_service_uri="memory://",
                           artifact_service_uri="memory://", allow_origins=["http://localhost:3000"], **kwargs)
    return TestClient(app)


class ApiServer(unittest.TestCase):
    def setUp(self):
        warnings.simplefilter("ignore")  # unittest re-enables warnings per test
        self.http = server()

    def test_answer_skips_contentless_end_or_error_events(self):
        events = [{"author": "docs_assistant", "content": {"parts": [{"text": "Answer"}]}},
                  {"author": "docs_assistant", "content": None}]
        self.assertEqual(c.answer(events), "Answer")

    def test_run_returns_the_whole_turn_as_one_list(self):
        ReplayLlm.queue = list(RECORD["run"]["model_turns"])
        self.assertEqual(self.http.get("/list-apps").json(), RECORD["apps"])
        c.create_session(self.http, "s-run", {"team": "payments"})
        events = c.run(self.http, "s-run", RECORD["question"])
        self.assertEqual([c.describe(e) for e in events], RECORD["run"]["lines"])
        self.assertEqual(c.answer(events), RECORD["run"]["answer"])
        stored = self.http.get(f"/apps/{c.APP}/users/{c.USER}/sessions/s-run").json()
        self.assertEqual(stored["state"], {"team": "payments"})

    def test_run_sse_streams_partials_the_session_does_not_store(self):
        ReplayLlm.queue = list(RECORD["sse"]["model_turns"])
        c.create_session(self.http, "s-sse")
        events = list(c.run_sse(self.http, "s-sse", RECORD["question"]))
        self.assertEqual([c.describe(e) for e in events], RECORD["sse"]["lines"])
        stored = self.http.get(f"/apps/{c.APP}/users/{c.USER}/sessions/s-sse").json()
        self.assertEqual(len(stored["events"]), RECORD["sse"]["stored"])
        self.assertFalse(any(e.get("partial") for e in stored["events"]))

    def test_an_unknown_session_returns_404(self):
        body = {"app_name": c.APP, "user_id": c.USER, "session_id": "no-such-session", "new_message": c.message("hi")}
        r = self.http.post("/run", json=body)
        self.assertEqual((r.status_code, r.json()), (404, RECORD["failures"]["unknown_session"]["body"]))

    def test_auto_create_session_accepts_a_new_id(self):
        ReplayLlm.queue = list(RECORD["run"]["model_turns"])
        http = server(auto_create_session=True)
        events = c.run(http, "brand-new", RECORD["question"])
        self.assertEqual(c.answer(events), RECORD["run"]["answer"])

    def test_a_browser_origin_not_in_allow_origins_gets_403(self):
        body = {"app_name": c.APP, "user_id": c.USER, "session_id": "s-run", "new_message": c.message("hi")}
        r = self.http.post("/run", json=body, headers={"Origin": "https://docs-portal.example"})
        self.assertEqual((r.status_code, r.text), (403, RECORD["failures"]["origin"]["body"]))
        r = self.http.options("/run", headers={"Origin": "http://localhost:3000", "Access-Control-Request-Method": "POST"})
        self.assertEqual(r.headers.get("access-control-allow-origin"), "http://localhost:3000")

    def test_the_deprecated_per_id_path_nests_the_state(self):
        r = self.http.post(f"/apps/{c.APP}/users/{c.USER}/sessions/s-old", json={"state": {"team": "payments"}})
        self.assertEqual(r.json()["state"], RECORD["failures"]["deprecated_path"]["state"])
        self.assertEqual(c.create_session(self.http, "s-new", {"team": "payments"})["state"], {"team": "payments"})


if __name__ == "__main__":
    unittest.main()
record.pySource · pythonDownload
"""Record the docs assistant served by `adk api_server`, driven over HTTP and from `adk run`.

Run from this folder with GOOGLE_API_KEY set: python record.py --record
Starts the server on port 8765, writes run-output.txt and recorded-runs.json, then stops it.
"""
import json
import os
import subprocess
import socket
import sys
import time
from pathlib import Path

import httpx

import client as c

HERE = Path(__file__).resolve().parent
ADK = str(Path(sys.executable).with_name("adk"))
QUESTION = "How do I undo the last production deploy?"
ENV = {**os.environ, "GOOGLE_GENAI_USE_ENTERPRISE": "0", "GOOGLE_GENAI_USE_VERTEXAI": "0", "PYTHONWARNINGS": "ignore"}
SERVER = [ADK, "api_server", "--port", "8765", "--session_service_uri", "sqlite:///sessions.db",
          "--allow_origins", "http://localhost:3000", "."]


def model_turns(events):
    """The model's own responses, which the offline tests replay; thought signatures dropped."""
    turns = []
    for e in events:
        content = e.get("content") or {}
        if content.get("role") == "model":
            parts = [{k: v for k, v in p.items() if k != "thoughtSignature"} for p in content["parts"]]
            turns.append({"content": {"role": "model", "parts": parts}, "partial": bool(e.get("partial"))})
    return turns


def public_projection(value):
    """Omit opaque provider signatures without changing text, tool or usage fields."""
    opaque = {"thoughtSignature", "thought_signature", "functionCallSignature", "function_call_signature"}
    if isinstance(value, dict):
        return {key: public_projection(child) for key, child in value.items() if key not in opaque}
    if isinstance(value, list):
        return [public_projection(child) for child in value]
    return value


def short(event):
    keep = {k: event[k] for k in ("author", "invocationId", "partial", "content", "finishReason", "usageMetadata") if k in event}
    return public_projection(keep)


def main() -> None:
    # Select a free loopback port; never attach to another operator's server.
    with socket.socket() as probe:
        probe.bind(("127.0.0.1", 0))
        port = probe.getsockname()[1]
    server_command = list(SERVER)
    server_command[server_command.index("--port") + 1] = str(port)
    server_command += ["--host", "127.0.0.1"]
    (HERE / "sessions.db").unlink(missing_ok=True)
    out, rec = [], {"model": "gemini-3.8-flash", "question": QUESTION, "server": " ".join(["adk", *server_command[1:]])}
    log_path = HERE / "server-recording.log"
    server_log = log_path.open("w")
    server = subprocess.Popen(server_command, cwd=HERE, env=ENV, stdout=server_log, stderr=subprocess.STDOUT)
    try:
        with httpx.Client(base_url=f"http://127.0.0.1:{port}", timeout=120) as http:
            for _ in range(60):
                try:
                    if server.poll() is not None:
                        raise RuntimeError("Recording server stopped during startup")
                    response = http.get("/list-apps")
                    response.raise_for_status()
                    rec["apps"] = response.json()
                    break
                except httpx.ConnectError:
                    time.sleep(0.5)
            session = c.create_session(http, "s-run", {"team": "payments"})
            rec["created"] = {k: session[k] for k in ("id", "appName", "userId", "state", "events")}
            out += [f"$ {rec['server']}", f"$ python client.py {QUESTION!r}", f"apps: {rec['apps']}"]
            events = c.run(http, "s-run", QUESTION)
            rec["run"] = {"lines": [c.describe(e) for e in events], "answer": c.answer(events),
                          "events": [short(e) for e in events], "model_turns": model_turns(events)}
            out += rec["run"]["lines"] + [f"answer: {rec['run']['answer']}", ""]

            c.create_session(http, "s-sse")
            t0, seconds, streamed = time.perf_counter(), [], []
            for e in c.run_sse(http, "s-sse", QUESTION):
                streamed.append(e)
                seconds.append(round(time.perf_counter() - t0, 2))
            stored = http.get(f"/apps/{c.APP}/users/{c.USER}/sessions/s-sse").json()
            rec["sse"] = {"lines": [c.describe(e) for e in streamed], "seconds": seconds, "answer": c.answer(streamed),
                          "stored": len(stored["events"]), "model_turns": model_turns(streamed)}
            out += [f"$ python client.py --stream {QUESTION!r}"] + rec["sse"]["lines"]
            out += [f"yielded {len(streamed)} events; the session stored {rec['sse']['stored']}", ""]

            fail = {}
            body = {"app_name": c.APP, "user_id": c.USER, "session_id": "no-such-session", "new_message": c.message("hi")}
            r = http.post("/run", json=body)
            fail["unknown_session"] = {"status": r.status_code, "body": r.json()}
            body["session_id"] = "s-run"
            r = http.post("/run", json=body, headers={"Origin": "https://docs-portal.example"})
            fail["origin"] = {"status": r.status_code, "body": r.text}
            r = http.options("/run", headers={"Origin": "http://localhost:3000", "Access-Control-Request-Method": "POST"})
            fail["allowed_origin"] = {"status": r.status_code, "allow_origin": r.headers.get("access-control-allow-origin")}
            r = http.post(f"/apps/{c.APP}/users/{c.USER}/sessions/s-old", json={"state": {"team": "payments"}})
            fail["deprecated_path"] = {"status": r.status_code, "state": r.json()["state"]}
            rec["failures"] = fail
            out += ["$ curl -X POST /run with session_id no-such-session", f"HTTP {fail['unknown_session']['status']} {json.dumps(fail['unknown_session']['body'])}",
                    "$ curl -X POST /run -H 'Origin: https://docs-portal.example'", f"HTTP {fail['origin']['status']} {fail['origin']['body']}",
                    "$ curl -X POST /apps/docs_assistant/users/dev-1/sessions/s-old -d '{\"state\": {\"team\": \"payments\"}}'",
                    f"HTTP {fail['deprecated_path']['status']} state: {json.dumps(fail['deprecated_path']['state'])}", ""]
    finally:
        server.terminate()
        server.wait(timeout=20)

    cli = [ADK, "run", "--in_memory", "--replay", "replay.json", "docs_assistant"]
    proc = subprocess.run(cli, cwd=HERE, env=ENV, capture_output=True, text=True, timeout=300)
    lines = [l for l in proc.stdout.splitlines() if l.strip() and not l.startswith(("Log setup", "To access latest log"))]
    rec["cli_replay"] = {"command": "adk run --in_memory --replay replay.json docs_assistant", "stdout": lines, "exit": proc.returncode}
    out += [f"$ {rec['cli_replay']['command']}"] + lines + [""]
    cli = [ADK, "run", "--in_memory", "--jsonl", "docs_assistant", "Which page explains tokens?"]
    proc = subprocess.run(cli, cwd=HERE, env=ENV, capture_output=True, text=True, timeout=300)
    jsonl = [json.loads(l) for l in proc.stdout.splitlines() if l.startswith("{")]
    rec["cli_jsonl"] = {"command": "adk run --in_memory --jsonl docs_assistant 'Which page explains tokens?'", "records": public_projection(jsonl)}
    out += [f"$ {rec['cli_jsonl']['command']}"] + [json.dumps(j)[:150] + " …" for j in jsonl]
    (HERE / "sessions.db").unlink(missing_ok=True)
    (HERE / "run-output.txt").write_text("\n".join(out).rstrip() + "\n", encoding="utf-8")
    (HERE / "recorded-runs.json").write_text(json.dumps(public_projection(rec), indent=2, ensure_ascii=False) + "\n", encoding="utf-8")
    print("\n".join(out))


if __name__ == "__main__":
    import argparse
    parser = argparse.ArgumentParser(description="Optional live-model recording; overwrites saved run files.")
    parser.add_argument("--record", action="store_true", help="explicitly allow live model calls and record replacement")
    args = parser.parse_args()
    if not args.record:
        parser.error("recording is opt-in: use --record; normal lesson commands do not need this script")
    main()

Check your understanding

The docs portal allows its browser origin. A command-line client sends no Origin header. What does that origin policy establish?

Choose one answer, then check your reasoning.

Read the explanation

The lesson distinguishes origin checks from caller authentication; a client without Origin passes this check.

References

Checked October 22, 2026, against adk.dev and google-adk 2.12.0. The session and run request shapes come from the google.adk.cli.api_server module.

Previously on the blog: Google ADK Setup: adk web, CLI & Python Runner Guide covers the ADK 1.x version of these commands.

Written by Arjun Prabhulal.

Module 1

Run agents and manage sessions