Solution skill
opentelemetry ValueError: "Token was created in a different Context" with ParallelAgent and LlmAgent
Complete solution
Shows how to fix opentelemetry ValueError: "Token was created in a different Context" with ParallelAgent and LlmAgent. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.
TL;DR
If your code uses an async iterator, apply the workaround in this CPython comment: https://github.com/python/cpython/issues/118944#issuecomment-2105661088.
Steps
- [aabmass (maintainer)]: This is an upstream opentelemetry/cpython interaction, not an exporter bug: async iterators trip "Token was created in a different Context" after ParallelAgent sub-agents finish. If your code uses an async iterator, apply the workaround in this CPython comment: https://github.com/python/cpython/issues/118944#issuecomment-2105661088. Do not just swap startascurrentspan() for startspan(), that breaks span parent propagation.
When to use
You are seeing this: https://github.com/google/adk-python/issues/860: Issue google/adk-python#860 (closed, 27 comments): When using google.adk.agents.ParallelAgent with multiple google.adk.agents.LlmAgent instances as sub-agents, ValueError exceptions from the... Use this skill when you run into "opentelemetry ValueError: "Token was created in a different Context" with ParallelAgent and LlmAgent".
When not to use
If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.
Versions
No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.
Why this happens
The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.
Sources
- https://github.com/python/cpython/issues/118944#issuecomment-2105661088
- https://github.com/google/adk-python/issues/860:
Evidence
Issue gh:google/adk-python#860: the reporter asked: Issue google/adk-python#860 (closed, 27 comments): 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: 1. The verified answer was: [aabmass (maintainer)]: This is an upstream opentelemetry/cpython interaction, not an exporter bug: async iterators trip "Token was created in a different Context" after ParallelAgent sub-agents finish. If your code uses an async iterator, apply the workaround in this CPython comment: https://github.com/python/cpython/issues/118944#issuecomment-2105661088. Do not just swap startascurrentspan() for startspan(), that breaks span parent propagation. Evidence thread: https://github.com/google/adk-python/issues/860
By @issueatlas · Version 2 · Exact version
Embed this skill
Add this card to a website or documentation page. It links to the current skill on Vectle.
<a href="https://vectle.com/skills/skl_y7If3_8MyC_6wRwdxIRx1A" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">opentelemetry ValueError: "Token was created in a different Context" with ParallelAgent and LlmAgent <span>— Vectle</span></a>