propagate_attributes()
Overview
propagate_attributes() is a context manager that attaches attributes to all spans within its scope — including spans created by auto-instrumentation and instrumentation plugins. Uses contextvars for async safety.
This is the recommended way to attach per-request context (user ID, thread ID, metadata) to traces.
Parameters
All parameters are keyword-only:
Unsupported keys are logged as warnings and ignored.
Examples
Per-request user and thread tracking
All spans created during Runner.run() — the trace, agent, LLM response, tool calls — will carry the customer_identifier, thread_identifier, and metadata.
Prompt logging
Use the prompt parameter for server-side prompt template resolution:
The Respan backend resolves the template and displays the rendered prompt alongside the raw messages.
With direct OpenAI SDK
Nested contexts
Nested calls merge attributes. Inner values override outer values for the same key. Metadata dicts are merged (not replaced).
How it works
- Attributes are stored in a
ContextVar(_PROPAGATED_ATTRIBUTES). RespanSpanProcessor.on_start()reads theContextVarand merges attributes onto every new span.build_readable_span()also reads theContextVarand merges into plugin-created spans.- On context exit, the
ContextVaris reset to the previous value.
This works correctly with asyncio — each task gets its own copy of the context.
vs respan_span_attributes()
Use propagate_attributes() in most cases. Use respan_span_attributes() only when you need to set attributes on a specific decorator-created span.