Google Gen AI

The official Google Gen AI SDK is the client for Google’s Gemini models, supporting text, multimodal, tool use, and structured outputs. Respan gives you full observability over every Gemini call, streamed response, and tool invocation — and gateway routing through the Respan endpoint.

Create an account at platform.respan.ai and grab an API key. For gateway, also add credits or a provider key.

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

Setup

1

Install packages

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

Set environment variables

$export GOOGLE_API_KEY="YOUR_GOOGLE_API_KEY"
$export RESPAN_API_KEY="YOUR_RESPAN_API_KEY"

GOOGLE_API_KEY is used for Gemini requests. RESPAN_API_KEY is used to export traces to Respan.

3

Initialize and run

1import os
2from google import genai
3from respan import Respan
4from opentelemetry.instrumentation.google_generativeai import GoogleGenerativeAiInstrumentor
5
6respan = Respan(instrumentations=[GoogleGenerativeAiInstrumentor()])
7
8client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
9
10response = client.models.generate_content(
11 model="gemini-2.5-flash",
12 contents="Say hello in three languages.",
13)
14print(response.text)
15respan.flush()
4

View your trace

Open the Traces page to see your auto-instrumented Gemini spans with prompts, tokens, and tool calls.

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 (e.g. GoogleGenerativeAiInstrumentor()).
customer_identifierstr | NoneNoneDefault customer identifier for all spans.
metadatadict | NoneNoneDefault metadata attached to all spans.
environmentstr | NoneNoneEnvironment tag (e.g. "production").

Attributes

In Respan()

Set defaults at initialization — these apply to all spans.

1from respan import Respan
2from opentelemetry.instrumentation.google_generativeai import GoogleGenerativeAiInstrumentor
3
4respan = Respan(
5 instrumentations=[GoogleGenerativeAiInstrumentor()],
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 opentelemetry.instrumentation.google_generativeai import GoogleGenerativeAiInstrumentor
4
5respan = Respan(instrumentations=[GoogleGenerativeAiInstrumentor()])
6client = genai.Client(api_key=os.environ["GOOGLE_API_KEY"])
7
8def handle_request(user_id: str, question: str):
9 with propagate_attributes(
10 customer_identifier=user_id,
11 thread_identifier="conv_abc_123",
12 metadata={"plan": "pro"},
13 ):
14 response = client.models.generate_content(
15 model="gemini-2.5-flash",
16 contents=question,
17 )
18 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.