Skip to main content

OpenInference Instrumentation

PyPI Version

Utility functions for OpenInference instrumentation.

Installation

pip install openinference-instrumentation

Annotation and Evaluation Attributes

Use get_annotation_attributes and get_evaluation_attributes to turn typed Annotation objects into flattened OpenInference span attributes. Both helpers support "span" (the default), "trace", and "session" scopes and assign contiguous collection indices in input order.

from openinference.instrumentation import (
    Annotation,
    get_annotation_attributes,
    get_evaluation_attributes,
)

span_annotations = get_annotation_attributes(
    annotations=[
        Annotation(
            name="hallucination",
            label="factual",
            explanation="Every claim is supported by the retrieved documents.",
            annotator_kind="LLM",
            identifier="judge-v2",
            metadata={"rubric_version": 2},
        )
    ]
)

trace_evaluations = get_evaluation_attributes(
    evaluations=[Annotation(name="correctness", score=0.95)],
    scope="trace",
)

span.set_attributes({**span_annotations, **trace_evaluations})

Annotation has these fields:

  • name (required): criterion or metric name.
  • At least one of score, label, or explanation (required by the helpers).
  • annotator_kind: conventionally "HUMAN", "LLM", or "CODE"; custom values are allowed.
  • identifier: stable producer-assigned result identifier.
  • metadata: a dictionary to JSON-serialize, or an already serialized JSON object string.

The evaluation helper uses the same Annotation model because evaluation is an alternative attribute terminology for annotations. It emits evaluations.* instead of annotations.*. Trace and session scopes add the corresponding trace. or session. prefix. Session-scoped annotations also require the carrying span to have session.id; post-hoc span and trace annotations require the target Span Link described in the annotation specification.

Customizing Spans

The openinference-instrumentation package offers utilities to track important application metadata such as sessions and metadata using Python context managers:

  • using_session: to specify a session ID to track and group a multi-turn conversation with a user
  • using_user: to specify a user ID to track different conversations with a given user
  • using_metadata: to add custom metadata, that can provide extra information that supports a wide range of operational needs
  • using_tag: to add tags, to help filter on specific keywords
  • using_prompt_template: to reflect the prompt template used, with its version and variables. This is useful for prompt template management
  • using_attributes: it helps handling multiple of the previous options at once in a concise manner

For example:

from openinference.instrumentation import using_attributes
tags = ["business_critical", "simple", ...]
metadata = {
    "country": "United States",
    "topic":"weather",
    ...
}
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,
):
    # 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"
    ...

Each helper also works as a decorator. The attributes stay attached for the whole call, including across await suspension points of async def functions and while the body of a generator or async def generator runs (without leaking into the code that consumes it):

from openinference.instrumentation import using_session, using_user

@using_session("my-session-id")
@using_user("my-user-id")
async def answer(question: str) -> str:
    # Spans created here, and by any awaited instrumented call, carry
    # "session.id" = "my-session-id" and "user.id" = "my-user-id"
    ...

When combining them with span-creating decorators such as tracer.agent or tracer.tool, put the using_* decorators on top: the attributes are copied onto a span when it starts, so they have to be attached before the span-creating decorator runs.

See examples/async_context_attribute_decorators.py for a runnable example that exports to a local Phoenix server.

You can read more about this in our docs.

Suppressing Tracing

suppress_tracing turns off span creation for everything inside its block. It works as a regular context manager and as an async one:

from openinference.instrumentation import suppress_tracing

with suppress_tracing():
    ...  # no spans are created here

async with suppress_tracing():
    ...  # nor here, including across awaits

See examples/suppress_tracing_sync_async.py for a runnable example that exports to a local Phoenix server and shows both forms.

Tracing Configuration

This package contains the central TraceConfig class, which lets you specify a tracing configuration that lets you control settings like data privacy and payload sizes. 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 logged to reduced payload size.

In addition, you an also use environment variables, read more here. The following is an example of using the TraceConfig object:

from openinference.instrumentation import TraceConfig

config = TraceConfig(
    hide_inputs=hide_inputs,
    hide_outputs=hide_outputs,
    hide_input_messages=hide_input_messages,
    hide_output_messages=hide_output_messages,
    hide_input_images=hide_input_images,
    hide_input_text=hide_input_text,
    hide_output_text=hide_output_text,
    base64_image_max_length=base64_image_max_length,
)
tracer_provider = ...
# This example uses the OpenAIInstrumentor, but it works with any of our auto instrumentors
OpenAIInstrumentor().instrument(tracer_provider=tracer_provider, config=config)

Overriding the configuration for a scope

using_trace_config hides more content for spans started within its scope, for example for some requests only. It works as a context manager or a decorator, for sync and async code.

A scope can only add masking. It accepts the hide_* fields, set to True, and raises TypeError for False or any other field. Content that the instrumentor's config (or an OPENINFERENCE_HIDE_* environment variable) hides stays hidden inside every scope, and every other field keeps the instrumentor's value. Nested scopes combine their hidden fields, so an inner scope can't unhide what an outer scope hides.

from openinference.instrumentation import using_trace_config

with using_trace_config(hide_inputs=True, hide_outputs=True):
    client.chat.completions.create(...)

@using_trace_config(hide_inputs=True)
async def handle_sensitive_request(...):
    ...

The config is resolved when a span starts and applies to everything recorded on that span, so streamed responses that finish after the block exits are still covered.

The override is stored in the OpenTelemetry context. Threads started inside the block, such as ThreadPoolExecutor workers and loop.run_in_executor calls, don't inherit it, and spans created there use the instrumentor's config. asyncio.create_task and asyncio.to_thread copy the context, so they do inherit it. Propagate the context explicitly when you need the override in a thread:

import contextvars

with using_trace_config(hide_inputs=True):
    executor.submit(contextvars.copy_context().run, call_llm, prompt)

Instrumentation authors: call get_current_trace_config(config) when a span starts and use the result for every content decision on that span, instead of the config passed to instrument().

Metadata

Release files for openinference-instrumentation 0.1.84

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for openinference-instrumentation 0.1.84
File Size Uploaded Provenance Actions
openinference_instrumentation-0.1.84.tar.gz 50.6 kB With provenance Details for openinference_instrumentation-0.1.84.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for openinference-instrumentation 0.1.84
File Interpreter ABI Platform Actions
openinference_instrumentation-0.1.84-py3-none-any.whl Python 3 none any Details for openinference_instrumentation-0.1.84-py3-none-any.whl

Total release size: 108.9 kB

Release files / openinference_instrumentation-0.1.84.tar.gz

Download URL openinference_instrumentation-0.1.84.tar.gz
Size 50.6 kB
Tags Source
SHA-256 checksum
How to use checksums
427f82a5b59702d684786dca0f24e4fd7baf6d273b4f18d7991dfa921bc77890
BLAKE2b-256 checksum
How to use checksums
ba246f9e08124e81825a93b58f5953439834e97640cd650aa65c2a7255dd1651
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

Release files / openinference_instrumentation-0.1.84-py3-none-any.whl

Download URL openinference_instrumentation-0.1.84-py3-none-any.whl
Size 58.3 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9f721eb6caf3473769fb223f3702042c3212e389c2cb51c54161a37026f264fe
BLAKE2b-256 checksum
How to use checksums
30e701c05ae25bd566a4b743e2902d2a54c590a51c5f3fe1d4d29bcdd7a063b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

Security for openinference-instrumentation 0.1.84

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for openinference_instrumentation-0.1.84.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Dec 3, 2026.

Transparency log

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

Release history Release notifications | RSS feed

This release

0.1.84 This release

2 release files Full provenance

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page