WeSearch

Two Nasty Gotchas When Building Multi-Agent Systems with Google ADK

·3 min read · 0 reactions · 0 comments · 1 view
Two Nasty Gotchas When Building Multi-Agent Systems with Google ADK

Google's Agent Development Kit (ADK) makes it straightforward to compose LlmAgent instances into...

Original article
DEV Community
Read full at DEV Community →
Full article excerpt tap to expand

try { if(localStorage) { let currentUser = localStorage.getItem('current_user'); if (currentUser) { currentUser = JSON.parse(currentUser); if (currentUser.id === 41804) { document.getElementById('article-show-container').classList.add('current-user-is-article-author'); } } } } catch (e) { console.error(e); } Hiroshi Toyama Posted on Apr 28 Two Nasty Gotchas When Building Multi-Agent Systems with Google ADK #googleadk #llm #python #multiagent Google's Agent Development Kit (ADK) makes it straightforward to compose LlmAgent instances into multi-agent hierarchies. But two bugs bit me hard in production that aren't documented anywhere. Here's what happened and how to fix them. The Setup A root router LlmAgent with two sub-agents. Both sub-agents are module-level singletons — instantiated at import time, referenced from the root agent's constructor. # Agents/my_app/root_agent.py from Agents.my_app.sub_agent_a.agent import sub_agent_a from Agents.my_app.sub_agent_b.agent import sub_agent_b def _build_sub_agents() -> list: return [sub_agent_a, sub_agent_b] root_agent = LlmAgent( name="my_app", sub_agents=_build_sub_agents(), ... ) Enter fullscreen mode Exit fullscreen mode Worked fine locally with adk web. Blew up on Cloud Run. Bug 1: Agent already has a parent agent on module reload The error pydantic_core._pydantic_core.ValidationError: 1 validation error for LlmAgent Value error, Agent `SubAgentA` already has a parent agent, current parent: `my_app`, trying to add: `my_app` Enter fullscreen mode Exit fullscreen mode What's happening ADK's agent_loader calls importlib.import_module(agent_name) on every request. On the first request, it loads the module fresh and creates root_agent. The LlmAgent constructor sets sub_agent.parent_agent = root_agent for each sub-agent. On the second request, agent_loader reloads the module. Because sub_agent_a and sub_agent_b are module-level singletons, they're the same Python objects from the previous load — still carrying their parent_agent reference. When the new LlmAgent tries to assign the parent again, pydantic's validator rejects it. # Inside ADK's LlmAgent.__init__ (simplified) for sub in sub_agents: if sub.parent_agent is not None: raise ValueError(f"Agent `{sub.name}` already has a parent agent ...") sub.parent_agent = self Enter fullscreen mode Exit fullscreen mode This never surfaces locally because adk web loads the module only once per session. Cloud Run's request-per-reload behavior is what triggers it. The fix Reset parent_agent to None before passing sub-agents to the constructor: def _build_sub_agents() -> list: agents = [sub_agent_a, sub_agent_b] for agent in agents: agent.parent_agent = None # reset before each reload return agents Enter fullscreen mode Exit fullscreen mode This is safe because the assignment happens synchronously before the new parent is set. Bug 2: Context variable not found in instruction strings The error KeyError: 'Context variable not found: `hostname`.' Enter fullscreen mode Exit fullscreen mode Traceback points here: File ".../google/adk/utils/instructions_utils.py", line 124, in inject_session_state return await _async_sub(r'{+[^{}]*}+', _replace_match, template) Enter fullscreen mode Exit fullscreen mode What's happening ADK injects session state into agent instructions at runtime. The mechanism scans the instruction string with the regex r'{+[^{}]*}+' and replaces every {var_name} with the corresponding session state value. If your instruction contains an…

This excerpt is published under fair use for community discussion. Read the full article at DEV Community.

Anonymous · no account needed
Share 𝕏 Facebook Reddit LinkedIn Email

Discussion

0 comments

More from DEV Community