Skip to content

Crash while exiting loop agent in adk web #501

Closed

Description

@ThomasSilloway

Describe the bug

When a sub-agent within a google.adk.agents.LoopAgent signals termination using tool_context.actions.escalate = True, the loop terminates, but an opentelemetry.context error (ValueError: <Token ...> was created in a different Context) occurs immediately after the sub-agent's GeneratorExit. This appears to be an issue with OpenTelemetry context handling during the abrupt termination of the agent's async generator, triggered by the ADK escalation mechanism.

To Reproduce

Steps to reproduce the behavior:

  1. Install the Google ADK (google-adk) v0.3
  2. Create a LoopAgent that runs a sub-agent (e.g., an LlmAgent as shown in the code below).
  3. Define a tool for the sub-agent that accepts tool_context: ToolContext as an argument (see process_backlog_file example below).
  4. Implement logic within the tool to set tool_context.actions.escalate = True under a specific condition (e.g., when a backlog file is empty).
  5. Run the LoopAgent (e.g., via adk web or programmatically) and trigger the condition in the sub-agent's tool that sets escalate = True.
  6. Observe the application logs for the ValueError: ... was created in a different Context traceback immediately following the GeneratorExit.

Expected behavior

The LoopAgent should terminate cleanly when escalate is set to True by the sub-agent's tool, without any subsequent OpenTelemetry context detachment errors in the traceback.

Screenshots

(Not applicable, traceback provided below)

Desktop (please complete the following information):

  • OS: Windows
  • Python version(python -V): Python 3.13.2
  • ADK version(pip show google-adk): 0.3.0

Additional context

  • This issue appears related to known context propagation challenges within opentelemetry-python when dealing with async generators and abrupt exits, particularly involving contextvars. See related discussion: open-telemetry/opentelemetry-python#2606
  • The core ADK escalation mechanism (tool_context.actions.escalate = True) seems to function correctly in triggering the loop termination, but the subsequent cleanup phase within the ADK's flow hits this OpenTelemetry context issue.

Traceback:

2025-05-01 18:10:32,719 - INFO - fast_api.py:634 - Generated event in agent run streaming: {"content":{"parts":[{"functionResponse":{"id":"adk-92371e11-2c3e-4da0-b4a4-d301ae5caba3","name":"process_backlog_file","response":{"status":"empty","message":"Backlog is empty or not found."}}}],"role":"user"},"invocation_id":"e-0dae2740-4239-4fe5-9ec9-e740fc59c411","author":"BacklogReaderAgent_PoC","actions":{"state_delta":{},"artifact_delta":{},"escalate":true,"requested_auth_configs":{}},"id":"LNVzw0lc","timestamp":1746137432.719034}
2025-05-01 18:10:32,719 - ERROR - init.py:157 - Failed to detach context
Traceback (most recent call last):
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\trace_init_.py", line 587, in use_span
yield span
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\sdk\trace_init_.py", line 1105, in start_as_current_span
yield span
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\trace_init_.py", line 452, in start_as_current_span
yield span
File "C:\path\to\project\src\venv\Lib\site-packages\google\adk\agents\base_agent.py", line 142, in run_async
yield event
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\context_init_.py", line 155, in detach
RUNTIME_CONTEXT.detach(token)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File "C:\path\to\project\src\venv\Lib\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 0x000001F4CAA97A10> at 0x000001F4D72A7FC0> was created in a different Context
2025-05-01 18:10:32,722 - ERROR - init.py:157 - Failed to detach context
Traceback (most recent call last):
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\trace_init.py", line 587, in use_span
yield span
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\sdk\trace_init.py", line 1105, in start_as_current_span
yield span
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\trace_init_.py", line 452, in start_as_current_span
yield span
File "C:\path\to\project\src\venv\Lib\site-packages\google\adk\flows\llm_flows\base_llm_flow.py", line 487, in _call_llm_async
yield llm_response
GeneratorExit

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\path\to\project\src\venv\Lib\site-packages\opentelemetry\context_init_.py", line 155, in detach
_RUNTIME_CONTEXT.detach(token)
~~~~~~~~~~~~~~~~~~~~~~~^^^^^^^
File "C:\path\to\project\src\venv\Lib\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 0x000001F4CAA97A10> at 0x000001F4D732CDC0> was created in a different Context

Relevant Code Snippets (Illustrative):

Loop Agent Definition (sleepy_dev_poc/agent.py):

# Defines the root LoopAgent for the PoC
import logging
from google.adk.agents import LoopAgent
from .sub_agents.backlog_reader.agent import backlog_reader_agent
from .shared_libraries import constants

logging.basicConfig(level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')
logger = logging.getLogger(__name__)

root_agent = LoopAgent(
    name=constants.ROOT_AGENT_NAME,
    description="Root agent that loops through the BacklogReaderAgent until the backlog is empty (escalation).",
    sub_agents=[backlog_reader_agent]
)
logger.info(f"Initialized {constants.ROOT_AGENT_NAME} with sub-agent: {backlog_reader_agent.name}")

Sub-Agent Tool (sleepy_dev_poc/sub_agents/backlog_reader/tools.py):

# Tool for processing the backlog file
import logging
import os
from typing import Dict, Any, Optional
from google.adk.tools import ToolContext
from ...shared_libraries import constants

# ... (logging setup) ...

def process_backlog_file(tool_context: Optional[ToolContext] = None) -> Dict[str, Any]:
    # ... (get file path: abs_file_path) ...
    if tool_context is None:
         # ... (error handling) ...
         return {"status": "error", "message": "Critical Error: ToolContext is missing."}
    try:
        if not os.path.exists(abs_file_path) or os.path.getsize(abs_file_path) == 0:
            logger.info(f"{constants.BACKLOG_READER_AGENT_NAME} - Tool: Backlog file is empty... Signaling escalation.")
            tool_context.actions.escalate = True # <<<--- ESCALATION SIGNAL
            return {"status": "empty", "message": "Backlog is empty or not found."}
        # ... (read/write file logic) ...
        tool_context.actions.escalate = False
        return {"status": "ok", "task_description": first_line, "message": "Task processed successfully."}
    except Exception as e:
        # ... (error handling) ...
        if tool_context:
             tool_context.actions.escalate = True # Escalate on error too
        return {"status": "error", "message": f"An error occurred: {str(e)}"}

Sub-Agent Definition (sleepy_dev_poc/sub_agents/backlog_reader/agent.py):

# Defines the BacklogReaderAgent
import logging
from google.adk.agents import LlmAgent
from google.adk.tools import FunctionTool
from ...shared_libraries import constants
from . import tools
from .prompt import BACKLOG_READER_AGENT_PROMPT

# ... (logging setup) ...

process_backlog_tool = FunctionTool(func=tools.process_backlog_file)

backlog_reader_agent = LlmAgent(
    name=constants.BACKLOG_READER_AGENT_NAME,
    model=constants.MODEL_NAME,
    instruction=BACKLOG_READER_AGENT_PROMPT,
    tools=[process_backlog_tool],
    description="Reads tasks one by one from the backlog file using a tool and signals when empty.",
)
logger.info(f"Initialized {constants.BACKLOG_READER_AGENT_NAME} with model {constants.MODEL_NAME}")

Activity

  1. added
    core[Component] This issue is related to the core interface and implementation
    on May 2, 2025
  2. added theissue type on May 2, 2025
  3. kavinkumar807 commented on May 2, 2025

    @kavinkumar807
    Contributor

    @boyangsvl shall I look into this??

  4. boyangsvl commented on May 2, 2025

    @boyangsvl
    Collaborator

    Yes, please, thanks!

  5. kavinkumar807 commented on May 3, 2025

    @kavinkumar807
    Contributor

    @ThomasSilloway Could please you share me the prompt and model for subagent ? as of now using prompt same as description and model is gemini-2.0-flash

  6. kavinkumar807 commented on May 3, 2025

    @kavinkumar807
    Contributor

    This is issue is not reproducible in my Mac machine the loop agent mentioned in the docs as well as agent mentioned in the issue works fine. Adding below video for reference.

    Screen.Recording.2025-05-04.at.12.17.49.AM.1.mp4

    Will check whether this is reproducible with my Windows machine.

    cc: @hangfei @boyangsvl

  7. ThomasSilloway commented on May 3, 2025

    @ThomasSilloway
    Author

    Here you go, it happens 100% on my poc1 of sleepy AI army once it gets to the end of the loop it generates that error

    https://github.com/ThomasSilloway/Sleepy-AI-Army/tree/main/poc-1-core-loop

    Your example shows looping one time, when I noticed it was with 2-5 items that the loop goes through. I didn't test with 1 to repro it

  8. kavinkumar807 commented on May 4, 2025

    @kavinkumar807
    Contributor

    @ThomasSilloway Thanks, now able to reproduce will check with it.

  9. kavinkumar807 commented on May 5, 2025

    @kavinkumar807
    Contributor

    @boyangsvl @hangfei Raised PR for the bug

  10. hangfei commented on May 31, 2025

    @hangfei
    Collaborator

    @kavinkumar807 thanks. We are reviewing the fix.

  11. lsinghkochava commented on Jun 11, 2025

    @lsinghkochava

    I'm facing the same, looking forward to this fix making its way!

  12. added
    need attention[Status] This issue is important but the maintainers haven't started working on it
    on Jul 2, 2025
  13. iramaraju-proton commented on Aug 22, 2025

    @iramaraju-proton

    Have same issue, had to remove open telemetry wiring to get around temporarily. Look forward to the fix.

  14. self-assigned this
    on Oct 7, 2025
  15. klateefa commented on Oct 7, 2025

    @klateefa

    Hi @ThomasSilloway and thanks to everyone who participated in this thread.

    We're currently doing some housekeeping on our issue tracker. Given the rapid pace of development and the number of updates since this was last active, there's a good chance this issue has been resolved in a newer version. To help us keep our backlog focused on current issues, we are closing this as stale.

    If you're still facing this problem, could you please update to the very latest version of the library and confirm the issue still exists? If it does, please comment here, and we will be happy to reopen this issue and investigate further.

    Thanks for your contribution and understanding!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

core[Component] This issue is related to the core interface and implementationneed attention[Status] This issue is important but the maintainers haven't started working on it

Type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions