Skip to main content
Google Gemini

Installation

Install both the Netra SDK and Google Generative AI SDK:
pip install netra-sdk google-generativeai
npm install netra-sdk @google/generative-ai

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)
import { Netra } from "netra-sdk";
import { GoogleGenerativeAI } from "@google/generative-ai";

async function main() {
  // Initialize Netra with Gemini instrumentation (must await)
  await Netra.init({
    appName: "my-ai-app",
    headers: `x-api-key=${process.env.NETRA_API_KEY}`,
    traceContent: true
  });

  // Use Gemini client as usual - all calls are automatically traced
  const genAI = new GoogleGenerativeAI(process.env.GOOGLE_API_KEY);
  const model = genAI.getGenerativeModel({ model: "gemini-pro" });

  const result = await model.generateContent("What is observability?");
  const response = await result.response;

  console.log(response.text());
}

main();

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="")
const model = genAI.getGenerativeModel({ model: "gemini-pro" });

const result = await model.generateContentStream("Tell me a story");

for await (const chunk of result.stream) {
  const chunkText = chunk.text();
  process.stdout.write(chunkText);
}

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)
const model = genAI.getGenerativeModel({ model: "gemini-pro" });

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

const result = await chat.sendMessage("I have 2 dogs in my house.");
console.log(result.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}
)
import { Netra, NetraInstruments } from "netra-sdk";

// Only enable Gemini instrumentation
await Netra.init({
  headers: `x-api-key=${process.env.NETRA_API_KEY}`,
  instruments: new Set([NetraInstruments.GOOGLE_GENERATIVEAI])
});

// Or block specific instrumentations
await Netra.init({
  headers: `x-api-key=${process.env.NETRA_API_KEY}`,
  blockInstruments: new Set([NetraInstruments.HTTP])
});

Next Steps

Last modified on March 17, 2026