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.

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-openai-v2

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 and embeddings. You can also optionally capture prompts and completions as log events.

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

from opentelemetry.instrumentation.openai_v2 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."},
    ],
)

# 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 as log events, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of the following values:

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

To enable the latest experimental features, set the environment variable OTEL_SEMCONV_STABILITY_OPT_IN to gen_ai_latest_experimental. Or, if you use OTEL_SEMCONV_STABILITY_OPT_IN to enable other features, append ,gen_ai_latest_experimental to its value.

Without this setting, OpenAI instrumentation aligns with Semantic Conventions v1.30.0 and would not capture additional details introduced in later versions.

Uninstrument

To uninstrument clients, call the uninstrument method:

from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor

OpenAIInstrumentor().instrument()
# ...

# Uninstrument all clients
OpenAIInstrumentor().uninstrument()

References

Release files for opentelemetry-instrumentation-openai-v2 2.4b0

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

Source distribution (sdist)

Source distribution for opentelemetry-instrumentation-openai-v2 2.4b0
File Size Uploaded
opentelemetry_instrumentation_openai_v2-2.4b0.tar.gz 190.7 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for opentelemetry-instrumentation-openai-v2 2.4b0
File Interpreter ABI Platform
opentelemetry_instrumentation_openai_v2-2.4b0-py3-none-any.whl Python 3 none any Details

Total release size: 218.8 kB

Release files / opentelemetry_instrumentation_openai_v2-2.4b0.tar.gz

Download URL opentelemetry_instrumentation_openai_v2-2.4b0.tar.gz
Size 190.7 kB
Tags Source
SHA-256 checksum
How to use checksums
571a2febd05b15808d7c777455d5a74c9abe08c694cb9827a98c5b4258f2adf1
BLAKE2b-256 checksum
How to use checksums
075fd034617f70dbf2a92048b0c3536bc1cce10f88adefd43fd75abd51d918b1
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.20

Release files / opentelemetry_instrumentation_openai_v2-2.4b0-py3-none-any.whl

Download URL opentelemetry_instrumentation_openai_v2-2.4b0-py3-none-any.whl
Size 28.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c0c65fe4593fdcb466b55c047138ec71d28a5a3f36c3f0c2c5738343daa31d5c
BLAKE2b-256 checksum
How to use checksums
c3ed2e8f3348dfad1dc18c01c2bbc275694e6e6f0c85ccb56257b5516e5af702
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/6.2.0 CPython/3.10.20
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