OpenTelemetry Instrumentation for OpenAI
An OpenTelemetry instrumentation for the openai client library.
This instrumentation currently supports instrumenting the chat completions (create and parse (beta) APIs) and the embeddings API.
We currently support the following features:
syncandasyncchat completions- Streaming support for chat completions
- Functions calling with tools for chat completions
- Client side metrics
- Embeddings API calls
- Following 1.29.0 Gen AI Semantic Conventions
Installation
pip install elastic-opentelemetry-instrumentation-openai
Usage
This instrumentation supports zero-code / autoinstrumentation:
Set up a virtual environment with this package, the dependencies it requires
and dotenv (a portable way to load environment variables).
python3 -m venv .venv
source .venv/bin/activate
pip install -r dev-requirements.txt
pip install python-dotenv[cli]
Create a .env file containing the OpenAI API key:
echo "OPENAI_API_KEY=sk-..." > .env
Run the script with telemetry setup to use the instrumentation.
dotenv run -- opentelemetry-instrument python examples/chat.py
You can record more information about prompts as log events by enabling content capture.
OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT=true dotenv run -- \
opentelemetry-instrument python examples/chat.py
Using a local model
Ollama may be used to run examples without a cloud account. After you have set it up need to install the models in order to run the examples:
# for chat
ollama pull qwen2.5:0.5b
# for embeddings
ollama pull all-minilm:33m
Finally run the examples using ollama.env variables to point to Ollama instead of OpenAI:
dotenv -f ollama.env run -- opentelemetry-instrument python examples/chat.py
Instrumentation specific environment variable configuration
None
Elastic specific semantic conventions
None at the moment
Development
We use pytest to execute tests written with the standard library unittest framework.
Test dependencies need to be installed before running.
python3 -m venv .venv
source .venv/bin/activate
pip install -r dev-requirements.txt
pytest
To run integration tests doing real requests:
OPENAI_API_KEY=unused pytest --integration-tests
Refreshing HTTP payloads
We use VCR.py to automatically record HTTP responses from LLMs to reuse in tests without running the LLM. Refreshing HTTP payloads may be needed in these cases
- Adding a new unit test
- Extending a unit test with functionality that requires an up-to-date HTTP response
Integration tests default to using ollama, to avoid cost and leaking sensitive information. However, unit test recordings should use the authoritative OpenAI platform unless the test is about a specific portability corner case.
To refresh a test, delete its cassette file in tests/cassettes and make sure you have environment variables set for recordings, detailed later.
If writing a new test, start with the test logic with no assertions. If extending an existing unit test rather than writing a new one, remove the corresponding recorded response from cassettes instead.
Then, run pytest as normal. It will execute a request against the LLM and record it. Update the
test with correct assertions until it passes. Following executions of pytest will use the recorded
response without querying the LLM.
OpenAI Environment Variables
OPENAI_API_KEY- from https://platform.openai.com/settings/profile?tab=api-keys- It should look like
sk-...
- It should look like
Azure OpenAI Environment Variables
The AzureOpenAI client extends OpenAI with parameters specific to the Azure OpenAI Service.
AZURE_OPENAI_ENDPOINT- "Azure OpenAI Endpoint" in https://oai.azure.com/resource/overview- It should look like
https://<your-resource-name>.openai.azure.com/
- It should look like
AZURE_OPENAI_API_KEY- "API key 1 (or 2)" in https://oai.azure.com/resource/overview- It should look be a hex string like
abc01...
- It should look be a hex string like
OPENAI_API_VERSION= "Inference version" from https://learn.microsoft.com/en-us/azure/ai-services/openai/api-version-deprecation- It should look like
2024-10-01-preview
- It should look like
TEST_CHAT_MODEL= "Name" from https://oai.azure.com/resource/deployments that deployed a model that supports tool calling, such as "gpt-4o-mini".TEST_EMBEDDINGS_MODEL= "Name" from https://oai.azure.com/resource/deployments that deployed a model that supports embeddings, such as "text-embedding-3-small".
Note: The model parameter of a chat completion or embeddings request is substituted for an identical
deployment name. As deployment names are arbitrary they may have no correlation with a real model
like gpt-4o
License
This software is licensed under the Apache License, version 2 ("Apache-2.0").
Metadata
Release files for elastic-opentelemetry-instrumentation-openai 1.2.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz | 35.4 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| elastic_opentelemetry_instrumentation_openai-1.2.0-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 59.8 kB
Release files / elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz
| Download URL | elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz |
|---|---|
| Size | 35.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
b393bb3816c91ba2492adf5b87e2665b30e481facbf1f95909ebd658b0ea5d20
|
|
BLAKE2b-256 checksum How to use checksums |
e0c4a80de71f1f02f2ae278a7cd5850ccaa4512ca81d7b57d9ca7318d43d176f
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 6, 2025.
Transparency logRelease files / elastic_opentelemetry_instrumentation_openai-1.2.0-py3-none-any.whl
| Download URL | elastic_opentelemetry_instrumentation_openai-1.2.0-py3-none-any.whl |
|---|---|
| Size | 24.5 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
efd8ab1127d48207ae6db2b250e8bc7aca44f264c606a2acea05ff5a697d5ee9
|
|
BLAKE2b-256 checksum How to use checksums |
dccaff93835b2c2471a91c28279299a77e6ba57aa03bafd0619c356c5bc370a8
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.13.7
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 6, 2025.
Transparency log