Skip to main content

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.

How They Work#

Each context manager attaches a value to the OpenTelemetry context. When a span is created while the manager is in scope, the OpenInference-aware tracer 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 in openinference.instrumentation:

HelperSetsUse case
using_sessionsession.idGroup traces into a multi-turn conversation.
using_useruser.idTag spans with the user the request belongs to.
using_metadatametadataArbitrary JSON-stringified data.
using_tagstag.tagsA list of tags for filtering and search.
using_prompt_templatellm.prompt_template.{template,version,variables}Attach the prompt template, its version, and the variables used.
using_attributesAny combination of the aboveSet multiple in one block to avoid nesting.

using_session#

Sets the session.id attribute on every span in scope. This is how Phoenix groups traces into a session.

For practical session setup, see Set up sessions.

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 Phoenix UI.

using_prompt_template#

Sets the three prompt-template attributes — llm.prompt_template.template, llm.prompt_template.version, llm.prompt_template.variables — at once.

Attaching the template (rather than only the final prompt) makes it possible to evaluate, compare, and re-run prompts directly from the Phoenix UI.

using_attributes#

Sets several of the above in one block — useful when you’d otherwise nest three or four context managers.

For the canonical reference, see Customize your traces.

Manual Spans and Context Inheritance#

Context managers attach attributes to the OTel context. The OITracer 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:

This pulls the current context attributes and applies them to the new span. The @contextmanager decorator (with yield span instead of return span) keeps the span open for the duration of the with block — returning the span directly would end it before the block runs.

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.

Next step#

You’ve covered every approach to setting attributes. Next, the mechanics that keep traces intact across process, thread, and service boundaries — context propagation:

Next: Context Propagation