Files
George Weale 839e898fcf docs(tutorials): fix ToolContext, output_key and persistence claims (#2032)
* docs(tutorials): fix ToolContext, output_key and persistence claims

* Update agent-team.md

* docs: address review — revert get-started changes, move streaming note

Reverts docs/get-started/python.md entirely and drops the edit to the
retired quickstart-streaming.md page. The corrected streaming caveat now
lands on docs/live/get-started/streaming-python.md, scoped to the
run_async path and naming SequentialAgent as the only workflow agent
with live support.

---------

Co-authored-by: Joe Fernandez <931947+joefernandez@users.noreply.github.com>
2026-08-07 13:15:39 -07:00

191 lines
7.8 KiB
Markdown

# Build a streaming agent with Python
With this quickstart, you'll learn to create a simple agent and use ADK Streaming to enable voice and video communication with it that is low-latency and bidirectional. We will install ADK, set up a basic "Google Search" agent, try running the agent with Streaming with `adk web` tool, and then explain how to build a simple asynchronous web app by yourself using ADK Streaming and [FastAPI](https://fastapi.tiangolo.com/).
**Note:** This guide assumes you have experience using a terminal in Windows, Mac, and Linux environments.
## Supported models for voice/video streaming {#supported-models}
In order to use voice/video streaming in ADK, you will need to use Gemini models that support the Live API. You can find the **model ID(s)** that supports the Gemini Live API in the documentation:
- [Google AI Studio: Gemini Live API](https://ai.google.dev/gemini-api/docs/models#live-api)
- [Agent Platform: Gemini Live API](https://cloud.google.com/vertex-ai/generative-ai/docs/live-api)
## 1. Setup Environment & Install ADK { #setup-environment-install-adk }
Create & Activate Virtual Environment (Recommended):
```bash
# Create
python3 -m venv .venv
# Activate (each new terminal)
# macOS/Linux: source .venv/bin/activate
# Windows CMD: .venv\Scripts\activate.bat
# Windows PowerShell: .venv\Scripts\Activate.ps1
```
Install ADK:
```bash
pip install google-adk
```
## 2. Project Structure { #project-structure }
Create the following folder structure with empty files:
```console
adk-streaming/ # Project folder
└── app/ # the web app folder
├── .env # Gemini API key
└── google_search_agent/ # Agent folder
├── __init__.py # Python package
└── agent.py # Agent definition
```
### agent.py
Copy-paste the following code block into the `agent.py` file.
For `model`, please double-check the model ID as described earlier in the [Models section](#supported-models).
```py
from google.adk.agents import Agent
from google.adk.tools import google_search # Import the tool
root_agent = Agent(
# A unique name for the agent.
name="basic_search_agent",
# The Large Language Model (LLM) that agent will use.
# Please fill in the latest model id that supports live from
# https://adk.dev/live/get-started/streaming-python/#supported-models
model="...",
# A short description of the agent's purpose.
description="Agent to answer questions using Google Search.",
# Instructions to set the agent's behavior.
instruction="You are an expert researcher. You always stick to the facts.",
# Add google_search tool to perform grounding with Google search.
tools=[google_search]
)
```
`agent.py` is where all your agent(s)' logic will be stored, and you must have a `root_agent` defined.
Notice how easily you integrated [grounding with Google Search](https://ai.google.dev/gemini-api/docs/grounding?lang=python#configure-search) capabilities. The `Agent` class and the `google_search` tool handle the complex interactions with the LLM and grounding with the search API, allowing you to focus on the agent's *purpose* and *behavior*.
![intro_components.png](../../assets/quickstart-streaming-tool.png)
Copy-paste the following code block to `__init__.py` file.
```py title="__init__.py"
from . import agent
```
## 3\. Set up the platform { #set-up-the-platform }
To run the agent, choose a platform from either Google AI Studio or Google Cloud Agent Platform:
=== "Gemini - Google AI Studio"
1. Get an API key from [Google AI Studio](https://aistudio.google.com/apikey).
2. Open the **`.env`** file located inside (`app/`) and copy-paste the following code.
```env title=".env"
GOOGLE_GENAI_USE_ENTERPRISE=FALSE
GOOGLE_API_KEY=PASTE_YOUR_ACTUAL_API_KEY_HERE
```
3. Replace `PASTE_YOUR_ACTUAL_API_KEY_HERE` with your actual `API KEY`.
=== "Gemini - Google Cloud Agent Platform"
1. You need an existing
[Google Cloud](https://cloud.google.com/?e=48754805&hl=en) account and a
project.
* Set up a
[Google Cloud project](https://cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal#setup-gcp)
* Set up the
[gcloud CLI](https://cloud.google.com/vertex-ai/generative-ai/docs/start/quickstarts/quickstart-multimodal#setup-local)
* Authenticate to Google Cloud, from the terminal by running
`gcloud auth login`.
* [Enable the Agent Platform API](https://console.cloud.google.com/flows/enableapi?apiid=aiplatform.googleapis.com).
2. Open the **`.env`** file located inside (`app/`). Copy-paste
the following code and update the project ID and location.
```env title=".env"
GOOGLE_GENAI_USE_ENTERPRISE=TRUE
GOOGLE_CLOUD_PROJECT=PASTE_YOUR_ACTUAL_PROJECT_ID
GOOGLE_CLOUD_LOCATION=us-central1
```
For more information on connecting to Google Cloud from ADK agents, see
[Connect to Google Cloud and Agent Platform](/get-started/google-cloud/).
## 4. Try the agent with `adk web` { #try-the-agent-with-adk-web }
Now it's ready to try the agent. Run the following command to launch the **dev UI**. First, make sure to set the current directory to `app`:
```shell
cd app
```
Also, set `SSL_CERT_FILE` variable with the following command. This is required for the voice and video tests later.
=== "OS X &amp; Linux"
```bash
export SSL_CERT_FILE=$(python3 -m certifi)
```
=== "Windows"
```powershell
$env:SSL_CERT_FILE = (python3 -m certifi)
```
Then, run the dev UI:
```shell
adk web
```
!!!info "Note for Windows users"
When hitting the `_make_subprocess_transport NotImplementedError`, consider using `adk web --no-reload` instead.
!!! warning "Caution: ADK Web for development only"
ADK Web is ***not meant for use in production deployments***. You should
use ADK Web for development and debugging purposes only.
Open the URL provided (usually `http://localhost:8000` or
`http://127.0.0.1:8000`) **directly in your browser**. This connection stays
entirely on your local machine. Select `google_search_agent`.
### Try with voice and video
To try with voice, reload the web browser, click the microphone button to enable the voice input, and ask the the following questions in voice. The agent will use the google_search tool to get the latest information to answer those questions. You will hear the answer in voice in real-time.
* What is the weather in New York?
* What is the time in New York?
* What is the weather in Paris?
* What is the time in Paris?
To try with video, reload the web browser, click the camera button to enable the video input, and ask questions like "What do you see?". The agent will answer what they see in the video input.
#### Caveat
- You can not use text chat with the native-audio models. You will see errors when entering text messages on `adk web`.
### Stop the tool
Stop `adk web` by pressing `Ctrl-C` on the console.
### Note on ADK Streaming
Model callbacks (`before_model_callback` and `after_model_callback`) are not invoked on the streaming path; ADK only runs them on the `run_async` path. Agent callbacks (`before_agent_callback`, `after_agent_callback`) and tool callbacks (`before_tool_callback`, `after_tool_callback`) do run while streaming, as do `LongRunningFunctionTool` and `ExampleTool`. Of the workflow agents, only `SequentialAgent` supports streaming: `LoopAgent` and `ParallelAgent` raise `NotImplementedError`.
Congratulations\! You've successfully created and interacted with your first Streaming agent using ADK\!
## Next steps: build custom streaming app
The [Gemini Live API Toolkit development guide series](../dev-guide/part1.md) gives an overview of the server and client code for a custom asynchronous web app built with ADK Streaming, enabling real-time, bidirectional audio and text communication.