This library provides official OpenTelemetry instrumentation for the OpenAI Python API library (version 1.0.0 and above). It enables automatic tracing of LLM requests, capturing model name, token usage, finish reason, duration, and errors without modifying existing OpenAI client code. It also supports logging of messages and metrics, and is maintained as part of the OpenTelemetry Python Contrib project.
pip install opentelemetry-instrumentation-openai-v2 opentelemetry-sdk openaiVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to set up OpenTelemetry with the OpenAI instrumentation. It initializes a `TracerProvider` with a `ConsoleSpanExporter` to print traces to the console, then calls `OpenAIInstrumentor().instrument()` to automatically trace interactions with the OpenAI Python client (v1.0.0+). An example OpenAI chat completion call is included.
Ensure your `openai` package is `openai>=1.0.0`. Use `pip install opentelemetry-instrumentation-openai-v2` and import `OpenAIInstrumentor` from `opentelemetry.instrumentation.openai_v2`. Refer to OpenAI's official v1 migration guide for SDK changes.
Monitor release notes for breaking changes, especially when upgrading between minor beta versions. Configure your OpenTelemetry collector or backend to handle potential changes in attribute names by using schema URL transformations if available, or by adjusting dashboards/alerts.
To enable content capture for logs, set the environment variable `OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT` to `true`. Be aware of the implications for sensitive data.
Ensure you are using the latest available `opentelemetry-instrumentation-openai-v2` version. Test thoroughly if using `with_raw_response` or streaming functionalities, especially if encountering `AttributeError`.