Skip to main content

Installation

Install both the Netra SDK and Google Generative AI SDK:
pip install netra-sdk google-generativeai

Usage

Initialize the Netra SDK with Gemini instrumentation enabled. The SDK automatically traces all Gemini API calls once initialized.
from netra import Netra
import google.generativeai as genai
import os

# Initialize Netra with Gemini instrumentation
Netra.init(
    app_name="my-ai-app",
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    trace_content=True
)

# Use Gemini client as usual - all calls are automatically traced
genai.configure(api_key=os.environ.get("GOOGLE_API_KEY"))

model = genai.GenerativeModel('gemini-pro')

response = model.generate_content("What is observability?")
print(response.text)

Streaming Responses

The SDK automatically handles streaming responses and captures the complete output:
model = genai.GenerativeModel('gemini-pro')

response = model.generate_content("Tell me a story", stream=True)

for chunk in response:
    print(chunk.text, end="")

Chat Sessions

Chat operations are also automatically instrumented:
model = genai.GenerativeModel('gemini-pro')

chat = model.start_chat(history=[
  {"role": "user", "parts": ["Hello"]},
  {"role": "model", "parts": ["Great to meet you. What would you like to know?"]},
])

response = chat.send_message("I have 2 dogs in my house.")
print(response.text)

Selective Instrumentation

Control which integrations are enabled using the instruments or blockInstruments configuration:
from netra import Netra
from netra.instrumentation.instruments import InstrumentSet

# Only enable Gemini instrumentation
Netra.init(
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    instruments={InstrumentSet.GOOGLE_GENERATIVEAI}
)

# Or block specific instrumentations
Netra.init(
    headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}",
    block_instruments={InstrumentSet.HTTPX}
)

Next Steps

Last modified on February 3, 2026