|
| 1 | +# Google ADK Agents SDK Integration for Temporal |
| 2 | + |
| 3 | +This package provides the integration layer between the Google ADK and Temporal. It allows ADK Agents to run reliably within Temporal Workflows by ensuring determinism and correctly routing external calls (network I/O) through Temporal Activities. |
| 4 | + |
| 5 | +## Benefits of Temporal to the ADK |
| 6 | + |
| 7 | +Temporal provides a holistic, unified solution that centralizes your orchestration needs in one Workflow abstraction. Rather than cobbling together separate servers, task queues, gateways, and databases, you get: |
| 8 | + |
| 9 | +- **Recovering from crashes and stalls automatically**, rather than manually managing [sessions](https://google.github.io/adk-docs/sessions/session/#example-examining-session-properties) and [resuming](https://google.github.io/adk-docs/runtime/resume/#resume-a-stopped-workflow) them. (Google offers [Vertex Agent Engine](https://docs.cloud.google.com/agent-builder/agent-engine/sessions/manage-sessions-adk), which still leaves resumption to the user). No need to set up a separate [database](https://dev.to/greyisheepai/mastering-google-adk-databasesessionservice-and-events-complete-guide-to-event-injection-and-pdm#understanding-adk-databasesessionservice) |
| 10 | + - Along with [Retries](https://docs.temporal.io/encyclopedia/retry-policies) and mechanisms for handling backpressure and rate limits. |
| 11 | +- **Support for [ambient](https://temporal.io/blog/orchestrating-ambient-agents-with-temporal)/long-running agent patterns** via blocking awaits and [worker versioning](https://docs.temporal.io/production-deployment/worker-deployments/worker-versioning). |
| 12 | +- **Automatic execution state [persistence](https://docs.temporal.io/temporal-service/persistence)**, not just for agent interactions but for any custom automations in your workflows, without setting up a separate [database](https://dev.to/greyisheepai/mastering-google-adk-databasesessionservice-and-events-complete-guide-to-event-injection-and-pdm#understanding-adk-databasesessionservice). |
| 13 | +- For **Human-in-the-Loop patterns,** an api gateway to scalably [route](https://docs.temporal.io/task-routing) incoming messages (such as user chats) to awaken the correct workflow on your worker pool. |
| 14 | +- [**Long-running tools](https://google.github.io/adk-docs/tools-custom/function-tools/#long-run-tool) support** using [Activities](https://docs.temporal.io/activities) — no need to set up and maintain microservices. |
| 15 | +- [Manage and debug your agent workflow](https://temporal.io/resources/on-demand/demo-ai-agent) execution and pinpoint problems using Temporal UI. |
| 16 | + |
| 17 | +## Benefits of the ADK to Temporal |
| 18 | + |
| 19 | +ADK provides: (from the [ADK overview](https://google.github.io/adk-docs/#learn-more)): |
| 20 | + |
| 21 | +- Improved Agent development velocity with a first-class Agentic abstraction and integration with LLMs and an ecosystem of tools. |
| 22 | +- Improved agent robustness using built-in evals |
| 23 | +- Build complex agents using its Multi-agent architecture. |
| 24 | +- [Safety and security](https://google.github.io/adk-docs/safety/), via guardrails and integrations with sandboxing solutions like Vertex Agent Runtime. |
| 25 | + |
| 26 | +## What's Included |
| 27 | + |
| 28 | +### Core ADK Integration |
| 29 | +- **`TemporalModel`**: Intercepts model calls and executes them as Temporal activities |
| 30 | +- **`GoogleAdkPlugin`**: Worker plugin that configures runtime determinism and Pydantic serialization |
| 31 | +- **`invoke_model`**: Activity for executing LLM model calls with proper error handling |
| 32 | + |
| 33 | +### MCP (Model Context Protocol) Integration |
| 34 | +- **`TemporalMcpToolSet`**: Executes MCP tools as Temporal activities |
| 35 | +- **`TemporalMcpToolSetProvider`**: Manages toolset creation and activity registration |
| 36 | +- Full support for tool confirmation and event actions within workflows |
| 37 | + |
| 38 | +### OpenTelemetry Integration |
| 39 | +- Automatic instrumentation for ADK components when exporters are provided |
| 40 | +- Tracing integration that works within Temporal's execution context |
| 41 | +- Support for custom span exporters |
| 42 | + |
| 43 | +### Key Features |
| 44 | + |
| 45 | +#### 1. Deterministic Runtime |
| 46 | +- Replaces `time.time()` with `workflow.now()` when in workflow context |
| 47 | +- Replaces `uuid.uuid4()` with `workflow.uuid4()` for deterministic IDs |
| 48 | +- Automatic setup when using `GoogleAdkPlugin` |
| 49 | + |
| 50 | +#### 2. Activity-Based Model Execution |
| 51 | +Model calls are intercepted and executed as Temporal activities with configurable: |
| 52 | +- Timeouts (schedule-to-close, start-to-close, heartbeat) |
| 53 | +- Retry policies |
| 54 | +- Task queues |
| 55 | +- Cancellation behavior |
| 56 | +- Priority levels |
| 57 | + |
| 58 | +#### 3. Sandbox Compatibility |
| 59 | +- Automatic passthrough for `google.adk`, `google.genai`, and `mcp` modules |
| 60 | +- Works with both sandboxed and unsandboxed workflow runners |
| 61 | + |
| 62 | +#### 4. Advanced Serialization |
| 63 | +- Pydantic payload converter for ADK objects |
| 64 | +- Proper handling of complex ADK data types |
| 65 | +- Maintains type safety across workflow boundaries |
| 66 | + |
| 67 | +## Usage |
| 68 | + |
| 69 | +### Basic Setup |
| 70 | + |
| 71 | +**Agent (Workflow) Side:** |
| 72 | +```python |
| 73 | +from temporalio.contrib.google_adk_agents import TemporalModel |
| 74 | +from google.adk import Agent |
| 75 | + |
| 76 | + |
| 77 | +# Add to agent |
| 78 | +agent = Agent( |
| 79 | + name="test_agent", |
| 80 | + model=TemporalModel("gemini-2.5-pro"), |
| 81 | +) |
| 82 | +``` |
| 83 | + |
| 84 | +**Worker Side:** |
| 85 | + |
| 86 | +```python |
| 87 | +from temporalio.client import Client |
| 88 | +from temporalio.worker import Worker |
| 89 | +from temporalio.contrib.google_adk_agents import GoogleAdkPlugin |
| 90 | + |
| 91 | +client = await Client.connect( |
| 92 | + "localhost:7233", |
| 93 | + plugins=[ |
| 94 | + GoogleAdkPlugin(), |
| 95 | + ], |
| 96 | +) |
| 97 | + |
| 98 | +worker = Worker( |
| 99 | + client, |
| 100 | + task_queue="my-queue", |
| 101 | +) |
| 102 | +``` |
| 103 | + |
| 104 | +### Advanced Features |
| 105 | + |
| 106 | +**With MCP Tools:** |
| 107 | + |
| 108 | +```python |
| 109 | +import os |
| 110 | +from google.adk import Agent |
| 111 | +from google.adk.tools.mcp_tool import McpToolset |
| 112 | +from google.adk.tools.mcp_tool.mcp_session_manager import StdioConnectionParams |
| 113 | +from mcp import StdioServerParameters |
| 114 | +from temporalio.client import Client |
| 115 | +from temporalio.worker import Worker |
| 116 | + |
| 117 | +from temporalio.contrib.google_adk_agents import ( |
| 118 | + GoogleAdkPlugin, |
| 119 | + TemporalMcpToolSetProvider, |
| 120 | + TemporalMcpToolSet |
| 121 | +) |
| 122 | + |
| 123 | +# Create toolset provider |
| 124 | +provider = TemporalMcpToolSetProvider("my-tools", |
| 125 | + lambda _: McpToolset( |
| 126 | + connection_params=StdioConnectionParams( |
| 127 | + server_params=StdioServerParameters( |
| 128 | + command="npx", |
| 129 | + args=[ |
| 130 | + "-y", |
| 131 | + "@modelcontextprotocol/server-filesystem", |
| 132 | + os.path.dirname(os.path.abspath(__file__)), |
| 133 | + ], |
| 134 | + ), |
| 135 | + ), |
| 136 | + )) |
| 137 | + |
| 138 | +# Use in agent workflow |
| 139 | +agent = Agent( |
| 140 | + name="test_agent", |
| 141 | + model="gemini-2.5-pro", |
| 142 | + tools=[TemporalMcpToolSet("my-tools")] |
| 143 | +) |
| 144 | + |
| 145 | +client = await Client.connect( |
| 146 | + "localhost:7233", |
| 147 | + plugins=[ |
| 148 | + GoogleAdkPlugin(toolset_providers=[provider]), |
| 149 | + ], |
| 150 | +) |
| 151 | + |
| 152 | +# Configure worker |
| 153 | +worker = Worker( |
| 154 | + client, |
| 155 | + task_queue="task-queue" |
| 156 | +) |
| 157 | +``` |
| 158 | + |
| 159 | +## Integration Points |
| 160 | + |
| 161 | +This integration provides comprehensive support for running Google ADK Agents within Temporal workflows while maintaining: |
| 162 | +- **Determinism**: All non-deterministic operations are routed through Temporal |
| 163 | +- **Observability**: Full tracing and activity visibility |
| 164 | +- **Reliability**: Proper retry handling and error propagation |
| 165 | +- **Extensibility**: Support for custom tools via MCP protocol |
0 commit comments