Skip to main content
Pre-release

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

pypi

This library adds instrumentation to the Google GenAI SDK library to emit telemetry data following Semantic Conventions for GenAI systems. It adds trace spans for GenAI operations, events/logs for recording prompts/responses, and emits metrics that describe the GenAI operations in aggregate.

Experimental

This package is still experimental. The instrumentation may not be complete or correct just yet.

Please see “TODOS.md” for a list of known defects/TODOs that are blockers to package stability.

Installation

If your application is already instrumented with OpenTelemetry, add this package to your requirements.

pip install opentelemetry-instrumentation-google-genai

If you don’t have a Google GenAI SDK application, yet, try our examples.

Check out zero-code example for a quick start.

Usage

This section describes how to set up Google GenAI SDK instrumentation if you’re setting OpenTelemetry up manually. Check out the manual example for more details.

Instrumenting all clients

When using the instrumentor, all clients will automatically trace GenAI generate_content, embed_content and interactions.create operations. You can also optionally capture prompts and responses as log events.

Make sure to configure OpenTelemetry tracing, logging, and metrics to capture all telemetry emitted by the instrumentation.

from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor
from google.genai import Client

GoogleGenAiSdkInstrumentor().instrument()

client = Client()
response = client.models.generate_content(
    model="gemini-1.5-flash-002",
    contents="Write a short poem on OpenTelemetry."
)

Limitations

When using the Google GenAI SDK with automatic function calling enabled, the OpenTelemetry instrumentation creates an execute_tool span for each tool call the SDK executes, these spans are nested under the generate_content span.

Only tool calls that the SDK executes are traced, if you disable the automatic function calling and instead do the function calling in your application, our instrumentation cannot trace them.

Enabling message content

Message content is not captured by default. To capture message content set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to one of NO_CONTENT, SPAN_AND_EVENT, SPAN_ONLY, EVENT_ONLY. This controls whether the following content is captured on spans and/or events:

  • Input messages to the LLM.

  • Output messages from the LLM.

  • System Instructions

  • The result of tool calls and the tool call parameters (other tool call details and tool definitions are always captured).

Configuration recording

The instrumentation can optionally record GenerateContentConfig parameters as span and event attributes under the gcp.gen_ai.operation.config.* namespace.

By default, no config fields are recorded. You can control which fields are captured using the following environment variables:

  • OTEL_GOOGLE_GENAI_GENERATE_CONTENT_CONFIG_INCLUDES — A comma-separated list of config field names to include in the span attributes. For example:

    export OTEL_GOOGLE_GENAI_GENERATE_CONTENT_CONFIG_INCLUDES=temperature,max_output_tokens
  • OTEL_GOOGLE_GENAI_GENERATE_CONTENT_CONFIG_EXCLUDES — A comma-separated list of config field names to exclude from the span attributes:

    export OTEL_GOOGLE_GENAI_GENERATE_CONTENT_CONFIG_EXCLUDES=stop_sequences

If both variables are set, the includes list is applied first, then the excludes list filters the result further.

Uninstrument

To uninstrument clients, call the uninstrument method:

from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor

GoogleGenAiSdkInstrumentor().instrument()
# ...

# Uninstrument all clients
GoogleGenAiSdkInstrumentor().uninstrument()

References

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

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

File details

Details for the file opentelemetry_instrumentation_google_genai-1.0b1.tar.gz.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_google_genai-1.0b1.tar.gz
Algorithm Hash digest
SHA256 784b3a473ed51033837114b96854b31a0f0b0fe7c81e132e58a45352eee3b102
MD5 4f39a65be7aa3cfabbf7cf4e66c18867
BLAKE2b-256 7c13a2422f9164c09406092576d878d9b23d74b0a2ed75c1a77454c3a0178eb1

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_instrumentation_google_genai-1.0b1.tar.gz:

Publisher: release-package.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_instrumentation_google_genai-1.0b1-py3-none-any.whl.

File metadata

File hashes

Hashes for opentelemetry_instrumentation_google_genai-1.0b1-py3-none-any.whl
Algorithm Hash digest
SHA256 50789eebf4ce38dc09147e28bdbd96d45550c7f479232c593c0ddbdab37cf97f
MD5 a05c3dd262af5efcadeacbe592c23bb1
BLAKE2b-256 07a3315330fb955d5caea51793c4f49d8904fcade3927b3512fdc0595b3b701a

See more details on using hashes here.

Provenance

The following attestation bundles were made for opentelemetry_instrumentation_google_genai-1.0b1-py3-none-any.whl:

Publisher: release-package.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.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page