OpenAI integration for telemetry.dev Python SDK
Project description
telemetry-dev-openai
OpenAI SDK instrumentation for telemetry.dev. It wraps the official openai Python SDK and emits telemetry.dev generation and embedding spans through telemetry-dev.
Install
pip install telemetry-dev-openai
Initialize the core SDK first:
import telemetry_dev
telemetry_dev.init(
api_key="td_live_...",
base_url="http://localhost:4318",
service_name="my-service",
)
Per-client wrapping
from openai import OpenAI
from telemetry_dev_openai import wrap_openai
client = wrap_openai(OpenAI())
client.chat.completions.create(
model="gpt-4o-mini",
messages=[{"role": "user", "content": "Tell me a joke about OpenTelemetry"}],
)
Use this when you want explicit control over which sync or async clients are instrumented.
Global instrumentation
from openai import OpenAI
from telemetry_dev_openai import instrument_openai, uninstrument_openai
instrument_openai()
client = OpenAI()
try:
client.responses.create(model="gpt-4o-mini", input="Tell me a joke about OpenTelemetry")
finally:
uninstrument_openai()
Use this as the app-wide one-liner at startup when all OpenAI clients should be instrumented.
Instrumented surfaces
Sync and async variants are covered:
client.chat.completions.create(...)client.chat.completions.parse(...)client.chat.completions.stream(...)client.responses.create(...)client.responses.parse(...)client.responses.stream(...)when starting a new responseclient.embeddings.create(...)
The integration maps native OpenAI request/response shapes directly into telemetry.dev fields. It does not normalize messages into another schema.
Streaming
Chat completion streams are traced. Requests are sent unchanged by default, so token usage is only captured when the caller sets stream_options={"include_usage": True} themselves. Pass inject_stream_usage=True to wrap_openai or instrument_openai to inject it automatically; the synthetic usage-only chunk is then hidden from the caller. Injection is opt-in because some providers reject stream_options — for example Azure OpenAI "on your data" (data_sources) returns 400 for it while plain stream=True works.
client = wrap_openai(OpenAI(), inject_stream_usage=True)
Responses API streams are traced through responses.create(stream=True); terminal response.completed, response.failed, and response.incomplete events close the span.
Embeddings
Embedding calls emit gen_ai.operation.name = "embeddings", request model/input, response model, and token usage. Embedding vectors are intentionally not captured as output.
Azure OpenAI
wrap_openai(AzureOpenAI(...)) and wrap_openai(AsyncAzureOpenAI(...)) record provider azure.ai.openai. Global class instrumentation detects Azure from the resource client when available.
Limitations
with_raw_responsesnapshots bound methods on first access in the OpenAI Python SDK. Callwrap_openai()orinstrument_openai()before accessingwith_raw_responseif those methods need instrumentation.responses.stream(response_id=...)resumes an existing response throughretrieve(), which is not instrumented in this version.- Unconsumed streams end their spans only when the stream is exhausted, errors, or is closed.
Project details
Release history Release notifications | RSS feed
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file telemetry_dev_openai-0.1.0.tar.gz.
File metadata
- Download URL: telemetry_dev_openai-0.1.0.tar.gz
- Upload date:
- Size: 8.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
68fb2f8f529e17cdca2745416dadd3cd95104b5b2d5e9e033a8cc0c6ae7a6cf1
|
|
| MD5 |
32d8194bcce1fac7399dcd4ef1b54bd9
|
|
| BLAKE2b-256 |
05c9ebf7447632b4deb822322370c61d05b62ddcd30cd2364a8b7d7e251eee0b
|
File details
Details for the file telemetry_dev_openai-0.1.0-py3-none-any.whl.
File metadata
- Download URL: telemetry_dev_openai-0.1.0-py3-none-any.whl
- Upload date:
- Size: 9.4 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: uv/0.11.27 {"installer":{"name":"uv","version":"0.11.27","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"macOS","version":null,"id":null,"libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
894d36bd7dda4e7676ed6ee8f182a8608fb7da438e118b436022c37cce15af9a
|
|
| MD5 |
da46e9ebac08446e462857a4d5151c3d
|
|
| BLAKE2b-256 |
566e02ef48beb8f6758610c4a1e7cafb609675148c44d31fcf69f0e27fc3c1bb
|