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.
Docs: https://telemetry.dev/docs/integrations/google-genai
Install
pip install telemetry-dev-google-genai
Requires Python 3.10 or later and google-genai 2.
Initialize the core SDK first. init() reads your project API key from the TELEMETRY_DEV_API_KEY environment variable, so set it before you start the app:
import telemetry_dev
telemetry_dev.init(service_name="my-service")
Versions of telemetry-dev before 0.3.0 sent data to https://ingest.telemetry.dev by default.
That hostname reaches the same service and stays supported: if a firewall allows only it, set
TELEMETRY_DEV_BASE_URL to it.
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-3.8-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-3.8-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(...), andgenerate_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.
Block, safety, grounding, and URL-context metadata are raw attributes, which the core's capture_output check and mask never see. The block reason message is free text. Grounding metadata repeats the answer text segment by segment and can carry retrieved document text, source titles and URIs, and search queries; URL-context metadata lists the retrieved URLs. This metadata is recorded whole only when output is captured and no mask is configured. Otherwise google_genai.response.block_reason_message is omitted and the other attributes keep only their structure: the block reason; each safety rating's category, probability, severity, their scores, blocked, and overwritten threshold, with enum fields kept only when they look like an enum value (such as HARM_CATEGORY_HARASSMENT); each grounding chunk's source type (such as {"web": {}}), segment offsets, chunk indices, confidence scores, and the dynamic retrieval score; and each URL's retrieval status.
Tool definitions (gen_ai.tool.definitions) always hold each tool's type and name. Tool descriptions and parameter schemas are prompt content: they are recorded only with capture_input on, and pass through the mask. When the whole list is longer than max_attribute_length, 0 included, it keeps only each tool's type and name, so that it stays JSON, and it is left out when those do not fit either (0 sets no limit for them). With telemetry-dev 0.3.2 or earlier, the attribute is cut at 65,536 characters by default and 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, shared with streamed safety, grounding, and URL-context metadata, is kept up to 16 MiB, and all streams in flight share the max_stream_bytes bound of the core (64 MiB by default). These bounds need a telemetry-dev core with max_stream_bytes; with an older core, each stream keeps up to 64 KiB and 1,024 items. Media request inputs are retained up to 16 MiB. 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. A chunk that fails to map is reported to on_error; only the first such failure of a stream is reported. Any other failure while a chunk is recorded, such as in its response ID, model version, candidate index or role, parts, or finish reasons, also sets the attribute, and no later part is kept; parts of that chunk recorded before the failure are kept. A failure in a chunk's time to first chunk, response ID and model version for the span, or output-chunk timing loses only that value and does not set the attribute. A failure in usage loses only that value and does not set the attribute. A failure in a candidate's safety ratings, grounding or URL-context metadata can lose all of that candidate's metadata from that chunk, and a failure in prompt feedback can lose all of that chunk's prompt feedback (block reason, its message, and prompt safety ratings); the output stays whole. Such a failure sets the attribute when that metadata is recorded whole (output captured and no mask configured), as a value dropped by the bound does, and not when only its structure is kept. The AFC history is the only record of the tool calls and their results, so a failure while it is recorded sets the attribute, also with a mask configured. The history is not output, so the output is kept whole and is not withheld by a mask; the history kept from an earlier chunk, if any, is exported, and a later history that maps replaces it. A stream whose fields fail to map when it ends exports no output and sets the attribute. 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 providergcp.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. When the response of a successful generate_content, embed_content, image, or video call fails to map, the failure is reported to on_error and the span ends without output and with telemetry.dev.capture.truncated; it is not marked failed.
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 correspondinggen_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.
License
MIT. See LICENSE.
Metadata
Release files for telemetry-dev-google-genai 0.2.8
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| telemetry_dev_google_genai-0.2.8.tar.gz | 23.4 kB | Without provenance | Details for telemetry_dev_google_genai-0.2.8.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| telemetry_dev_google_genai-0.2.8-py3-none-any.whl | Python 3 | none | any | Details for telemetry_dev_google_genai-0.2.8-py3-none-any.whl |
Total release size: 47.8 kB
Release files / telemetry_dev_google_genai-0.2.8.tar.gz
| Download URL | telemetry_dev_google_genai-0.2.8.tar.gz |
|---|---|
| Size | 23.4 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
a39d45e47aa083c76934ce70cf907b3ddf92b6b0ef364700ec429ddc9d80cf74
|
|
BLAKE2b-256 checksum How to use checksums |
04a7b5fc2db8788f536928c93c863c0bf80687c4e040830504d7529852fc3076
|
| 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.2.8-py3-none-any.whl
| Download URL | telemetry_dev_google_genai-0.2.8-py3-none-any.whl |
|---|---|
| Size | 24.4 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
95ac129260dda6252d6b0de783a3a80aab7cca0fdc852fd90c702a45bd132eaf
|
|
BLAKE2b-256 checksum How to use checksums |
09a3f67c29538fec8ffa9f274cd24c495ef832bbc698db3184ecba71d04d8bb6
|
| 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}
|
Security for telemetry-dev-google-genai 0.2.8
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.