Python APIopeninference-instrumentation

Core

Instrumentation

Python · Core

openinference-instrumentation

OpenInference instrumentation utilities

Module
openinference.instrumentation
Version
0.1.79
Python
>=3.10, <3.16

Install

pip install openinference-instrumentation

openinference.instrumentation

capture_span_context

classSource ↗
class capture_span_context()

Context manager for capturing OpenInference span context.

Examples

with capture_span_context() as capture:
    response = openai_client.chat.completions.create(...)
    phoenix_client.annotations.add_span_annotation(
        span_id=capture.get_last_span_id(),
        annotation_name="feedback",
        ...
    )

get_first_span_id

methodSource ↗
def get_first_span_id() -> Optional[str]

Returns the first captured span ID, or None if no spans were captured. This can be useful if the first span is the one that you want to annotate or evaluate.

get_last_span_id

methodSource ↗
def get_last_span_id() -> Optional[str]

Returns the last captured span ID, or None if no spans were captured. This can be useful if the last span is the one that you want to annotate or evaluate.

get_span_contexts

methodSource ↗
def get_span_contexts() -> Sequence[SpanContext]

Returns a sequence of all captured span contexts.

get_attributes_from_context

functionSource ↗
def get_attributes_from_context() -> Iterator[Tuple[str, AttributeValue]]

Also importable from openinference.instrumentation.context_attributes.

Yield the OpenInference context attributes currently attached to the OpenTelemetry context. OITracer calls this when a span starts to copy them onto the span.

using_attributes

classSource ↗
class using_attributes(
    *,
    session_id: str = '',
    user_id: str = '',
    metadata: Optional[Dict[str, Any]] = None,
    tags: Optional[List[str]] = None,
    prompt_template: str = '',
    prompt_template_version: str = '',
    prompt_template_variables: Optional[Dict[str, Any]] = None,
)

Also importable from openinference.instrumentation.context_attributes.

Context manager to add attributes to the current OpenTelemetry Context. OpenInference instrumentations will read this Context and pass the attributes to the traced span, following the OpenInference semantic conventions.

Also usable as a decorator on plain, async def, generator and async generator functions; the attributes then apply for the whole call.

It is a convenient context manager to use if you find yourself using many others, provided by this package, combined.

Example

tags = [
    "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"
with 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,
):
    # Tracing within this block will include the span attribute:
    # "session.id" = "my-session-id"
    # "user.id" = "my-user-id"
    # "metadata" = "{"key-1": value_1, "key-2": value_2, ... }"
    # "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"}"
    # "llm.prompt_template.version " = "v1.0"



    ...

The previous example is equivalent to doing

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,
    ),
):
    ...

attach_context

methodSource ↗
def attach_context() -> None

__call__

methodSource ↗
def __call__(decorated_function: DecoratedCallable) -> DecoratedCallable

using_metadata

classSource ↗
class using_metadata(metadata: Dict[str, Any])

Also importable from openinference.instrumentation.context_attributes.

Context manager to add metadata to the current OpenTelemetry Context. OpenInference instrumentations will read this Context and pass the metadata as a span attribute, following the OpenInference semantic conventions.

Also usable as a decorator on plain, async def, generator and async generator functions; the attributes then apply for the whole call.

Examples

metadata = {
    "key-1": value_1,
    "key-2": value_2,
    ...
}
with using_metadata(metadata):
    # Tracing within this block will include the span attribute:
    # "metadata" = "{"key-1": value_1, "key-2": value_2, ... }"
    ...

attach_context

methodSource ↗
def attach_context() -> None

__call__

methodSource ↗
def __call__(decorated_function: DecoratedCallable) -> DecoratedCallable

using_prompt_template

classSource ↗
class using_prompt_template(
    *,
    template: str = '',
    version: str = '',
    variables: Optional[Dict[str, Any]] = None,
)

Also importable from openinference.instrumentation.context_attributes.

Context manager to add prompt template, with its version and variables a to the current OpenTelemetry Context. OpenInference instrumentations will read this Context and pass the prompt template as a span attribute, following the OpenInference semantic conventions.

Also usable as a decorator on plain, async def, generator and async generator functions; the attributes then apply for the whole call.

Examples

prompt_template = "Please describe the weather forecast for {city} on {date}"
prompt_template_variables = {"city": "Johannesburg", date:"July 11"}
with using_prompt_template(
    template=prompt_template,
    version="v1.0",
    variables=prompt_template_variables,
):
    # Tracing within this block will include the span attribute:
    # "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"}"
    ...

attach_context

methodSource ↗
def attach_context() -> None

__call__

methodSource ↗
def __call__(decorated_function: DecoratedCallable) -> DecoratedCallable

using_session

classSource ↗
class using_session(session_id: str)

Also importable from openinference.instrumentation.context_attributes.

Context manager to add session id to the current OpenTelemetry Context. OpenInference instrumentations will read this Context and pass the session id as a span attribute, following the OpenInference semantic conventions.

Also usable as a decorator on plain, async def, generator and async generator functions; the attributes then apply for the whole call.

Examples

with using_session("my-session-id"):
    # Tracing within this block will include the span attribute:
    # "session.id" = "my-session-id"
    ...

attach_context

methodSource ↗
def attach_context() -> None

__call__

methodSource ↗
def __call__(decorated_function: DecoratedCallable) -> DecoratedCallable

using_tags

classSource ↗
class using_tags(tags: List[str])

Also importable from openinference.instrumentation.context_attributes.

Context manager to add tags to the current OpenTelemetry Context. OpenInference instrumentations will read this Context and pass the tags as a span attribute, following the OpenInference semantic conventions.

Also usable as a decorator on plain, async def, generator and async generator functions; the attributes then apply for the whole call.

Examples

tags = [
    "tag_1",
    "tag_2",
    ...
]
with using_tags(tags):
    # Tracing within this block will include the span attribute:
    # "tag.tags" = "["tag_1","tag_2",...]"
    ...

attach_context

methodSource ↗
def attach_context() -> None

__call__

methodSource ↗
def __call__(decorated_function: DecoratedCallable) -> DecoratedCallable

using_user

classSource ↗
class using_user(user_id: str)

Also importable from openinference.instrumentation.context_attributes.

Context manager to add user id to the current OpenTelemetry Context. OpenInference instrumentations will read this Context and pass the user id as a span attribute, following the OpenInference semantic conventions.

Also usable as a decorator on plain, async def, generator and async generator functions; the attributes then apply for the whole call.

Examples

with using_user("my-user-id"):
    # Tracing within this block will include the span attribute:
    # "user.id" = "my-user-id"
    ...

attach_context

methodSource ↗
def attach_context() -> None

__call__

methodSource ↗
def __call__(decorated_function: DecoratedCallable) -> DecoratedCallable

dangerously_using_project

classSource ↗
class dangerously_using_project(project_name: str)

A context manager that switches the project for all spans created within the context.

This is intended for use in notebook environments where it’s useful to be able to change the project associated with spans on the fly.

Note: This should not be used in production environments or complex OpenTelemetry setups. As dynamically modifying span resources in this way can lead to unexpected behavior.

Parameters

NameTypeDescription
project_namestr

The project name to associate with spans created within the context.

Examples:

python with dangerously_using_project('my_project'): # Spans created here will be associated with 'my_project'

Attributes

NameType and value
project_nameproject_name

safe_json_dumps

functionSource ↗
def safe_json_dumps(obj: Any, **kwargs: Any = {}) -> str

Also importable from openinference.instrumentation.helpers.

A convenience wrapper around json.dumps that ensures that any object can be safely encoded without a TypeError and that non-ASCII Unicode characters are not escaped.

suppress_tracing

classSource ↗
class suppress_tracing

Also importable from openinference.instrumentation.config.

Context manager to pause OpenTelemetry instrumentation.

Examples

with suppress_tracing():
    # No tracing will occur within this block
    ...

async with suppress_tracing():
    # No tracing will occur within this block
    ...

TraceConfig

dataclassSource ↗
class TraceConfig(
    hide_llm_invocation_parameters: Optional[bool] = None,
    hide_llm_tools: Optional[bool] = None,
    hide_inputs: Optional[bool] = None,
    hide_outputs: Optional[bool] = None,
    hide_input_messages: Optional[bool] = None,
    hide_output_messages: Optional[bool] = None,
    hide_input_images: Optional[bool] = None,
    hide_input_text: Optional[bool] = None,
    hide_output_text: Optional[bool] = None,
    hide_embedding_vectors: Optional[bool] = None,
    hide_embeddings_vectors: Optional[bool] = None,
    hide_embeddings_text: Optional[bool] = None,
    hide_prompts: Optional[bool] = None,
    hide_choices: Optional[bool] = None,
    hide_retrieval_documents: Optional[bool] = None,
    enable_genai_semconv: Optional[bool] = None,
    base64_image_max_length: Optional[int] = None,
    blob_uploader: Optional[BlobUploader] = None,
)

Also importable from openinference.instrumentation.config.

TraceConfig helps you modify the observability level of your tracing. For instance, you may want to keep sensitive information from being logged for security reasons, or you may want to limit the size of the base64 encoded images to reduce payloads.

For those attributes not passed, this object tries to read from designated environment variables and, if not found, has default values that maximize observability.

Attributes

NameType and value
hide_llm_invocation_parametersOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_LLM_INVOCATION_PARAMETERS, 'default_value': DEFAULT_HIDE_...
hide_llm_toolsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_LLM_TOOLS, 'default_value': DEFAULT_HIDE_LLM_TOOLS})

Hides the tool definitions advertised to the LLM

hide_inputsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_INPUTS, 'default_value': DEFAULT_HIDE_INPUTS})

Hides input value, input images, input documents & messages

hide_outputsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_OUTPUTS, 'default_value': DEFAULT_HIDE_OUTPUTS})

Hides output value, output images, output documents & messages

hide_input_messagesOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_INPUT_MESSAGES, 'default_value': DEFAULT_HIDE_INPUT_MESSA...

Hides all input messages

hide_output_messagesOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_OUTPUT_MESSAGES, 'default_value': DEFAULT_HIDE_OUTPUT_MES...

Hides all output messages

hide_input_imagesOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_INPUT_IMAGES, 'default_value': DEFAULT_HIDE_INPUT_IMAGES})

Hides images from input messages and the span-level input.images

hide_input_textOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_INPUT_TEXT, 'default_value': DEFAULT_HIDE_INPUT_TEXT})

Hides text from input messages, including message document content

hide_output_textOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_OUTPUT_TEXT, 'default_value': DEFAULT_HIDE_OUTPUT_TEXT})

Hides text from output messages, including message document content

hide_embedding_vectorsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_EMBEDDING_VECTORS, 'default_value': DEFAULT_HIDE_EMBEDDIN...

Deprecated: use hide_embeddings_vectors instead

hide_embeddings_vectorsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_EMBEDDINGS_VECTORS, 'default_value': DEFAULT_HIDE_EMBEDDI...

Hides embedding vectors

hide_embeddings_textOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_EMBEDDINGS_TEXT, 'default_value': DEFAULT_HIDE_EMBEDDINGS...

Hides embedding text

hide_promptsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_PROMPTS, 'default_value': DEFAULT_HIDE_PROMPTS})

Hides LLM prompts (completions API)

hide_choicesOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_CHOICES, 'default_value': DEFAULT_HIDE_CHOICES})

Hides LLM choices (completions API outputs)

hide_retrieval_documentsOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_HIDE_RETRIEVAL_DOCUMENTS, 'default_value': DEFAULT_HIDE_RETRIE...

Redacts the content and metadata of documents on RETRIEVER spans (retrieval.documents.*.document.content / retrieval.documents.*.document.metadata). hide_outputs implies this, mirroring how it already redacts reranker.output_documents.*.

enable_genai_semconvOptional[bool] = field(default=None, metadata={'env_var': OPENINFERENCE_ENABLE_GENAI_SEMCONV, 'default_value': DEFAULT_ENABLE_GENAI_SE...

Emits OTel GenAI semantic conventions alongside OpenInference attributes

base64_image_max_lengthOptional[int] = field(default=None, metadata={'env_var': OPENINFERENCE_BASE64_IMAGE_MAX_LENGTH, 'default_value': DEFAULT_BASE64_IMAGE...

Limits characters of a base64 data URI in an image or document url

blob_uploaderOptional[BlobUploader] = field(default=None, metadata={'env_var': None, 'default_value': None})

Uploads base64 images and documents exceeding base64_image_max_length to external storage and records the destination URI in the span attribute instead of redacting. OpenInference ships no implementation — pass any object satisfying the BlobUploader protocol, or set the OPENINFERENCE_BLOB_UPLOADER environment variable to the name of an uploader registered under the “openinference_blob_uploader” entry-point group.

mask

methodSource ↗
def mask(
    key: str,
    value: Union[AttributeValue, Callable[[], AttributeValue]],
    *,
    externalize: bool = True,
) -> Optional[AttributeValue]

OITracer

classSource ↗
class OITracer(wrapped: Tracer, config: TraceConfig)

Attributes

NameType and value
id_generatorIdGenerator

start_as_current_span

methodSource ↗
def start_as_current_span(
    name: str,
    context: Optional[Context] = None,
    kind: SpanKind = SpanKind.INTERNAL,
    attributes: Attributes = None,
    links: Optional[Sequence[Link]] = (),
    start_time: Optional[int] = None,
    record_exception: bool = True,
    set_status_on_exception: bool = True,
    end_on_exit: bool = True,
    *,
    openinference_span_kind: Optional[OpenInferenceSpanKind] = None,
) -> Iterator[OpenInferenceSpan]

start_span

methodSource ↗
def start_span(
    name: str,
    context: Optional[Context] = None,
    kind: SpanKind = SpanKind.INTERNAL,
    attributes: Attributes = None,
    links: Optional[Sequence[Link]] = (),
    start_time: Optional[int] = None,
    record_exception: bool = True,
    set_status_on_exception: bool = True,
    *,
    openinference_span_kind: Optional[OpenInferenceSpanKind] = None,
) -> OpenInferenceSpan

agent

methodSource ↗
def agent(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

chain

methodSource ↗
def chain(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

retriever

methodSource ↗
def retriever(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

reranker

methodSource ↗
def reranker(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

guardrail

methodSource ↗
def guardrail(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

evaluator

methodSource ↗
def evaluator(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

tool

methodSource ↗
def tool(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
    description: Optional[str] = None,
    parameters: Optional[Union[str, Dict[str, Any]]] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

llm

methodSource ↗
def llm(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
    process_input: Optional[Callable[ParametersType, Mapping[str, AttributeValue]]] = None,
    process_output: Optional[Callable[..., Mapping[str, AttributeValue]]] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

decision

methodSource ↗
def decision(
    wrapped_function: Optional[Callable[ParametersType, ReturnType]] = None,
    /,
    *,
    name: Optional[str] = None,
    process_input: Optional[Callable[ParametersType, Mapping[str, AttributeValue]]] = None,
    process_output: Optional[Callable[..., Mapping[str, AttributeValue]]] = None,
) -> Union[Callable[ParametersType, ReturnType], Callable[[Callable[ParametersType, ReturnType]], Callable[ParametersType, ReturnType]]]

Trace a decision model call as a DECISION span.

A decision model scores or selects among candidate options supplied in the request rather than generating text. The decorator behaves exactly like llm: it captures the arguments as input.value and the return value as output.value unless process_input / process_output are given, and supports sync, async, generator and async generator functions. Use get_decision_attributes in those callbacks to record the decision.* model identification and token counts.

REDACTED_VALUE

attributeSource ↗
REDACTED_VALUE = '__REDACTED__'

Also importable from openinference.instrumentation.config.

Blob

dataclassSource ↗
class Blob(
    data: bytes,
    mime_type: str,
    modality: str = '',
    attribute_key: Optional[str] = None,
    content_sha256: str = '',
)

A unit of binary content captured during instrumentation, e.g. the decoded payload of a base64 data URI found in a span attribute.

Attributes

NameType and value
databytes
mime_typestr
modalitystr = ''

One of “image”, “audio”, “video”, or “document”. Derived from the mime type when not provided.

attribute_keyOptional[str] = None

The span attribute key the content was captured from, if any.

content_sha256str = field(default='')

Hex digest of the decoded bytes. Computed automatically.

BlobUploader

classSource ↗
class BlobUploader(Protocol)

Uploads blobs to external storage, returning a reference URI that is recorded in span attributes in place of inline base64 content.

upload

methodSource ↗
def upload(blob: Blob) -> Optional[str]

Returns the destination URI immediately and performs the actual write asynchronously, or returns None if the blob cannot be accepted (the caller then falls back to redaction). Must not block the instrumented call path.

shutdown

methodSource ↗
def shutdown(timeout_sec: float = 10.0) -> None

Flushes pending uploads and stops background workers.

decode_base64_data_uri_to_blob

functionSource ↗
def decode_base64_data_uri_to_blob(
    url: str,
    attribute_key: Optional[str] = None,
) -> Optional[Blob]

Decodes a base64 data URI into a Blob, or returns None if the value is not a base64 data URI or the payload cannot be decoded.

load_blob_uploader

functionSource ↗
def load_blob_uploader(name: str) -> Optional[BlobUploader]

Loads a BlobUploader registered under the openinference_blob_uploader entry-point group (mirroring OTel util-genai’s opentelemetry_genai_completion_hook mechanics). The entry point may resolve to an uploader instance or to a zero-argument callable (e.g. a class) producing one. Returns None — and logs — when the name is unknown or the loaded object does not satisfy the protocol.

Loads are memoized per name: every TraceConfig constructed in the process shares one uploader instance (one worker pool, one queue), rather than each instrumentor spawning its own.

A package providing an uploader registers it in its packaging metadata:

python [project.entry-points.openinference_blob_uploader] arize = "arize_otel.blob:ArizeBlobUploader"

and users select it with OPENINFERENCE_BLOB_UPLOADER=arize — no application code required.

parse_base64_data_uri

functionSource ↗
def parse_base64_data_uri(url: str) -> Optional[Tuple[str, str]]

Parses a data:<mime>;base64,<payload> URI of any media type. Returns (mime_type, base64_payload), or None if the value is not a base64 data URI.

TracerProvider

classSource ↗
class TracerProvider(
    *args: Any = (),
    config: Optional[TraceConfig] = None,
    **kwargs: Any = {},
)

get_tracer

methodSource ↗
def get_tracer(*args: Any = (), **kwargs: Any = {}) -> OITracer

get_context_attributes

functionSource ↗
def get_context_attributes(
    *,
    session_id: Optional[str] = None,
    user_id: Optional[str] = None,
    metadata: Optional[Union[str, Dict[str, Any]]] = None,
    tags: Optional[List[str]] = None,
) -> Dict[str, AttributeValue]

get_annotation_attributes

functionSource ↗
def get_annotation_attributes(
    *,
    annotations: Sequence[Annotation],
    scope: AnnotationScope = 'span',
) -> Dict[str, AttributeValue]

Return flattened OpenInference attributes for annotations.

Collection indices are assigned in input order. Metadata dictionaries are JSON-serialized, while metadata strings are preserved as provided.

get_decision_attributes

functionSource ↗
def get_decision_attributes(
    *,
    provider: Optional[OpenInferenceDecisionProvider] = None,
    system: Optional[OpenInferenceDecisionSystem] = None,
    model_name: Optional[str] = None,
    request_model_name: Optional[str] = None,
    response_model_name: Optional[str] = None,
    token_count: Optional[DecisionTokenCount] = None,
) -> Dict[str, AttributeValue]

Build OpenInference DECISION span attributes.

A decision model scores or selects among candidate options supplied in the request rather than generating text, so decision spans identify the model under decision.* instead of llm.*. The arguments mirror get_llm_attributes and follow the same rules: system names the decision API ecosystem the call conforms to, provider names who hosts the model, and decision.model_name is model_name, else response_model_name, else request_model_name. decision.request.model_name and decision.response.model_name are set only when those arguments are passed.

Decision spans have no input or output messages; record the raw request and response with span.set_input / span.set_output.

get_decision_model_name_attributes

functionSource ↗
def get_decision_model_name_attributes(
    model_name: Optional[str],
) -> Mapping[str, AttributeValue]

get_decision_provider_attributes

functionSource ↗
def get_decision_provider_attributes(
    provider: Optional[OpenInferenceDecisionProvider],
) -> Mapping[str, AttributeValue]

get_decision_request_model_name_attributes

functionSource ↗
def get_decision_request_model_name_attributes(
    request_model_name: Optional[str],
) -> Mapping[str, AttributeValue]

get_decision_response_model_name_attributes

functionSource ↗
def get_decision_response_model_name_attributes(
    response_model_name: Optional[str],
) -> Mapping[str, AttributeValue]

get_decision_system_attributes

functionSource ↗
def get_decision_system_attributes(
    system: Optional[OpenInferenceDecisionSystem],
) -> Mapping[str, AttributeValue]

get_decision_token_count_attributes

functionSource ↗
def get_decision_token_count_attributes(
    token_count: Optional[DecisionTokenCount],
) -> Mapping[str, AttributeValue]

get_embedding_attributes

functionSource ↗
def get_embedding_attributes(
    *,
    model_name: Optional[str] = None,
    embeddings: Optional[List[Embedding]] = None,
) -> Dict[str, AttributeValue]

get_evaluation_attributes

functionSource ↗
def get_evaluation_attributes(
    *,
    evaluations: Sequence[Annotation],
    scope: AnnotationScope = 'span',
) -> Dict[str, AttributeValue]

Return annotations encoded using evaluation terminology.

Collection indices are assigned in input order. Metadata dictionaries are JSON-serialized, while metadata strings are preserved as provided.

get_input_attributes

functionSource ↗
def get_input_attributes(
    value: Any,
    *,
    mime_type: Optional[OpenInferenceMimeType] = None,
) -> Dict[str, AttributeValue]

get_llm_attributes

functionSource ↗
def get_llm_attributes(
    *,
    provider: Optional[OpenInferenceLLMProvider] = None,
    system: Optional[OpenInferenceLLMSystem] = None,
    model_name: Optional[str] = None,
    request_model_name: Optional[str] = None,
    response_model_name: Optional[str] = None,
    invocation_parameters: Optional[Union[str, Dict[str, Any]]] = None,
    input_messages: Optional[Sequence[Message]] = None,
    output_messages: Optional[Sequence[Message]] = None,
    token_count: Optional[TokenCount] = None,
    tools: Optional[Sequence[Tool]] = None,
) -> Dict[str, AttributeValue]

Build OpenInference LLM span attributes.

llm.model_name is model_name, else response_model_name, else request_model_name. llm.request.model_name and llm.response.model_name are set only when those arguments are passed.

get_llm_input_message_attributes

functionSource ↗
def get_llm_input_message_attributes(
    messages: Optional[Sequence[Message]],
) -> Mapping[str, AttributeValue]

get_llm_invocation_parameter_attributes

functionSource ↗
def get_llm_invocation_parameter_attributes(
    invocation_parameters: Optional[Union[str, Dict[str, Any]]],
) -> Mapping[str, AttributeValue]

get_llm_model_name_attributes

functionSource ↗
def get_llm_model_name_attributes(
    model_name: Optional[str],
) -> Mapping[str, AttributeValue]

get_llm_output_message_attributes

functionSource ↗
def get_llm_output_message_attributes(
    messages: Optional[Sequence[Message]],
) -> Mapping[str, AttributeValue]

get_llm_provider_attributes

functionSource ↗
def get_llm_provider_attributes(
    provider: Optional[OpenInferenceLLMProvider],
) -> Mapping[str, AttributeValue]

get_llm_request_model_name_attributes

functionSource ↗
def get_llm_request_model_name_attributes(
    request_model_name: Optional[str],
) -> Mapping[str, AttributeValue]

get_llm_response_model_name_attributes

functionSource ↗
def get_llm_response_model_name_attributes(
    response_model_name: Optional[str],
) -> Mapping[str, AttributeValue]

get_llm_system_attributes

functionSource ↗
def get_llm_system_attributes(
    system: Optional[OpenInferenceLLMSystem],
) -> Mapping[str, AttributeValue]

get_llm_token_count_attributes

functionSource ↗
def get_llm_token_count_attributes(
    token_count: Optional[TokenCount],
) -> Mapping[str, AttributeValue]

get_llm_tool_attributes

functionSource ↗
def get_llm_tool_attributes(
    tools: Optional[Sequence[Tool]],
) -> Mapping[str, AttributeValue]

get_metadata_attributes

functionSource ↗
def get_metadata_attributes(
    *,
    metadata: Union[str, Dict[str, Any]],
) -> Dict[str, AttributeValue]

get_output_attributes

functionSource ↗
def get_output_attributes(
    value: Any,
    *,
    mime_type: Optional[OpenInferenceMimeType] = None,
) -> Dict[str, AttributeValue]

get_reranker_attributes

functionSource ↗
def get_reranker_attributes(
    *,
    query: Optional[str] = None,
    model_name: Optional[str] = None,
    input_documents: Optional[List[Document]] = None,
    output_documents: Optional[List[Document]] = None,
    top_k: Optional[int] = None,
) -> Dict[str, AttributeValue]

get_retriever_attributes

functionSource ↗
def get_retriever_attributes(
    *,
    documents: List[Document],
) -> Dict[str, AttributeValue]

get_session_attributes

functionSource ↗
def get_session_attributes(
    *,
    session_id: str,
) -> Dict[str, AttributeValue]

get_span_kind_attributes

functionSource ↗
def get_span_kind_attributes(
    kind: OpenInferenceSpanKind,
    /,
) -> Dict[str, AttributeValue]

get_tag_attributes

functionSource ↗
def get_tag_attributes(*, tags: List[str]) -> Dict[str, AttributeValue]

get_tool_attributes

functionSource ↗
def get_tool_attributes(
    *,
    name: str,
    description: Optional[str] = None,
    parameters: Union[str, Dict[str, Any]],
) -> Dict[str, AttributeValue]

get_user_id_attributes

functionSource ↗
def get_user_id_attributes(*, user_id: str) -> Dict[str, AttributeValue]

infer_llm_provider_from_host

functionSource ↗
def infer_llm_provider_from_host(
    host: str,
) -> Optional[OpenInferenceLLMProviderValues]

Return the LLM provider name for the given API hostname.

infer_llm_system_from_model_name

functionSource ↗
def infer_llm_system_from_model_name(
    model_name: str,
) -> Optional[OpenInferenceLLMSystemValues]

Return the LLM system name for the given model identifier.

Annotation

classSource ↗
class Annotation(TypedDict)

A single annotation or evaluation result.

Attributes

NameType and value
nameRequired[str]
scoreUnion[int, float]
labelstr
explanationstr
annotator_kindstr
identifierstr
metadataUnion[str, Dict[str, Any]]

AnnotationScope

attributeSource ↗
AnnotationScope: TypeAlias = Literal['span', 'trace', 'session']

DecisionTokenCount

classSource ↗
class DecisionTokenCount(TypedDict)

Token usage of a decision model call.

Decision models have no prompt/completion split: input counts the tokens sent (state, questions, and candidate options) and output the tokens in the typed answers. There is no total; it is the sum when both are present.

Attributes

NameType and value
inputint
outputint

Document

classSource ↗
class Document(TypedDict)

Attributes

NameType and value
contentstr
idUnion[str, int]
metadataUnion[str, Dict[str, Any]]
scorefloat

Embedding

classSource ↗
class Embedding(TypedDict)

Attributes

NameType and value
textstr
vectorList[float]

Image

classSource ↗
class Image(TypedDict)

Attributes

NameType and value
urlstr

ImageMessageContent

classSource ↗
class ImageMessageContent(TypedDict)

Attributes

NameType and value
typeLiteral['image']
imageImage

Message

classSource ↗
class Message(TypedDict)

Attributes

NameType and value
rolestr
contentstr
contentsSequence[MessageContent]
tool_call_idstr
tool_callsSequence[ToolCall]

MessageContent

attributeSource ↗
MessageContent: TypeAlias = Union[TextMessageContent, ImageMessageContent, ReasoningMessageContent]

PromptDetails

classSource ↗
class PromptDetails(TypedDict)

Attributes

NameType and value
audioint
cache_readint
cache_writeint

ReasoningMessageContent

classSource ↗
class ReasoningMessageContent(TypedDict)

Attributes

NameType and value
typeRequired[Literal['reasoning']]
textstr
signaturestr
datastr
encrypted_contentstr

TextMessageContent

classSource ↗
class TextMessageContent(TypedDict)

Attributes

NameType and value
typeRequired[Literal['text']]
textRequired[str]
signaturestr

TokenCount

classSource ↗
class TokenCount(TypedDict)

Attributes

Tool

classSource ↗
class Tool(TypedDict)

Attributes

NameType and value
json_schemaRequired[Union[str, Dict[str, Any]]]
namestr
descriptionstr

ToolCall

classSource ↗
class ToolCall(TypedDict)

Attributes

ToolCallFunction

classSource ↗
class ToolCallFunction(TypedDict)

Attributes

NameType and value
namestr
argumentsUnion[str, Dict[str, Any]]

openinference.instrumentation.logging

CustomFormatter

classSource ↗
class CustomFormatter(logging.Formatter)

format

methodSource ↗
def format(record: LogRecord) -> str

openinference.instrumentation.helpers

get_span_id

functionSource ↗
def get_span_id(span: Span) -> str

get_trace_id

functionSource ↗
def get_trace_id(span: Span) -> str

openinference.instrumentation.config

mask_without_externalization

functionSource ↗
def mask_without_externalization(
    config: TraceConfig,
    key: str,
    value: Union[AttributeValue, Callable[[], AttributeValue]],
) -> Optional[AttributeValue]

Applies config.mask as a pure view — no blob-upload side effects. Falls back to the two-argument form for TraceConfig subclasses that override mask without the externalize keyword.

is_base64_url

functionSource ↗
def is_base64_url(url: str) -> bool