Skip to main content
Pre-release

This release is a pre-release and may not be stable for production use.

The GenAI Utils package provides boilerplate and helpers to standardize instrumentation for Generative AI. It offers APIs to minimize the work needed to instrument GenAI libraries, while providing standardization for generating spans, metrics, and events.

Key Components

  • TelemetryHandler – manages LLM invocation lifecycles (spans, metrics, events)

  • InferenceInvocation and message types (Text, Reasoning, Blob, etc.) – structured data model for GenAI interactions

  • CompletionHook – protocol for uploading content to external storage (built-in fsspec support)

  • Metrics – gen_ai.client.operation.duration and gen_ai.client.token.usage histograms, plus the streaming timing histograms gen_ai.client.operation.time_to_first_chunk and gen_ai.client.operation.time_per_output_chunk

Usage

See the module docstring in opentelemetry.util.genai.handler for usage examples, including context manager and manual lifecycle patterns.

Environment Variables

This package relies on environment variables to configure capturing of message content. By default, message content will not be captured. Set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of:

  • NO_CONTENT: Do not capture message content (default).

  • SPAN_ONLY: Capture message content in spans only.

  • EVENT_ONLY: Capture message content in events only.

  • SPAN_AND_EVENT: Capture message content in both spans and events.

To control event emission, you can optionally set OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT to true or false (case-insensitive). This variable controls whether to emit gen_ai.client.inference.operation.details events. If not explicitly set, the default value is automatically determined by OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT:

  • When NO_CONTENT or SPAN_ONLY is set: defaults to false

  • When EVENT_ONLY or SPAN_AND_EVENT is set: defaults to true

If explicitly set, the user’s value takes precedence over the default.

When EVENT_ONLY or SPAN_AND_EVENT mode is enabled and a LoggerProvider is configured, the package also emits gen_ai.client.inference.operation.details events with structured message content (as dictionaries instead of JSON strings). Note that when using EVENT_ONLY or SPAN_AND_EVENT, the OTEL_INSTRUMENTATION_GENAI_EMIT_EVENT environment variable defaults to true, so events will be emitted automatically unless explicitly set to false.

Completion Hook / Upload

  • OTEL_INSTRUMENTATION_GENAI_COMPLETION_HOOK: Name of the completion hook entry point to load (e.g. upload).

  • OTEL_INSTRUMENTATION_GENAI_UPLOAD_BASE_PATH: An fsspec-compatible URI/path for uploading prompts and completions (e.g. /path/to/prompts or gs://my_bucket). Required when using the upload hook.

  • OTEL_INSTRUMENTATION_GENAI_UPLOAD_FORMAT: Format for uploaded data – json (default) or jsonl.

  • OTEL_INSTRUMENTATION_GENAI_UPLOAD_MAX_QUEUE_SIZE: Maximum number of concurrent uploads to queue (default: 20).

Span Attributes

This package sets the following span attributes on LLM invocations:

Common attributes:

  • gen_ai.operation.name: Str(chat)

  • gen_ai.provider.name: Str(openai)

  • gen_ai.request.model: Str(gpt-4o)

  • server.address: Str(api.openai.com)

  • server.port: Int(443)

Response attributes:

  • gen_ai.response.finish_reasons: Slice([“stop”])

  • gen_ai.response.model: Str(gpt-4o-2024-05-13)

  • gen_ai.response.id: Str(chatcmpl-Bz8yrvPnydD9pObv625n2CGBPHS13)

  • gen_ai.usage.input_tokens: Int(24)

  • gen_ai.usage.output_tokens: Int(7)

Request parameter attributes (when provided):

  • gen_ai.request.temperature: Float(0.7)

  • gen_ai.request.top_p: Float(1.0)

  • gen_ai.request.frequency_penalty: Float(0.0)

  • gen_ai.request.presence_penalty: Float(0.0)

  • gen_ai.request.max_tokens: Int(1024)

  • gen_ai.request.stop_sequences: Slice([”\n”])

  • gen_ai.request.seed: Int(42)

Content attributes (sensitive, requires content capturing enabled):

  • gen_ai.input.messages: Str(‘[{“role”: “user”, “parts”: [{“content”: “hello world”, “type”: “text”}]}]’)

  • gen_ai.output.messages: Str(‘[{“role”: “assistant”, “parts”: [{“content”: “hello back”, “type”: “text”}], “finish_reason”: “stop”}]’)

  • gen_ai.system_instructions: Str(‘[{“content”: “You are a helpful assistant.”, “type”: “text”}]’)

Error attributes:

  • error.type: Str(TimeoutError)

Embedding Span Attributes

This package also supports embedding invocation spans via the embedding context manager. For embedding invocations, the following attributes are set:

Common attributes:

  • gen_ai.operation.name: Str(embeddings)

  • gen_ai.provider.name: Str(openai)

  • server.address: Str(api.openai.com)

  • server.port: Int(443)

Request attributes:

  • gen_ai.request.model: Str(text-embedding-3-small)

  • gen_ai.embeddings.dimension.count: Int(1536)

  • gen_ai.request.encoding_formats: Slice([“float”])

Response attributes:

  • gen_ai.response.model: Str(text-embedding-3-small)

  • gen_ai.usage.input_tokens: Int(24)

Installation

pip install opentelemetry-util-genai

For upload support (requires fsspec):

pip install opentelemetry-util-genai[upload]

Design Document

The design document for the OpenTelemetry GenAI Utils can be found at: Design Document

References

Download files

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

Source Distribution

opentelemetry_util_genai-1.1b0.tar.gz (68.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

opentelemetry_util_genai-1.1b0-py3-none-any.whl (50.1 kB view details)

Uploaded Python 3

File details

Details for the file opentelemetry_util_genai-1.1b0.tar.gz.

File metadata

  • Download URL: opentelemetry_util_genai-1.1b0.tar.gz
  • Upload date:
  • Size: 68.1 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/7.0.0 CPython/3.13.14

File hashes

Hashes for opentelemetry_util_genai-1.1b0.tar.gz
Algorithm Hash digest
SHA256 079c6118dbca9a6f667913ac5f1f4a8477a94e17f30d198a0988d1d52dd6ff10
MD5 ed8c9fc791b553b3dd1fdc9f8d86b8e8
BLAKE2b-256 501a2bd5102d38409cf6db92ff0120e035cf971f819a43132887286402420c01

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_util_genai-1.1b0.tar.gz:

Publisher: release-all.yml on open-telemetry/opentelemetry-python-genai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file opentelemetry_util_genai-1.1b0-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_util_genai-1.1b0-py3-none-any.whl
Algorithm Hash digest
SHA256 77a33be11b234e7bf2f5eaced90206dec577a971b54c383e32f71e7d96c96d10
MD5 0e8925f36a503bf3eafff08143a7a729
BLAKE2b-256 65bc0bc52014dd62284dcb114cad380e80220df7629ad4ab8604046d23f07acd

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_util_genai-1.1b0-py3-none-any.whl:

Publisher: release-all.yml on open-telemetry/opentelemetry-python-genai

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Release history Release notifications | RSS feed

This release

1.1b0 This release

2 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