Description
When using OpenTelemetry instrumentation (via openinference-instrumentation-agno) with AgnoInstrumentor to trace agent/team operations, a context detach error occurs after the streaming response completes. The error happens during the cleanup phase when OpenTelemetry attempts to detach a context token that was created in a different context.
The functionality works correctly and produces expected results, but the error creates noise in logs and may indicate potential issues with context management in async streaming scenarios.
Steps to Reproduce
Steps to Reproduce
Set up OpenTelemetry instrumentation with Langfuse (or any OTLP endpoint)
Instrument agno using AgnoInstrumentor().instrument()
Create a Team with multiple agents
Call team.print_response() or team.run() with stream=True
Observe the context detach error in logs after the response completes
LANGFUSE_AUTH = base64.b64encode(
f"{os.getenv(‘LANGFUSE_PUBLIC_KEY’)}:{os.getenv(‘LANGFUSE_SECRET_KEY’)}“.encode()
).decode()
os.environ[“OTEL_EXPORTER_OTLP_ENDPOINT”] = “https://us.cloud.langfuse.com/api/public/otel”
os.environ[“OTEL_EXPORTER_OTLP_HEADERS”] = f"Authorization=Basic {LANGFUSE_AUTH}”
Failed to detach context
Traceback (most recent call last):
File “/Users/wangbw/anaconda3/envs/work/lib/python3.12/site-packages/opentelemetry/trace/init.py”, line 589, in use_span
yield span
File “/Users/wangbw/anaconda3/envs/work/lib/python3.12/site-packages/openinference/instrumentation/_tracers.py”, line 141, in start_as_current_span
yield cast(OpenInferenceSpan, current_span)
File “/Users/wangbw/anaconda3/envs/work/lib/python3.12/site-packages/openinference/instrumentation/agno/_wrappers.py”, line 479, in arun_stream
yield response
GeneratorExit
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File “/Users/wangbw/anaconda3/envs/work/lib/python3.12/site-packages/opentelemetry/context/init.py”, line 155, in detach
_RUNTIME_CONTEXT.detach(token)
File “/Users/wangbw/anaconda3/envs/work/lib/python3.12/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 0x152027b00> at 0x155c8cd80> was created in a different Context``
Python Version: 3.12
agno Version: Latest (please specify exact version, e.g., 1.7.5)
openinference-instrumentation-agno Version: Latest (please specify exact version, e.g., 0.1.10)
opentelemetry-sdk Version: Latest (please specify exact version, e.g., 1.35.0)
opentelemetry-api Version: Latest (please specify exact version, e.g., 1.35.0)
opentelemetry-exporter-otlp Version: Latest (please specify exact version, e.g., 1.35.0)