Google Generative AI Integration

Full observability for the Google GenAI SDK. Capture Gemini model calls, function calling, and multimodal inputs with automatic instrumentation.

Google GenAI >= 0.3.0Gemini ProGemini FlashMultimodal

Installation

Terminal
pip install turingpulse_sdk turingpulse_sdk_google_genai google-generativeai

Quick Start

1. Initialize & Instrument

setup.py
from turingpulse_sdk import init, TuringPulseConfig
from turingpulse_sdk_google_genai import patch_google_genai

# Initialize TuringPulse
init(TuringPulseConfig(
    api_key="sk_live_your_api_key",
    workflow_name="my-project",
))

# Enable auto-instrumentation for Google GenAI
patch_google_genai()

2. Use Google GenAI Normally

main.py
import google.generativeai as genai

genai.configure(api_key="your-gemini-key")

model = genai.GenerativeModel("gemini-1.5-pro")

# Run the model - traces are captured automatically
response = model.generate_content("Explain quantum computing in simple terms")
print(response.text)
ℹ️
Zero Code Changes
Once auto-instrumentation is enabled, all Google GenAI model calls, function invocations, and chat sessions are automatically traced.

What Gets Captured

Data PointDescriptionExample
Model CallsEach generate_content call with model and parametersgemini-1.5-pro, temp=0.9
Function CallingFunction declarations, calls, and responsesget_weather(location='NYC')
Multimodal InputsImage, video, and audio input metadataimage: 1024x768 JPEG, 245KB
Chat SessionsMulti-turn conversation history and contextchat session: 8 turns
Token UsageInput and output token counts per callprompt: 320, completion: 180
LatencyEnd-to-end and time-to-first-token timingtotal: 2200ms, ttfb: 450ms
Safety RatingsContent safety filter results per responseHARM_CATEGORY_HARASSMENT: LOW

Advanced Configuration

config.py
from turingpulse_sdk import instrument, KPIConfig
from turingpulse_sdk_google_genai import patch_google_genai

patch_google_genai(name="google-genai-service")

@instrument(
    name="genai-agent",
    kpis=[
        KPIConfig(kpi_id="latency_ms", use_duration=True, alert_threshold=8000),
        KPIConfig(kpi_id="tokens", alert_threshold=10000, comparator="gt"),
    ],
)
def my_agent(query: str):
    return model.generate_content(query)

Function Calling

functions.py
import google.generativeai as genai

def get_weather(location: str) -> dict:
    """Get the current weather for a location."""
    return {"location": location, "temp_f": 72, "condition": "Sunny"}

def search_restaurants(cuisine: str, city: str) -> dict:
    """Search for restaurants by cuisine type."""
    return {"results": [{"name": "Sakura", "rating": 4.5}]}

model = genai.GenerativeModel(
    model_name="gemini-1.5-pro",
    tools=[get_weather, search_restaurants],
)

# Function calls are automatically captured with inputs, outputs, and timing
response = model.generate_content("Find Italian restaurants in San Francisco")

Multimodal Inputs

multimodal.py
import google.generativeai as genai
from PIL import Image

model = genai.GenerativeModel("gemini-1.5-pro")

image = Image.open("diagram.png")

# Multimodal calls are traced with input metadata
response = model.generate_content([
    "Describe this diagram and explain the architecture.",
    image,
])

# Trace captures: image dimensions, format, prompt text, and full response
💡
Gemini Model Comparison
TuringPulse tracks performance across Gemini Pro and Flash models, helping you choose the right model for cost and quality trade-offs.

Next Steps