|
| 1 | +"""Google ADK multi-agent-remote example: same pipeline as multi-agent but with sub-agents hosted remotely via A2A. |
| 2 | +
|
| 3 | +Demonstrates how to mix local orchestration with remote agent implementations: |
| 4 | +- Coordinator and formatter run locally (they hold the orchestration logic) |
| 5 | +- Specialist sub-agents (research, code) are RemoteA2aAgent instances hosted elsewhere |
| 6 | +- The remote services don't need to know about each other — coordination stays local |
| 7 | +
|
| 8 | +Compare with the multi-agent sample: |
| 9 | + multi-agent: Agent(tools=[search_web]) Agent(tools=[run_python]) |
| 10 | + multi-agent-remote: RemoteA2aAgent(agent_card=...) for each specialist |
| 11 | +
|
| 12 | +The key insight: RemoteA2aAgent cannot have sub_agents (it's not an LlmAgent), |
| 13 | +but a local Agent CAN have RemoteA2aAgent instances as its sub_agents. This lets |
| 14 | +you keep orchestration logic local while moving implementations to remote services. |
| 15 | +
|
| 16 | +Graph structure: |
| 17 | + __start__ → pipeline → coordinator → research_agent (RemoteA2aAgent) |
| 18 | + → code_agent (RemoteA2aAgent) |
| 19 | + → formatter (output_schema=ReportOutput) |
| 20 | + → __end__ |
| 21 | +
|
| 22 | +Schema resolution (handled by the runtime recursively): |
| 23 | + - input_schema: from FIRST sub_agent chain → coordinator.input_schema (ReportInput) |
| 24 | + - output_schema: from LAST sub_agent chain → formatter.output_schema (ReportOutput) |
| 25 | + - output_key: from LAST sub_agent chain → formatter.output_key ("report") |
| 26 | +""" |
| 27 | + |
| 28 | +import os |
| 29 | + |
| 30 | +import httpx |
| 31 | +from a2a.client.client import ClientConfig as A2AClientConfig |
| 32 | +from a2a.client.client_factory import ClientFactory as A2AClientFactory |
| 33 | +from a2a.types import TransportProtocol as A2ATransport |
| 34 | +from google.adk.agents import Agent, SequentialAgent |
| 35 | +from google.adk.agents.remote_a2a_agent import RemoteA2aAgent |
| 36 | +from pydantic import BaseModel, Field |
| 37 | + |
| 38 | + |
| 39 | +class ReportInput(BaseModel): |
| 40 | + """Structured input for the report generation pipeline.""" |
| 41 | + |
| 42 | + topic: str = Field( |
| 43 | + default="Natural Language Processing fundamentals", |
| 44 | + description="The topic to research and analyze", |
| 45 | + ) |
| 46 | + depth: str = Field( |
| 47 | + default="standard", |
| 48 | + description="How deep the analysis should be: 'brief', 'standard', or 'detailed'", |
| 49 | + ) |
| 50 | + |
| 51 | + |
| 52 | +class ReportOutput(BaseModel): |
| 53 | + """Structured output from the report generation pipeline.""" |
| 54 | + |
| 55 | + title: str = Field(description="Report title") |
| 56 | + summary: str = Field(description="Executive summary of findings") |
| 57 | + key_findings: list[str] = Field(description="Key findings as bullet points") |
| 58 | + code_snippet: str = Field(description="A relevant Python code example") |
| 59 | + |
| 60 | + |
| 61 | +# --- Shared HTTP client with authorization --- |
| 62 | +# |
| 63 | +# UIPATH_ACCESS_TOKEN is used to authenticate requests to the remote A2A endpoints. |
| 64 | +# Set it in your .env file before running. |
| 65 | +_access_token = os.environ.get("UIPATH_ACCESS_TOKEN", "") |
| 66 | + |
| 67 | + |
| 68 | +async def _log_request(request: httpx.Request): |
| 69 | + body = (request.content or b"")[:4000] |
| 70 | + print( |
| 71 | + f">>> {request.method} {request.url}\n body={body.decode(errors='replace')}", |
| 72 | + flush=True, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +async def _log_response(response: httpx.Response): |
| 77 | + await response.aread() |
| 78 | + print( |
| 79 | + f"<<< {response.status_code} {response.request.url}\n body={response.text[:4000]}", |
| 80 | + flush=True, |
| 81 | + ) |
| 82 | + |
| 83 | + |
| 84 | +_http_client = httpx.AsyncClient( |
| 85 | + headers={"Authorization": f"Bearer {_access_token}"}, |
| 86 | + timeout=httpx.Timeout(120.0), |
| 87 | + event_hooks={"request": [_log_request], "response": [_log_response]}, |
| 88 | +) |
| 89 | + |
| 90 | +_a2a_client_factory = A2AClientFactory( |
| 91 | + config=A2AClientConfig( |
| 92 | + httpx_client=_http_client, |
| 93 | + supported_transports=[A2ATransport.jsonrpc], |
| 94 | + streaming=False, |
| 95 | + polling=False, |
| 96 | + accepted_output_modes=["text"], |
| 97 | + ), |
| 98 | +) |
| 99 | + |
| 100 | +# --- Remote Sub-agents --- |
| 101 | +# |
| 102 | +# These replace the local Agent(tools=[...]) definitions from the multi-agent sample. |
| 103 | +# The coordinator delegates to them exactly the same way — the only difference is |
| 104 | +# that their implementation runs remotely via the A2A protocol. |
| 105 | +# |
| 106 | +# Replace the URLs with your actual deployed agent endpoints. |
| 107 | +ORG_NAME = "YourOrgName" |
| 108 | +TENANT_NAME = "YourTenantName" |
| 109 | +RESEARCH_AGENT_FOLDER_KEY = "a11f72b1-90fd-4b30-b733-f0285cbf4a19" |
| 110 | +RESEARCH_AGENT_RELEASE_ID = "1234" |
| 111 | + |
| 112 | +CODE_AGENT_FOLDER_KEY = "b22f83c2-91fe-5c41-c844-g1396dcg5b2a" |
| 113 | +CODE_AGENT_RELEASE_ID = "5678" |
| 114 | + |
| 115 | +research_agent = RemoteA2aAgent( |
| 116 | + name="research_agent", |
| 117 | + agent_card=f"https://cloud.uipath.com/{ORG_NAME}/{TENANT_NAME}/agenthub_/a2a/{RESEARCH_AGENT_FOLDER_KEY}/{RESEARCH_AGENT_RELEASE_ID}/.well-known/agent-card.json", |
| 118 | + description="Remote research specialist that searches the web and summarizes findings", |
| 119 | + a2a_client_factory=_a2a_client_factory, |
| 120 | +) |
| 121 | + |
| 122 | +code_agent = RemoteA2aAgent( |
| 123 | + name="code_agent", |
| 124 | + agent_card=f"https://cloud.uipath.com/{ORG_NAME}/{TENANT_NAME}/agenthub_/a2a/{CODE_AGENT_FOLDER_KEY}/{CODE_AGENT_RELEASE_ID}/.well-known/agent-card.json", |
| 125 | + description="Remote Python developer that writes and executes code examples", |
| 126 | + a2a_client_factory=_a2a_client_factory, |
| 127 | +) |
| 128 | + |
| 129 | + |
| 130 | +# --- Coordinator (local Agent, sub_agents are remote) --- |
| 131 | +# |
| 132 | +# Stays local so it can reference the remote sub_agents above. |
| 133 | +# RemoteA2aAgent cannot have sub_agents — only local Agents can. |
| 134 | +# The coordinator LLM decides when to call each remote sub-agent; |
| 135 | +# the remote services themselves don't need to know about each other. |
| 136 | +coordinator = Agent( |
| 137 | + name="coordinator", |
| 138 | + model="gemini-2.5-flash", |
| 139 | + instruction=( |
| 140 | + "You are a report coordinator. Given a topic:\n" |
| 141 | + "1. Delegate research to research_agent to gather information\n" |
| 142 | + "2. Delegate to code_agent to write a relevant Python code example\n" |
| 143 | + "3. Compile all findings into a comprehensive text report\n" |
| 144 | + "Include the research findings and the code example in your response." |
| 145 | + ), |
| 146 | + sub_agents=[research_agent, code_agent], |
| 147 | + input_schema=ReportInput, |
| 148 | + output_key="research_results", |
| 149 | +) |
| 150 | + |
| 151 | + |
| 152 | +# --- Formatter (local Agent with output_schema) --- |
| 153 | +# |
| 154 | +# Stays local — output_schema + structured JSON formatting is a local concern. |
| 155 | +formatter = Agent( |
| 156 | + name="formatter", |
| 157 | + model="gemini-2.5-flash", |
| 158 | + instruction=( |
| 159 | + "You are a report formatter. Take the research results from the previous " |
| 160 | + "step and format them into a structured report with a title, summary, " |
| 161 | + "key findings, and a code snippet. Output valid JSON matching the schema." |
| 162 | + ), |
| 163 | + output_schema=ReportOutput, |
| 164 | + output_key="report", |
| 165 | +) |
| 166 | + |
| 167 | + |
| 168 | +# --- Root: SequentialAgent pipeline --- |
| 169 | +agent = SequentialAgent( |
| 170 | + name="pipeline", |
| 171 | + sub_agents=[coordinator, formatter], |
| 172 | +) |
0 commit comments