> ## Documentation Index
> Fetch the complete documentation index at: https://docs.getnetra.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Gemini

> Trace Google Gemini model calls with Netra auto-instrumentation. Monitor prompts, completions, token usage, and latency across Gemini model variants.

<img src="https://mintcdn.com/netra/u6ajHWd7ki_9CRWQ/images/integration-logos/llm-providers/gemini.png?fit=max&auto=format&n=u6ajHWd7ki_9CRWQ&q=85&s=c78408dc0e7e51fcacc5cc19f791810f" alt="Google Gemini" width="354" height="80" data-path="images/integration-logos/llm-providers/gemini.png" />

## Installation

Install both the Netra SDK and Google Generative AI SDK:

<CodeGroup>
  ```bash Python theme={null}
  pip install netra-sdk google-generativeai
  ```

  ```bash Typescript theme={null}
  npm install netra-sdk @google/generative-ai
  ```
</CodeGroup>

## Usage

Initialize the Netra SDK with Gemini instrumentation enabled. The SDK automatically traces all Gemini API calls once initialized.

<CodeGroup>
  ```python Python theme={null}
  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)
  ```

  ```typescript Typescript theme={null}
  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();
  ```
</CodeGroup>

### Streaming Responses

The SDK automatically handles streaming responses and captures the complete output:

<CodeGroup>
  ```python Python theme={null}
  model = genai.GenerativeModel('gemini-pro')

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

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

  ```typescript Typescript theme={null}
  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);
  }
  ```
</CodeGroup>

### Chat Sessions

Chat operations are also automatically instrumented:

<CodeGroup>
  ```python Python theme={null}
  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)
  ```

  ```typescript Typescript theme={null}
  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());
  ```
</CodeGroup>

### Selective Instrumentation

Control which integrations are enabled using the `instruments` or `blockInstruments` configuration:

<CodeGroup>
  ```python Python theme={null}
  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}
  )
  ```

  ```typescript Typescript theme={null}
  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])
  });
  ```
</CodeGroup>

## Next Steps

* [Quick Start Guide](https://docs.getnetra.ai/quick-start/python) - Complete setup and configuration
* [Auto Instrumentation](https://docs.getnetra.ai/tracing/auto-instrumentation) - Automatic tracing for supported libraries
* [Decorators](https://docs.getnetra.ai/tracing/decorators) - Add custom tracing with `@workflow`, `@agent`, and `@task` decorators
* [Session Tracking](https://docs.getnetra.ai/tracing/session) - Track user sessions and conversations
* [Gemini Documentation](https://ai.google.dev/gemini-api/docs/quickstart) - Official Google Gemini API quickstart guide
