Repository navigation
Description
When using google.adk.agents.ParallelAgent with multiple google.adk.agents.LlmAgent instances as sub-agents, ValueError exceptions from the opentelemetry.context module are consistently logged. These errors occur after each sub-agent completes its processing. While the core functionality of the agents (e.g., LLM calls and output generation) appears to be unaffected, these errors create noise in the logs and suggest a potential issue with context management within the ADK framework or its interaction with opentelemetry.
Steps to Reproduce:
- Define multiple
LlmAgentinstances, each with its own model, instructions, and output schema. - Define an
AsyncExitStackand initialize tools (e.g.,MCPToolset). - Instantiate a
ParallelAgent, providing theLlmAgentinstances to itssub_agentsparameter. - The root agent setup returns the
ParallelAgentinstance and theAsyncExitStack. - Run the agent (e.g., by making a request to the FastAPI endpoint if using
google.adk.cli.fast_api).
Expected Behavior:
The ParallelAgent and its sub-agents should execute without opentelemetry context errors being logged.
Actual Behavior:
After each sub-agent finishes its execution and its output is generated, the following type of error is logged multiple times:
ERROR - __init__.py:157 - Failed to detach context
Traceback (most recent call last):
File "/path/to/your/env/lib/pythonX.Y/site-packages/opentelemetry/context/__init__.py", line 155, in detach
_RUNTIME_CONTEXT.detach(token)
File "/path/to/your/env/lib/pythonX.Y/site-packages/opentelemetry/context/contextvars_context.py", line 53, in detach
self._current_context.reset(token)
ValueError: <Token var=<ContextVar name='current_context' default={} at 0xXXXXXXXXX> at 0xYYYYYYYYY> was created in a different Context
(Note: Replace /path/to/your/env/lib/pythonX.Y/ and memory addresses with actuals from your logs if desired)
Environment:
- Operating System: macOS (darwin 23.5.0)
- Python Version: 3.12
- Google ADK Version: 0.4.0
- Minimal Reproducible Code (
agent.py):import asyncio from google.adk.agents import LlmAgent, BaseAgent, ParallelAgent from pydantic import BaseModel, Field from google.adk.tools.mcp_tool.mcp_toolset import MCPToolset, SseServerParams from contextlib import AsyncExitStack import enum # ... (Out_of_office_category, Out_of_office_output_schema definitions) ... # ... (New_lead_category, New_lead_output_schema definitions) ... # ... (Book_meeting_category, Book_meeting_output_schema definitions) ... # ... (General_type_reply_category, General_type_reply_output_schema definitions) ... # [PASTE YOUR Enum and BaseModel Schema definitions here for completeness] class Out_of_office_category(enum.Enum): resignation = "resignation" holiday = "holiday" other = "other" not_out_of_office = "not_out_of_office" class Out_of_office_output_schema(BaseModel): out_of_office: bool = Field(description="Whether the email is out of office or not") category: Out_of_office_category = Field(description="The category of the email") reason: str = Field(description="The reason for the email being out of office") class New_lead_category(enum.Enum): not_right_person_add_new_lead_new_email = "not_right_person_add_new_lead_new_email" right_person_add_new_lead_new_email = "right_person_add_new_lead_new_email" right_person_add_new_lead_existing_email = "right_person_add_new_lead_existing_email" not_add_new_lead = "not_add_new_lead" class New_lead_output_schema(BaseModel): new_lead: bool = Field(description="Whether the email contains a new lead or not") new_lead_category: New_lead_category = Field(description="The category of the email") reason: str = Field(description="The reason for the email being a new lead or not") class Book_meeting_category(enum.Enum): book_meeting = "book_meeting" book_meeting_details = "book_meeting_details" not_book_meeting = "not_book_meeting" class Book_meeting_output_schema(BaseModel): book_meeting: bool = Field(description="Whether the email contains a request to book a meeting or not") book_meeting_category: Book_meeting_category = Field(description="The category of the email") reason: str = Field(description="The reason for the email being a book meeting or not") class General_type_reply_category(enum.Enum): positive = "positive" negative = "negative" class General_type_reply_output_schema(BaseModel): general_type_reply_category: General_type_reply_category = Field(description="The category of the email") reason: str = Field(description="The reason for the email being a positive or negative reply") async def reply_agent(): common_exit_stack = AsyncExitStack() tools, _ = await MCPToolset.from_server( connection_params=SseServerParams( url="http://44.214.120.160:8002/sse" # Example URL ), async_exit_stack=common_exit_stack ) out_of_office_agent = LlmAgent( model="gemini-2.0-flash", name="out_of_office_agent", description="check whether the email is out of office or not...", instruction=\"\"\"...\"\"\", # Shorten for brevity or refer to full agent code output_schema=Out_of_office_output_schema, output_key="out_of_office_agent" ) new_lead_agent = LlmAgent( model="gemini-2.0-flash", name="new_lead_agent", description="Check whether the email contains a new lead...", instruction=\"\"\"...\"\"\", output_schema=New_lead_output_schema, output_key="new_lead_agent" ) book_meeting_agent = LlmAgent( model="gemini-2.0-flash", name="book_meeting_agent", description="Check whether the email contains a request to book a meeting...", instruction=\"\"\"...\"\"\", output_schema=Book_meeting_output_schema, output_key="book_meeting_agent" ) general_type_reply_agent = LlmAgent( model="gemini-2.0-flash", name="general_type_reply_agent", description="Classify the email into 'positive', 'negative'...", instruction=\"\"\"...\"\"\", output_schema=General_type_reply_output_schema, output_key="general_type_reply_agent" ) return out_of_office_agent, new_lead_agent, book_meeting_agent, general_type_reply_agent, common_exit_stack async def main(): out_of_office_agent, new_lead_agent, book_meeting_agent, general_type_reply_agent, common_exit_stack = await reply_agent() reply_intent_agent = ParallelAgent( name="replyagent", description="Analyze the email and determine the intent of the email.", sub_agents=[out_of_office_agent, new_lead_agent, book_meeting_agent, general_type_reply_agent] ) return reply_intent_agent, common_exit_stack root_agent = main()
Additional Context:
- The sub-agents themselves successfully process inputs and produce the expected outputs via LLM calls. The
opentelemetryerrors seem to occur around the completion/logging phase of each sub-agent's lifecycle within theParallelAgent.
Activity
calvingiles commentedon May 23, 2025
Disclaimer: not googler.
We have seen this, and it seems to be caused by another error, and the trace error is masking the underlying error.
Colstuwjx commentedon May 26, 2025
similar issue reproduced on my local, it would be great if we could fix this issue.
Jacksunwei commentedon May 28, 2025
Could you retry with 1.0.0. I think we've fixed this already at head.
Colstuwjx commentedon May 28, 2025
Hi @Jacksunwei , unfortuantely, I had just reproduced this issue by using 1.0.0 . See the error logs below:
As we could see, while the llm invocation failed, the tracing job has also thrown panic.
stefanadelbert commentedon May 30, 2025
I'm getting the same on 1.1.1.
antonpp commentedon Jun 2, 2025
Have same issue without parallel agent just some tools and an AgentTool.
petitoff commentedon Jun 11, 2025
This is problem with google
opentelemetry-exporter-gcp-tracethat works with OpenTelemetry 1.34.x. The latest available version is still 1.9.0, which is incompatible with the newer OpenTelemetry API. I tested the temporary patch that works. You can disable that telemetry with code below in file where you have root agent.This is only temp fix.
rcleveng commentedon Jun 25, 2025
I'm also seeing it randomly with 1.42 and had not before.
rcleveng commentedon Jun 26, 2025
What is the issue number to track the
opentelemetry-exporter-gcp-traceissue?pyralix commentedon Jun 29, 2025
I'm seeing this issue in 1.5 when using ParallelAgents
6 remaining items
tuanchris commentedon Jul 20, 2025
+1
mykytaterentiev commentedon Jul 22, 2025
+1
idreamsahan commentedon Jul 22, 2025
+1 Have the same issue will try the temp fix. thanks
pallaoro commentedon Jul 23, 2025
+1
aabmass commentedon Jul 24, 2025
I don't think this bug is related to
opentelemetry-exporter-gcp-trace. For some context on what is happening see the following issuesI would recommend this fix python/cpython#118944 (comment) as a quick and correct workaround if the issue is an async iterator. Changing from
start_as_current_span()->start_span()is not a great fix because it breaks span parent propagationjjbi123 commentedon Jul 24, 2025
+1
svelezdevilla commentedon Jul 25, 2025
+1
rcleveng commentedon Jul 25, 2025
So the plan will be to update everywhere with:
is that right? That sounds good, I hadn't seen aclose in contextlib before
KiranRaghavendra1248 commentedon Jul 28, 2025
+1 - Having same issue
me-her commentedon Jul 29, 2025
+1
Jacksunwei commentedon Aug 12, 2025
We're using #1670 to track the effort to fix this. This is a non-trival one, I have to say..