opentelemetry-instrumentation-google-genai 0.4b0


pip install opentelemetry-instrumentation-google-genai

  Latest version

Released: Oct 16, 2025


Meta
Author: OpenTelemetry Authors
Requires Python: >=3.9

Classifiers

Development Status
  • 4 - Beta

Intended Audience
  • Developers

License
  • OSI Approved :: Apache Software License

Programming Language
  • Python
  • Python :: 3
  • Python :: 3.10
  • Python :: 3.11
  • Python :: 3.12

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 operations. You can also optionally capture prompts and responses as log events.

Make sure to configure OpenTelemetry tracing, logging, metrics, and events 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.")

Enabling message content

Message content such as the contents of the prompt and response are not captured by default. To capture message content as log events, set the environment variable OTEL_INSTRUMENTATION_GENAI_CAPTURE_MESSAGE_CONTENT to true.

Uninstrument

To uninstrument clients, call the uninstrument method:

from opentelemetry.instrumentation.google_genai import GoogleGenAiSdkInstrumentor

GoogleGenAiSdkInstrumentor().instrument()
# ...

# Uninstrument all clients
GoogleGenAiSdkInstrumentor().uninstrument()

References

Extras:
Dependencies:
opentelemetry-api (~=1.37)
opentelemetry-instrumentation (<2,>=0.58b0)
opentelemetry-semantic-conventions (<2,>=0.58b0)
opentelemetry-util-genai (<0.3b0,>=0.2b0)