Google GenAI (tracing)

The official Google Gen AI SDK (google-genai) is the Python client for Gemini models, supporting text, multimodal prompts, streaming, tool use, and structured outputs. Respan gives you full observability over every Gemini generation, streamed response, token count, and model tool call — and gateway routing through the Respan endpoint.

Create an account at platform.respan.ai and grab an API key.

Run npx @respan/cli setup to set up with your coding agent.

See Google Gen AI gateway setup to route this integration through the Respan gateway.

Setup

1

Install packages

$pip install respan-ai respan-instrumentation-google-genai google-genai
2

Set environment variables

$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"
$# Optional for direct Google calls while still exporting traces to Respan
$export GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"

If GOOGLE_API_KEY is not set, route through the Respan Gemini gateway with RESPAN_API_KEY. Gateway calls require Gemini provider credentials or managed credits configured on your Respan account.

3

Initialize and run

1import os
2from google import genai
3from respan import Respan
4from respan_instrumentation_google_genai import GoogleGenAIInstrumentor
5
6respan = Respan(instrumentations=[GoogleGenAIInstrumentor()])
7
8client = genai.Client(
9 api_key=os.environ.get("GOOGLE_API_KEY") or os.environ["RESPAN_API_KEY"],
10 http_options=None if os.environ.get("GOOGLE_API_KEY") else {
11 "base_url": "https://api.respan.ai/api/google/gemini"
12 },
13)
14
15response = client.models.generate_content(
16 model="gemini-2.5-flash",
17 contents="Say hello in three languages.",
18)
19print(response.text)
20respan.flush()
4

View your trace

Open the Traces page to see your Gemini span with prompt content, completion content, model name, token usage, and tool-call attributes.

Configuration

ParameterTypeDefaultDescription
api_keystr | NoneNoneFalls back to RESPAN_API_KEY env var.
base_urlstr | NoneNoneFalls back to RESPAN_BASE_URL env var.
instrumentationslist[]Plugin instrumentations to activate, such as GoogleGenAIInstrumentor().
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag, such as "production".

Attributes

In Respan()

Set defaults at initialization — these apply to all generated spans.

1from respan import Respan
2from respan_instrumentation_google_genai import GoogleGenAIInstrumentor
3
4respan = Respan(
5 instrumentations=[GoogleGenAIInstrumentor()],
6 customer_identifier="user_123",
7 metadata={"service": "gemini-api", "version": "1.0.0"},
8)

With propagate_attributes

Override per-request using a context scope.

1from google import genai
2from respan import Respan, propagate_attributes
3from respan_instrumentation_google_genai import GoogleGenAIInstrumentor
4
5respan = Respan(instrumentations=[GoogleGenAIInstrumentor()])
6client = genai.Client(
7 api_key=os.environ.get("GOOGLE_API_KEY") or os.environ["RESPAN_API_KEY"],
8 http_options=None if os.environ.get("GOOGLE_API_KEY") else {
9 "base_url": "https://api.respan.ai/api/google/gemini"
10 },
11)
12
13def handle_request(user_id: str, question: str):
14 with propagate_attributes(
15 customer_identifier=user_id,
16 thread_identifier="conv_abc_123",
17 metadata={"plan": "pro"},
18 ):
19 response = client.models.generate_content(
20 model="gemini-2.5-flash",
21 contents=question,
22 )
23 print(response.text)
AttributeTypeDescription
customer_identifierstrIdentifies the end user in Respan analytics.
thread_identifierstrGroups related messages into a conversation.
metadatadictCustom key-value pairs. Merged with default metadata.