The OITracer class wraps an OpenTelemetry tracer with OpenInference-specific functionality, including support for TraceConfig masking and context attribute propagation.
from openinference.semconv.trace import OpenInferenceSpanKindValues# Start a spanwith tracer.start_as_current_span( "my-operation", openinference_span_kind=OpenInferenceSpanKindValues.CHAIN,) as span: span.set_input(value="User question") # ... do work ... span.set_output(value="Response")
OITracer provides decorators for common span types:
from openinference.instrumentation import OITracer# Decorate a function as a chain@tracer.chaindef process_query(query: str) -> str: return query.upper()# With custom name@tracer.chain(name="QueryProcessor")def process_query(query: str) -> str: return query.upper()
from openinference.instrumentation import using_sessionwith using_session("session-123"): # All spans will include session.id = "session-123" response = client.chat.completions.create(...)
from openinference.instrumentation import using_userwith using_user("user-456"): # All spans will include user.id = "user-456" response = client.chat.completions.create(...)
from openinference.instrumentation import using_metadatametadata = { "country": "United States", "topic": "weather", "priority": "high"}with using_metadata(metadata): # All spans will include metadata as JSON response = client.chat.completions.create(...)
from openinference.instrumentation import using_prompt_templatewith using_prompt_template( template="Please describe the weather forecast for {city} on {date}", version="v1.0", variables={"city": "Johannesburg", "date": "July 11"}): # All spans will include prompt template attributes response = client.chat.completions.create(...)