fix: ensure call_llm tracing spans are always ended in multi-agent setups#4742
fix: ensure call_llm tracing spans are always ended in multi-agent setups#4742atian8179 wants to merge 1 commit intogoogle:mainfrom
Conversation
…tups Replace start_as_current_span context manager with explicit span management in _call_llm_with_tracing to guarantee span.end() is called even when GeneratorExit is raised during async iteration. When transfer_to_agent triggers sub-agent execution, the parent agent's AsyncGenerator is closed, raising GeneratorExit inside the context manager. The OTel context.detach() then raises ValueError (stale contextvars token after async context switch), preventing span.end() from being reached. Unended spans are never exported. This fix uses try/finally with explicit span lifecycle to ensure spans are always properly ended and exported. Fixes google#4715
|
Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA). View this failed invocation of the CLA check for more information. For the most up to date status, view the checks section at the bottom of the pull request. |
Summary of ChangesHello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request addresses a critical bug in the OpenTelemetry tracing integration for multi-agent LLM flows. It ensures that tracing spans for parent agents are correctly exported by modifying the span management logic to explicitly handle the span lifecycle, preventing premature closure or unexported spans caused by asynchronous generator behavior and context switching errors. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
|
Response from ADK Triaging Agent Hello @atian8179, thank you for your contribution! Before we can review this PR, could you please sign the Contributor License Agreement (CLA)? You can find more information at https://cla.developers.google.com/. Also, could you please add a This information will help us to review your PR more efficiently. Thanks! |
There was a problem hiding this comment.
Code Review
This pull request effectively resolves a bug where tracing spans were not being correctly exported in multi-agent scenarios. The change from using tracer.start_as_current_span to explicit span lifecycle management with a try...finally block is the correct approach to ensure spans are always ended, even in the presence of GeneratorExit or ValueError during async operations. The implementation is robust and directly addresses the root cause described.
Problem
In multi-agent setups using
transfer_to_agent,call_llmtracing spans for parent agents are created but never exported. Only sub-agent spans appear in the trace backend.Root Cause
_call_llm_with_tracing()usestracer.start_as_current_span('call_llm')as a context manager around an async generator. Whentransfer_to_agenttriggers sub-agent execution and the generator is later closed,GeneratorExitis raised inside thewithblock. The OTelcontext.detach()then raisesValueError(stale contextvars token after async context switch), preventingspan.end()from being reached. Unended spans are never exported.This is the same root cause as #501 and #1670 (fixed in
base_agent.py), butbase_llm_flow.pywas not updated with the same pattern.Solution
Replace
start_as_current_spanwith explicit span lifecycle management:This ensures
span.end()is always reached regardless ofGeneratorExitorValueErrorfrom context detach.Fixes #4715