Registry /
observability / opentelemetry-instrumentation-google-genai
Install & Compatibility
Where this runs
tested against v0.7b1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 22.7MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 2.7s · import 0.000s · 23MB
21MB installed
● package 21MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
GoogleGenAIInstrumentor
✓ from opentelemetry.instrumentation.google_genai import GoogleGenAIInstrumentor
This quickstart demonstrates how to enable OpenTelemetry instrumentation for Google Generative AI. It sets up a basic OpenTelemetry SDK with a console exporter, then initializes the `GoogleGenAIInstrumentor`. Ensure `GEMINI_API_KEY` is set in your environment for the GenAI client to function.
import os
import google.generativeai as genai
from opentelemetry import trace
from opentelemetry.instrumentation.google_genai import GoogleGenAIInstrumentor
from opentelemetry.sdk.resources import Resource
from opentelemetry.sdk.trace import TracerProvider
from opentelemetry.sdk.trace.export import ConsoleSpanExporter, SimpleSpanProcessor
# 1. Configure OpenTelemetry SDK
resource = Resource.create(attributes={
"service.name": "my-genai-app",
"application.name": "google-gemini-example"
})
provider = TracerProvider(resource=resource)
# For local testing, use ConsoleSpanExporter to print traces to console
provider.add_span_processor(SimpleSpanProcessor(ConsoleSpanExporter()))
trace.set_tracer_provider(provider)
# 2. Enable Google GenAI instrumentation
GoogleGenAIInstrumentor().instrument()
# 3. Configure Google Generative AI
# Ensure GEMINI_API_KEY environment variable is set or pass directly
api_key = os.environ.get('GEMINI_API_KEY', 'YOUR_API_KEY_HERE')
if not api_key or api_key == 'YOUR_API_KEY_HERE':
print("Warning: GEMINI_API_KEY environment variable not set. API calls might fail.")
genai.configure(api_key=api_key)
# 4. Use Google GenAI - this will now be instrumented
print("\n--- Generating Content ---")
tracer = trace.get_tracer(__name__)
with tracer.start_as_current_span("generate-story"): # Custom span to wrap GenAI call
model = genai.GenerativeModel('gemini-pro')
try:
response = model.generate_content("Tell me a short, imaginative story about a cat who learns to fly.")
print("Generated story:\n" + response.text)
except Exception as e:
print(f"Error generating content: {e}")
print("\n--- Traces (if configured) should appear above/below ---")
Upgrade
Version history
0.7b1latest on PyPI · released May 19, 2026
Audit
Dependencies
opentelemetry-apirequiredCore OpenTelemetry API for tracing and metrics.
opentelemetry-sdkrequiredOpenTelemetry SDK for providers, processors, and exporters.
opentelemetry-instrumentationrequiredBase package for OpenTelemetry instrumentations.
opentelemetry-semantic-conventionsrequiredProvides standard attribute names for OpenTelemetry data, including GenAI conventions.
opentelemetry-util-genairequiredUtility functions for GenAI instrumentations within OpenTelemetry.
google-generativeairequiredThe Google Generative AI client library which this package instruments.
Resources
No resource links recorded.