Skip to content

Conversation

@viniciusdsmello
Copy link
Contributor

Pull Request

Summary

Fix context variable ValueError that occurs when async generators cross context boundaries in multi-agent systems. This issue manifests when using isolated execution contexts (e.g., Google ADK's AgentTool pattern, LangGraph sub-graphs, CrewAI delegation) where cleanup attempts to reset a context variable token in a different context than where it was created.

Changes

  • Added try-catch block in tracer.py:405-420 to gracefully handle context variable mismatch during async generator cleanup

Context

Problem: When agent frameworks create isolated execution contexts (AgentTool pattern), async generator cleanup fails with:
ValueError: <Token var= was created in a different ContextRoot Cause:

  1. Parent agent creates trace step with context variable token in Context A
  2. AgentTool/sub-agent runs in isolated Context B
  3. Async generator yields events across contexts
  4. Cleanup attempts _current_step.reset(token) in Context B with token from Context A
  5. Python's contextvars enforces context boundaries → ValueError

Why This Fix Works:

  • Traces are complete and sent before cleanup error occurs
  • Child context is destroyed immediately after, making stale references harmless

Testing

  • Unit tests (existing tests pass, error only occurs in cleanup)
  • Manual testing
    • Created AG-UI FastAPI server with Google ADK + AgentTool pattern
    • Test client sends requests triggering multiple AgentTool invocations
    • Verified no ValueError in logs after fix
    • Confirmed traces sent successfully to Openlayer
  • Postman CI/CD (not applicable - internal tracing fix)

@viniciusdsmello viniciusdsmello marked this pull request as ready for review November 13, 2025 13:37
@gustavocidornelas gustavocidornelas merged commit 5649f17 into main Nov 13, 2025
5 checks passed
@gustavocidornelas gustavocidornelas deleted the vini/open-8001-google-adk-integration-valueerror-token-varcontextvar branch November 13, 2025 18:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants