Skip to main content

telemetry-dev-google-genai

Google GenAI (Gemini) SDK instrumentation for telemetry.dev. It wraps the official google-genai Python SDK and emits telemetry.dev generation and embedding spans through telemetry-dev.

Install

pip install telemetry-dev-google-genai

Initialize the core SDK first:

import telemetry_dev

telemetry_dev.init(
    api_key="td_live_...",
    base_url="http://localhost:4318",
    service_name="my-service",
)

Per-client wrapping

from google import genai
from telemetry_dev_google_genai import wrap_google_genai

client = wrap_google_genai(genai.Client(api_key="..."))

client.models.generate_content(
    model="gemini-2.5-flash",
    contents=[{"role": "user", "parts": [{"text": "Tell me a joke about OpenTelemetry"}]}],
)

Use this when you want explicit control over which clients are instrumented. wrap_google_genai patches both sync client.models and async client.aio.models.

Global instrumentation

from google import genai
from telemetry_dev_google_genai import instrument_google_genai, uninstrument_google_genai

instrument_google_genai()
client = genai.Client(api_key="...")

try:
    client.models.generate_content(
        model="gemini-2.5-flash",
        contents="Tell me a joke about OpenTelemetry",
    )
finally:
    uninstrument_google_genai()

Use this as the app-wide one-liner at startup when all GenAI clients should be instrumented.

Instrumented surfaces

Sync and async variants are covered:

  • client.models.generate_content(...) / client.aio.models.generate_content(...)
  • client.models.generate_content_stream(...) / client.aio.models.generate_content_stream(...)
  • client.models.embed_content(...) / client.aio.models.embed_content(...)
  • generate_images(...), edit_image(...), upscale_image(...), and generate_videos(...) on sync and async models

client.chats.create(...).send_message(...) and send_message_stream(...) are covered automatically because they call the wrapped models methods.

When generate_videos(...) returns a nonterminal operation, its span completes only after operations.get(...) on the same wrapped client observes that operation in a terminal state. An operation polled by another client or process cannot complete the submission span. If the wrapped client closes, instrumentation is removed, or the bounded operation tracker evicts an uncompleted operation, the submission span ends with an error instead of remaining open.

What gets captured

Gemini signal telemetry.dev field / attribute
model model
contents input
config.system_instruction system_instructions
sampling params (temperature, top_p, top_k, seed, penalties, max_output_tokens, stop_sequences) same-named span fields
JSON / schema output config output_type (json or text)
config.candidate_count gen_ai.request.choice.count
config.tools gen_ai.tool.definitions
config.tool_config, safety_settings, thinking_config, labels, cached_content, response_modalities google_genai.request.* attributes
response IDs, model version, finish reasons, output messages, usage tokens (including TEXT/IMAGE/AUDIO prompt, candidate, and cache details) mapped span fields
block/safety/grounding/url-context metadata google_genai.response.* attributes
AFC history on the final response replaces span input; sets google_genai.automatic_function_calling=true

The integration maps native Gemini request/response shapes directly. It never mutates caller requests.

Tool definitions (gen_ai.tool.definitions, including tool descriptions and parameter schemas) are request attributes, not captured content: capture_input, the mask, and max_attribute_length=0 do not remove them. They are cut only by the OpenTelemetry attribute limit (65,536 characters when max_attribute_length is 0).

Streaming

Gemini streams already include cumulative usage_metadata on chunks, so no request injection is needed. Stream spans record time-to-first-chunk, aggregate text parts (merging consecutive text with the same thought flag), last-seen usage/finish reasons, and end once when the stream completes, errors, or is closed.

Stream reconstruction retains up to 48 KiB and 1,000 items, shared with streamed safety, grounding, and URL-context metadata. Media request inputs are retained up to 64 KiB and 1,024 items. The configured max_attribute_length does not shrink these retention bounds; the core applies it to the exported attribute after the mask runs. A stream that is cut at its bound, closed early, or fails sets telemetry.dev.capture.truncated. With a mask configured, incomplete reconstructed output is omitted because the mask cannot inspect the complete value.

Automatic function calling (AFC)

When Python callables are passed in tools, the SDK may run an internal AFC loop across multiple transport calls. The integration emits one span for the public generate_content call and, when present, sets span input to automatic_function_calling_history.

Embeddings

Embedding calls emit gen_ai.operation.name = "embeddings", request model/input, embedding count/dimension attributes, optional token usage, and optional billable character counts. Embedding vectors are not captured as output.

Provider values

  • Gemini Developer API clients record provider gcp.gemini.
  • Vertex AI clients (vertexai=True) record provider gcp.vertex_ai.

Fail-open guarantee

Mapping code is defensive; wrapped calls return the SDK response unchanged and re-raise exceptions untouched. Telemetry bugs never break callers.

Limitations

  • Not instrumented: count_tokens, compute_tokens, live, caches, files, tunings, batches.
  • Media operations capture text prompts, model/provider attribution, non-binary media references such as URIs and MIME types, and gen_ai.operation.name=generate_content. Image, video, and reference-image bytes are never captured. Image and video operations emit the corresponding gen_ai.output.type.
  • Deliberately not captured: logprobs, citation metadata, create_time, sdk_http_response.
  • Unconsumed streams end their spans only when the stream is exhausted, errors, or is closed.

Metadata

Release files for telemetry-dev-google-genai 0.1.6

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for telemetry-dev-google-genai 0.1.6
File Size Uploaded
telemetry_dev_google_genai-0.1.6.tar.gz 16.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for telemetry-dev-google-genai 0.1.6
File Interpreter ABI Platform
telemetry_dev_google_genai-0.1.6-py3-none-any.whl Python 3 none any Details

Total release size: 33.9 kB

Release files / telemetry_dev_google_genai-0.1.6.tar.gz

Download URL telemetry_dev_google_genai-0.1.6.tar.gz
Size 16.5 kB
Tags Source
SHA-256 checksum
How to use checksums
e1bd5f98b9a83b555f439986f7b0e8417b0445e9f59e3a40c6289d29c5b1300e
BLAKE2b-256 checksum
How to use checksums
5250cf388d33d78b820e79d48436e4a38522f841efdbb22f4d069e75466c5887
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release files / telemetry_dev_google_genai-0.1.6-py3-none-any.whl

Download URL telemetry_dev_google_genai-0.1.6-py3-none-any.whl
Size 17.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
61fd9497f4e1230a5f08ca28784ea664621fb5701f8b9b21034a26f9d69c8fde
BLAKE2b-256 checksum
How to use checksums
4114416431532da167ac58bb62919ad273f9cb98339cd9981f0f0e60b7ca3509
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via uv/0.12.5 {"installer":{"name":"uv","version":"0.12.5","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"24.04","id":"noble","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":true}

Release history Release notifications | RSS feed

This release

0.1.6 This release

2 release files

0.1.5

2 release files

0.1.4

2 release files

0.1.3

2 release files

0.1.2

2 release files

0.1.1

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