OpenInference provides a set of context managers that attach attributes to every span created while they’re in scope. They are the easiest way to add cross-cutting context — session ID, user ID, metadata, tags, prompt templates — without modifying every span by hand. Context managers are the recommended way to handle attributes that apply to a group of spans. For attributes that only apply to one span, set them directly on that span instead.Documentation Index
Fetch the complete documentation index at: https://arize-ax.mintlify.dev/docs/llms.txt
Use this file to discover all available pages before exploring further.
How They Work
Each context manager attaches a value to the OpenTelemetry context. When a span is created while the manager is in scope, theOITracer returned by register() reads those context attributes and stamps them onto the span as it’s created.
This is why register() matters: it returns an OITracer instead of a vanilla OTel tracer. If you bypass register() and use the raw OTel tracer, these context managers still set context values, but the tracer won’t pick them up automatically.
The Six Helpers
All six live inopeninference.instrumentation:
| Helper | Sets | Use case |
|---|---|---|
using_session | session.id | Group traces into a multi-turn conversation. |
using_user | user.id | Tag spans with the user the request belongs to. |
using_metadata | metadata | Arbitrary JSON-stringified data. |
using_tags | tag.tags | A list of tags for filtering and search. |
using_prompt_template | llm.prompt_template.{template,version,variables} | Attach the prompt template, its version, and the variables used. |
using_attributes | Any combination of the above | Set multiple in one block to avoid nesting. |
using_session
Sets the session.id attribute on every span in scope. This is how Arize AX groups traces into a session.
using_user
Sets the user.id attribute.
using_metadata
Sets arbitrary JSON-stringified metadata. Useful for application-specific context (tenant, environment, request source) that doesn’t fit the other helpers.
using_tags
Sets the tag.tags attribute — a list of strings used for filtering and search in the Arize AX UI.
using_prompt_template
Sets the three prompt-template attributes — llm.prompt_template.template, llm.prompt_template.version, llm.prompt_template.variables — at once.
using_attributes
Sets several of the above in one block — useful when you’d otherwise nest three or four context managers.
Manual Spans and Context Inheritance
Context managers attach attributes to the OTel context. TheOITracer reads those attributes when it creates a span — that’s how auto-instrumented spans (and spans created with the OITracer) automatically inherit them.
Manual spans created with a raw OTel tracer do not automatically pick up these context attributes. If you mix manual spans with context managers, use the helper from openinference.instrumentation:
When Context Managers Help (and When They Don’t)
| Use a context manager when… | Set the attribute on the span directly when… |
|---|---|
| The value applies to many spans (whole conversation, whole request). | The value applies to just this one span. |
| You’re using auto-instrumentation and can’t reach inside library code. | You’re creating the span by hand anyway. |
You want to scope cross-cutting context cleanly with a with block. | The value is computed from per-span data. |