Component: temporalio.contrib.openai_agents with OpenAIAgentsPlugin(use_otel_instrumentation=True) (SDK 1.32.0).
With OpenTelemetry instrumentation enabled, worker logs fill with ERROR opentelemetry.context: Failed to detach context tracebacks, one every time a temporal:startActivity span (likewise temporal:startChildWorkflow and temporal:startLocalActivity) finishes. The span is started in the workflow task's contextvars Context but finished from the activity handle's done callback, which asyncio runs in a copy of that Context, so the OpenInference processor cannot detach the OpenTelemetry context it attached when the span started. The fix (#1856) starts and finishes the span in one dedicated Context. Tracing output itself is correct; the errors are noise, but at ERROR level and frequent.
Technical details
openinference-instrumentation-openai-agents does self._tokens[span.span_id] = attach(...) in on_span_start and detach(token) in on_span_end. The SDK's tracing interceptor starts the temporal:start* spans synchronously with span.start(mark_as_current=True) and finishes them with handle.add_done_callback(lambda _: span.finish()). Future.add_done_callback captures contextvars.copy_context() and runs the callback in that copy, where ContextVar.reset(token) raises ValueError: ... was created in a different Context; opentelemetry.context.detach catches it and logs the traceback. Agent, turn and task spans start and finish in the same task Context and are not affected; workflow task boundaries and replay are not the trigger, though with max_cached_workflows=0 each span errors once more when the evicted run's handle is cancelled.
ERROR opentelemetry.context: Failed to detach context
Traceback (most recent call last):
File ".../opentelemetry/context/__init__.py", line 143, in detach
_RUNTIME_CONTEXT.detach(token)
File ".../opentelemetry/context/contextvars_context.py", line 42, in detach
self._current_context.reset(token)
ValueError: <Token var=<ContextVar name='current_context' default={} at 0x...> at 0x...> was created in a different Context
Repro: any workflow that runs Runner.run(agent) with the plugin above and add_temporal_spans at its default (True); watch the worker's log output. Workaround until the fix is released: a logging filter on the opentelemetry.context logger that drops records starting with Failed to detach context.
Component:
temporalio.contrib.openai_agentswithOpenAIAgentsPlugin(use_otel_instrumentation=True)(SDK 1.32.0).With OpenTelemetry instrumentation enabled, worker logs fill with
ERROR opentelemetry.context: Failed to detach contexttracebacks, one every time atemporal:startActivityspan (likewisetemporal:startChildWorkflowandtemporal:startLocalActivity) finishes. The span is started in the workflow task'scontextvarsContext but finished from the activity handle's done callback, which asyncio runs in a copy of that Context, so the OpenInference processor cannot detach the OpenTelemetry context it attached when the span started. The fix (#1856) starts and finishes the span in one dedicated Context. Tracing output itself is correct; the errors are noise, but at ERROR level and frequent.Technical details
openinference-instrumentation-openai-agentsdoesself._tokens[span.span_id] = attach(...)inon_span_startanddetach(token)inon_span_end. The SDK's tracing interceptor starts thetemporal:start*spans synchronously withspan.start(mark_as_current=True)and finishes them withhandle.add_done_callback(lambda _: span.finish()).Future.add_done_callbackcapturescontextvars.copy_context()and runs the callback in that copy, whereContextVar.reset(token)raisesValueError: ... was created in a different Context;opentelemetry.context.detachcatches it and logs the traceback. Agent, turn and task spans start and finish in the same task Context and are not affected; workflow task boundaries and replay are not the trigger, though withmax_cached_workflows=0each span errors once more when the evicted run's handle is cancelled.Repro: any workflow that runs
Runner.run(agent)with the plugin above andadd_temporal_spansat its default (True); watch the worker's log output. Workaround until the fix is released: a logging filter on theopentelemetry.contextlogger that drops records starting withFailed to detach context.