Skip to content

openai_agents OTel bridge logs "Failed to detach context" whenever a temporal:startActivity span finishes #1853

Description

@DABH

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.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions