Skip to Content

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.

Set up Respan

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

Paste this into your coding agent:

Help me set up Respan tracing with the Google GenAI integration. Follow https://www.respan.ai/docs/integrations/google-genai.md

Setup

Install packages

Terminal
pip install respan-ai respan-instrumentation-google-genai google-genai

Set environment variables

Terminal
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.

Initialize and run

Python
import os from google import genai from respan import Respan from respan_instrumentation_google_genai import GoogleGenAIInstrumentor respan = Respan(instrumentations=[GoogleGenAIInstrumentor()]) client = genai.Client( api_key=os.environ.get("GOOGLE_API_KEY") or os.environ["RESPAN_API_KEY"], http_options=None if os.environ.get("GOOGLE_API_KEY") else { "base_url": "https://api.respan.ai/api/google/gemini" }, ) response = client.models.generate_content( model="gemini-2.5-flash", contents="Say hello in three languages.", ) print(response.text)

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.

Python
from respan import Respan from respan_instrumentation_google_genai import GoogleGenAIInstrumentor respan = Respan( instrumentations=[GoogleGenAIInstrumentor()], customer_identifier="user_123", metadata={"service": "gemini-api", "version": "1.0.0"}, )

With propagate_attributes

Override per-request using a context scope.

Python
from google import genai from respan import Respan, propagate_attributes from respan_instrumentation_google_genai import GoogleGenAIInstrumentor respan = Respan(instrumentations=[GoogleGenAIInstrumentor()]) client = genai.Client( api_key=os.environ.get("GOOGLE_API_KEY") or os.environ["RESPAN_API_KEY"], http_options=None if os.environ.get("GOOGLE_API_KEY") else { "base_url": "https://api.respan.ai/api/google/gemini" }, ) def handle_request(user_id: str, question: str): with propagate_attributes( customer_identifier=user_id, thread_identifier="conv_abc_123", metadata={"plan": "pro"}, ): response = client.models.generate_content( model="gemini-2.5-flash", contents=question, ) 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.