Skip to main content

OpenTelemetry Official OpenAI instrumentation

Project description

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

az.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 true.

Uninstrument

To uninstrument clients, call the uninstrument method:

from opentelemetry.instrumentation.openai_v2 import OpenAIInstrumentor

OpenAIInstrumentor().instrument()
# ...

# Uninstrument all clients
OpenAIInstrumentor().uninstrument()

References

Project details


Download files

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

Source Distribution

opentelemetry_instrumentation_openai_v2-2.2b0.tar.gz (167.9 kB view details)

Uploaded Source

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_openai_v2-2.2b0.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_openai_v2-2.2b0.tar.gz
Algorithm Hash digest
SHA256 04f9a7730fd4be0f47f9b7e50b1a6bad2430aa8970c2537e6210c0ba9ba0b820
MD5 f7a700363a0d66ff0656b5259ea1de54
BLAKE2b-256 ebaea34cfd791962c8c7184674d64d200c1ba71d6f6809d1eef00a939fb1287e

See more details on using hashes here.

File details

Details for the file opentelemetry_instrumentation_openai_v2-2.2b0-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_openai_v2-2.2b0-py3-none-any.whl
Algorithm Hash digest
SHA256 9d08d9436faade042e5d69750007a119f17c4a9b98c66d851bd74329268491f3
MD5 2366e90870d6080d26de08b6750acdcf
BLAKE2b-256 9c2fba28b497371a820c408eec6479a6ee090cf5631cd91e82219e5fac85fdce

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page