Skip to main content

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:

  • sync and async chat 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

Azure OpenAI Environment Variables

The AzureOpenAI client extends OpenAI with parameters specific to the Azure OpenAI Service.

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)

Source distribution for elastic-opentelemetry-instrumentation-openai 1.2.0
File Size Uploaded
elastic_opentelemetry_instrumentation_openai-1.2.0.tar.gz 35.4 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for elastic-opentelemetry-instrumentation-openai 1.2.0
File Interpreter ABI Platform
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 log

Release 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

Release history Release notifications | RSS feed

This release

1.2.0 This release

2 release files

1.1.1

2 release files

1.1.0

2 release files

1.0.0

2 release files

0.7.0

2 release files

0.6.1

2 release files

0.6.0

2 release files

0.5.0

2 release files

0.4.0

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.0

2 release files

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