Building Google ADK Agents (Expert Mode)
p
When to use this skill
- New Agent Creation: Scaffolding
Agent,SequentialAgent,ParallelAgent, orLoopAgent. - Tool Development: Creating custom tools,
MCPintegrations, orFunctionToolwrappers. - Advanced Workflows: Implementing "Researcher-Writer", "Router", or hierarchical multi-agent systems.
- Infrastructure: Setting up
Artifacts(file storage),Sessions(state), orCallbacks(observability). - Deployment: Preparing agents for Cloud Run, GKE, or Vertex AI Agent Engine.
🚨 Critical Mandates
- Model Selection:
- Complex Reasoning:
gemini-3.5-flash(Default for logic/orchestration) - Speed/Efficiency:
gemini-3.1-flash-lite(Default for simple tasks/summarization) - Legacy: Avoid
gemini-1.5,gemini-2.0unless specifically requested.
- Structured Output:
- ALWAYS use Pydantic (Python) or Zod (TS) for structured data extraction.
- NEVER rely on prompt engineering alone for JSON. Use
output_schema.
- Code Quality:
- Type Hints: Mandatory for all Python tool functions (used for schema generation).
- Docstrings: Mandatory for all tools (used for LLM tool descriptions).
- Error Handling: Wrap tool execution in try/except blocks.
- Environment & Execution:
- ALWAYS use
uv: Mandated for project initialization (uv init), dependency management (uv add), and execution (uv run). - No Orphan Environments: Do not create or use non-uv venvs or global python environments.
🧠 Knowledge Base
This skill is backed by the full ADK documentation.
- Location:
resources/adk_docs.mdandresources/llms.txt - Usage: If you are unsure about an API signature, import, or pattern, SEARCH these files first.
grep "class Agent" resources/adk_docs.mdgrep "def save_artifact" resources/adk_docs.md
🛠️ Implementation Patterns
1. Structured Output (JSON)
Enforce strict schema adherence for reliable downstream processing.
from pydantic import BaseModel, Field
from google.adk.agents import Agent
# 1. Define Schema
class ResearchResult(BaseModel):
summary: str = Field(description="Executive summary of findings")
sources: list[str] = Field(description="List of URLs cited")
# 2. Configure Agent (Consolidated Agent class)
agent = Agent(
name="researcher",
model="gemini-3.1-flash-lite",
instruction="Analyze the given topic and return structured data.",
output_schema=ResearchResult, # <--- CRITICAL
output_key="research_data" # <--- Stores in ctx.session.state['research_data']
)
2. State & Artifacts
Manage conversation context and binary files (PDFs, Images).
from google.adk.agents import CallbackContext
async def my_tool(ctx: CallbackContext, data: str):
# Access Session State
user_id = ctx.session.state.get("user_id")
# Save Binary Artifact (e.g., generated PDF)
await ctx.save_artifact(
filename="report.pdf",
artifact=pdf_bytes,
mime_type="application/pdf"
)
3. Workflow Construction
Compose agents into deterministic or dynamic flows.
- Sequential:
A -> B -> C. Output of A becomes context for B. - Parallel:
[A, B, C]. Run all at once, aggregate results. - Loop:
A -> condition? -> A. Repeat until done.
from google.adk.agents import SequentialAgent
workflow = SequentialAgent(
name="blog_post_workflow",
agents=[researcher_agent, writer_agent, editor_agent],
output_key="final_post"
)
✅ Development Checklist
- Define Strategy:
- Single Agent vs. Multi-Agent?
- Stateful (needs memory) or Stateless?
- Scaffold:
- Create
main.pywithRunner. - Initialize
InMemorySessionServiceandInMemoryArtifactService(for local dev).
- Implement Tools:
- Use
@tooldecorator. - Annotate args with
Annotated[type, "description"]if using advanced typing.
- Verify:
- Run the agent locally using
uv run. - Check logs for tool calling errors.
4. Next-Gen Agent Engine Deployment
Utilize the vertexai.Client and vertexai.agent_engines.AdkApp pattern for robust Cloud deployments.
import vertexai
from vertexai.agent_engines import AdkApp
# 1. Initialize with Client
client = vertexai.Client(project="PROJ", location="LOC")
# 2. Wrap Agent
deployment_app = AdkApp(agent=root_agent, enable_tracing=True)
# 3. Idempotent Update or Create
all_engines = list(client.agent_engines.list())
target = next((e for e in all_engines if e.api_resource.display_name == "MY_APP"), None)
if target:
remote_app = client.agent_engines.update(
name=target.api_resource.name,
agent=deployment_app,
config={"display_name": "MY_APP", "staging_bucket": "gs://...", "requirements": "requirements.txt", "extra_packages": ["agent.py"]}
)
else:
remote_app = client.agent_engines.create(
agent=deployment_app,
config={"display_name": "MY_APP", "staging_bucket": "gs://...", "requirements": "requirements.txt", "extra_packages": ["agent.py"]}
)
🛑 Common Pitfalls to Avoid
- Missing
output_key: If you don't set this in a workflow, the parent agent won't get the result. - Missing Tool Types: The LLM cannot call a tool if the arguments are not typed (e.g.,
def func(x)vsdef func(x: int)). - Ignoring Model Version: Using an old model (
gemini-1.0) will result in poor instruction following. - Missing Local Serialization Dependency (
cloudpickle): Local deployment using theClientfromvertexai.agent_enginesrequires thecloudpicklepackage to serialize the agent state (e.g.agent_engine.pkl). Make surecloudpickleis listed in your local dependencies (pyproject.toml). - Missing Cloud Container Dependencies (
mcp,anthropic,google-cloud-aiplatform): If the agent uses theMcpToolsetor custom model integrations, those dependencies must be specified in the cloud deployment configuration'srequirements.txtfile, otherwise container loading fails at runtime. - PyPI 401 Authentication Bypasses: When running deployment commands in environments utilizing authenticated private indices with expired tokens, force dependencies to resolve from public PyPI using
uv run --default-index https://pypi.org/simpleorpip install --index-url https://pypi.org/simple. - Default App Name ValidationError: If
AdkAppis initialized without an explicitapp_name(e.g.AdkApp(agent=root_agent)), on remote deployment the template runner defaults the app name to the numericGOOGLE_CLOUD_AGENT_ENGINE_IDof the container. Because this starts with a digit, it fails the ADKAppname Pydantic validation. Always passapp_name=AGENT_ENGINE_NAMEexplicitly when initializingAdkApp. - Forcing Global Location for Model Routing: Models that serve from the
globalregion (such as preview models orgemini-3.5-flash) or third-party models (such asclaude-sonnet-4-6@defaultwhich is only servable inus-east5orglobalregions, and not inus-central1where Agent Engine runs) require routing location override. To achieve this, setos.environ["GOOGLE_CLOUD_LOCATION"] = "global"insideagent.pyat import time, or pass it viaenv_vars={"GOOGLE_CLOUD_LOCATION": "global"}in the deployment configuration dict. - Sub-Agent Search Tool Delegation: The built-in
google_searchtool is only supported for Gemini models. If you have a root agent using Claude (or any non-Gemini model), you can delegate search tasks to a Gemini sub-agent (e.g.gemini-2.5-flash) equipped withgoogle_searchby wrapping the sub-agent inAgentTooland providing it to the root agent's tools list. - AdkApp Client Query Method Bug (
Unsupported api mode): If querying a deployedAdkAppreasoning engine, the Python SDK client (ReasoningEngine) will fail to register the query methods and throwobject has no attribute 'query'becauseAdkAppexposesasync_stream_query(which containsasyncin its name, rejected as an invalid mode by the SDK). Bypass this by performing a direct authenticated HTTP POST request to the REST endpoint:https://{LOCATION}-aiplatform.googleapis.com/v1/{REASONING_ENGINE_RES}:streamQuery?alt=sse. - Gemini-Only Built-In Tools: The built-in
google_searchtool is only supported for Gemini models. If configuring a third-party Model Garden model (like Claude Sonnet 4.6), usinggoogle_searchwill raise a runtimeValueError: Google search tool is not supported for model .... For third-party models, write a custom python tool or use Brave Search MCP instead, or delegate to a Gemini search sub-agent.