Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

pypi

This library allows tracing LLM requests and logging of messages made by the OpenAI Python API library. It also captures the duration of the operations and the number of tokens used as metrics, and for streaming chat completions the time to the first chunk and the time between output chunks.

Many LLM platforms support the OpenAI SDK. This means systems such as the following are observable with this instrumentation when accessed using it:

OpenAI Compatible Platforms

Name

gen_ai.system

Azure OpenAI

azure.ai.openai

Gemini

gemini

Perplexity

perplexity

xAI (Compatible with Anthropic)

xai

DeepSeek

deepseek

Groq

groq

MistralAI

mistral_ai

Installation

If your application is already instrumented with OpenTelemetry, add this package to your requirements.

pip install opentelemetry-instrumentation-genai-openai

If you don’t have an OpenAI application, yet, try our examples which only need a valid OpenAI API key.

Check out zero-code example for a quick start.

Usage

This section describes how to set up OpenAI instrumentation if you’re setting OpenTelemetry up manually. Check out the manual example for more details.

Instrumenting all clients

When using the instrumentor, all clients will automatically trace OpenAI operations including chat completions, the Responses API, and embeddings. You can also optionally capture prompts and completions as log events.

Fetching a stored response with client.responses.retrieve(...) performs no inference, so it is reported as a fetch_response operation rather than as an inference call. Its span carries the fetched response’s identifier, model, status, and finish reasons, but no token usage — those counts belong to the operation that originally generated the response.

Make sure to configure OpenTelemetry tracing, logging, and events to capture all telemetry emitted by the instrumentation.

from opentelemetry.instrumentation.genai.openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument()

client = OpenAI()
# Chat completion example
response = client.chat.completions.create(
    model="gpt-4o-mini",
    messages=[
        {"role": "user", "content": "Write a short poem on open telemetry."},
    ],
)

# Responses API example, fetching a stored response back by its id
created = client.responses.create(
    model="gpt-4o-mini",
    input="Write a short poem on open telemetry.",
    store=True,
)
fetched = client.responses.retrieve(created.id)

# Embeddings example
embedding_response = client.embeddings.create(
    model="text-embedding-3-small",
    input="Generate vector embeddings for this text"
)

Enabling message content

Message content such as the contents of the prompt, completion, function arguments and return values are not captured by default. To capture message content, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of the following values:

  • span_only - capture content on span attributes.

  • event_only - capture content on event attributes.

  • span_and_event - capture content on both span and event attributes.

  • no_content - do not capture content (the default).

Uploading prompts and completions

To enable the built-in upload hook, set:

  • OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK=upload

  • OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH to an fsspec-compatible URI/path (e.g. /path/to/prompts or gs://my_bucket).

Install the upload extra to pull in fsspec:

pip install opentelemetry-util-genai[upload]

See the opentelemetry-util-genai for additional options.

Enabling the latest experimental features

The latest experimental GenAI semantic conventions are used unconditionally; there is no environment variable to opt in or out.

Uninstrument

To uninstrument clients, call the uninstrument method:

from opentelemetry.instrumentation.genai.openai import OpenAIInstrumentor

OpenAIInstrumentor().instrument()
# ...

# Uninstrument all clients
OpenAIInstrumentor().uninstrument()

References

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

File details

Details for the file opentelemetry_instrumentation_genai_openai-1.1b0.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_genai_openai-1.1b0.tar.gz
Algorithm Hash digest
SHA256 26387b64c56670eeb4376339287812dd95e712c11fdf2994ac86f708c748403a
MD5 d02d33fcb4b266015e9d32f37ad8a728
BLAKE2b-256 5b47eba2a847a0e5b5b385d0a150ffab42ab7fe5aeb02a1e8883f0465ae84a82

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_instrumentation_genai_openai-1.1b0.tar.gz:

Publisher: release-all.yml on open-telemetry/opentelemetry-python-genai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file opentelemetry_instrumentation_genai_openai-1.1b0-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_genai_openai-1.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 057b021840cafb9c11352a3c786d592a9639d59a19b225cc68068db67bb5bf78
MD5 8039861479144b0d74f9f0b8e52ca42a
BLAKE2b-256 9a6f359b5e30c8caa3d25746225c9109db79805bb377bb2dd028355830c6afe3

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_instrumentation_genai_openai-1.1b0-py3-none-any.whl:

Publisher: release-all.yml on open-telemetry/opentelemetry-python-genai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.1b0 This release

2 files

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