Context attributes refer to span attributes that can be read from the OTEL Context. Our openinference packages offer very convenient tools to write and read from the OTEL Context. The benefit of this approach is that OpenInference auto instrumentors will attempt to pass these attributes to all other spans underneath a parent trace.
Supported Context Attributes include:
Session ID: Unique identifier for a session.
User ID: identifier for a user.
Metadata: Metadata associated with a span.
Tags: List of tags to give the span a category.
Prompt Template:
Template: Used to generate prompts as Python f-strings.
Version: The version of the prompt template.
Variables: key-value pairs applied to the prompt template.
Python
Install the core instrumentation package via
pip install openinference-instrumentation
using_session
Context manager to add session ID to the current OpenTelemetry Context. OpenInference auto instrumentators will read this Context and pass the session ID as a span attribute, following the OpenInference semantic conventions. Its input, the session ID, must be a non-empty string.
from openinference.instrumentation import using_sessionwithusing_session(session_id="my-session-id"):# Calls within this block will generate spans with the attributes:# "session.id" = "my-session-id" ...
It can also be used as a decorator:
@using_session(session_id="my-session-id")defcall_fn(*args,**kwargs):# Calls within this function will generate spans with the attributes:# "session.id" = "my-session-id" ...
using_user
Context manager to add user ID to the current OpenTelemetry Context. OpenInference auto instrumentators will read this Context and pass the user ID as a span attribute, following the OpenInference semantic conventions. Its input, the user ID, must be a non-empty string.
from openinference.instrumentation import using_userwithusing_user("my-user-id"):# Calls within this block will generate spans with the attributes:# "user.id" = "my-user-id" ...
It can also be used as a decorator:
@using_user("my-user-id")defcall_fn(*args,**kwargs):# Calls within this function will generate spans with the attributes:# "user.id" = "my-user-id" ...
using_metadata
Context manager to add metadata to the current OpenTelemetry Context. OpenInference auto instrumentators will read this Context and pass the metadata as a span attribute, following the OpenInference semantic conventions. Its input, the metadata, must be a dictionary with string keys. This dictionary will be serialized to JSON when saved to the OTEL Context and remain a JSON string when sent as a span attribute.
from openinference.instrumentation import using_metadatametadata ={"key-1": value_1,"key-2": value_2, ...}withusing_metadata(metadata):# Calls within this block will generate spans with the attributes:# "metadata" = "{\"key-1\": value_1, \"key-2\": value_2, ... }" # JSON serialized ...
It can also be used as a decorator:
@using_metadata(metadata)defcall_fn(*args,**kwargs):# Calls within this function will generate spans with the attributes:# "metadata" = "{\"key-1\": value_1, \"key-2\": value_2, ... }" # JSON serialized ...
using_tags
Context manager to add tags to the current OpenTelemetry Context. OpenInference auto instrumentators will read this Context and pass the tags as a span attribute, following the OpenInference semantic conventions. ts input, the tag list, must be a list of strings.
from openinference.instrumentation import using_tagstags = ["tag_1","tag_2", ...]withusing_tags(tags):# Calls within this block will generate spans with the attributes:# "tag.tags" = "["tag_1","tag_2",...]" ...
It can also be used as a decorator:
@using_tags(tags)defcall_fn(*args,**kwargs):# Calls within this function will generate spans with the attributes:# "tag.tags" = "["tag_1","tag_2",...]" ...
using_prompt_template
Context manager to add a prompt template (including its version and variables) to the current OpenTelemetry Context. OpenInference auto instrumentators will read this Context and pass the prompt template fields as span attributes, following the OpenInference semantic conventions. Its inputs must be of the following type:
Template: non-empty string.
Version: non-empty string.
Variables: a dictionary with string keys. This dictionary will be serialized to JSON when saved to the OTEL Context and remain a JSON string when sent as a span attribute.
from openinference.instrumentation import using_prompt_templateprompt_template ="Please describe the weather forecast for {city} on {date}"prompt_template_variables ={"city":"Johannesburg","date":"July 11"}withusing_prompt_template( template=prompt_template, version=prompt_template_variables, variables="v1.0", ):# Calls within this block will generate spans with the attributes:# "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}"# "llm.prompt_template.version" = "v1.0"# "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized ...
It can also be used as a decorator:
@using_prompt_template( template=prompt_template, version=prompt_template_variables, variables="v1.0",)defcall_fn(*args,**kwargs):# Calls within this function will generate spans with the attributes:# "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}"# "llm.prompt_template.version" = "v1.0"# "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized ...
using_attributes
Context manager to add attributes to the current OpenTelemetry Context. OpenInference auto instrumentators will read this Context and pass the attributes fields as span attributes, following the OpenInference semantic conventions. This is a convenient context manager to use if you find yourself using many of the previous ones in conjunction.
from openinference.instrumentation import using_attributestags = ["tag_1","tag_2", ...]metadata ={"key-1": value_1,"key-2": value_2, ...}prompt_template ="Please describe the weather forecast for {city} on {date}"prompt_template_variables ={"city":"Johannesburg","date":"July 11"}prompt_template_version ="v1.0"withusing_attributes( session_id="my-session-id", user_id="my-user-id", metadata=metadata, tags=tags, prompt_template=prompt_template, prompt_template_version=prompt_template_version, prompt_template_variables=prompt_template_variables,):# Calls within this block will generate spans with the attributes:# "session.id" = "my-session-id"# "user.id" = "my-user-id"# "metadata" = "{\"key-1\": value_1, \"key-2\": value_2, ... }" # JSON serialized# "tag.tags" = "["tag_1","tag_2",...]"# "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}"# "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized# "llm.prompt_template.version " = "v1.0" ...
The previous example is equivalent to doing the following, making using_attributes a very convenient tool for the more complex settings.
with (using_session("my-session-id"),using_user("my-user-id"),using_metadata(metadata),using_tags(tags),using_prompt_template( template=prompt_template, version=prompt_template_version, variables=prompt_template_variables, ),):# Calls within this block will generate spans with the attributes:# "session.id" = "my-session-id"# "user.id" = "my-user-id"# "metadata" = "{\"key-1\": value_1, \"key-2\": value_2, ... }" # JSON serialized# "tag.tags" = "["tag_1","tag_2",...]"# "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}"# "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized# "llm.prompt_template.version " = "v1.0" ...
It can also be used as a decorator:
@using_attributes( session_id="my-session-id", user_id="my-user-id", metadata=metadata, tags=tags, prompt_template=prompt_template, prompt_template_version=prompt_template_version, prompt_template_variables=prompt_template_variables,)defcall_fn(*args,**kwargs):# Calls within this function will generate spans with the attributes:# "session.id" = "my-session-id"# "user.id" = "my-user-id"# "metadata" = "{\"key-1\": value_1, \"key-2\": value_2, ... }" # JSON serialized# "tag.tags" = "["tag_1","tag_2",...]"# "llm.prompt_template.template" = "Please describe the weather forecast for {city} on {date}"# "llm.prompt_template.variables" = "{\"city\": \"Johannesburg\", \"date\": \"July 11\"}" # JSON serialized# "llm.prompt_template.version " = "v1.0" ...