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

# Google Gemini Audio

> Trace Google Gemini text-to-speech operations with Netra auto-instrumentation. Monitor voice synthesis requests, streaming latency, and audio generation.

<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" />

Google Gemini provides text-to-speech capabilities through its generative AI models, enabling natural voice synthesis directly from the Gemini API. Netra helps you trace TTS operations, monitor synthesis performance, and analyze usage patterns.

## Installation

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

  ```bash TypeScript theme={null}
  npm install @google/genai netra-sdk
  ```
</CodeGroup>

## Usage

Initialize Netra before using Gemini audio APIs:

<CodeGroup>
  ```python Python theme={null}
  import os
  from netra import Netra

  Netra.init(
      app_name="gemini-audio-service",
      headers=f"x-api-key={os.environ.get('NETRA_API_KEY')}"
  )
  ```

  ```typescript TypeScript theme={null}
  import Netra from 'netra-sdk';

  await Netra.init({
    appName: 'gemini-audio-service',
    headers: `x-api-key=${process.env.NETRA_API_KEY}`
  });
  ```
</CodeGroup>

## Examples

### Text-to-Speech with Gemini

Track TTS generation using Netra decorators:

<CodeGroup>
  ```python Python theme={null}
  import os
  from google import genai
  from netra.decorators import task, workflow

  client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))

  @task()
  def generate_speech(text: str, voice: str = "Kore") -> bytes:
      """Generate speech using Gemini TTS."""
      response = client.models.generate_content(
          model="gemini-2.5-flash-preview-tts",
          contents=text,
          config=genai.types.GenerateContentConfig(
              response_modalities=["AUDIO"],
              speech_config=genai.types.SpeechConfig(
                  voice_config=genai.types.VoiceConfig(
                      prebuilt_voice_config=genai.types.PrebuiltVoiceConfig(
                          voice_name=voice
                      )
                  )
              )
          )
      )
      
      return response.candidates[0].content.parts[0].inline_data.data

  @task()
  def generate_multilingual_speech(text: str, voice: str = "Aoede") -> bytes:
      """Generate speech using Gemini 3.1 Flash TTS."""
      response = client.models.generate_content(
          model="gemini-3.1-flash-tts-preview",
          contents=text,
          config=genai.types.GenerateContentConfig(
              response_modalities=["AUDIO"],
              speech_config=genai.types.SpeechConfig(
                  voice_config=genai.types.VoiceConfig(
                      prebuilt_voice_config=genai.types.PrebuiltVoiceConfig(
                          voice_name=voice
                      )
                  )
              )
          )
      )
      
      return response.candidates[0].content.parts[0].inline_data.data

  @workflow()
  def create_audio_batch(texts: list[str], voice: str = "Kore") -> list[bytes]:
      """Generate speech for multiple text segments."""
      audio_segments = []
      
      for text in texts:
          audio = generate_speech(text, voice)
          audio_segments.append(audio)
      
      return audio_segments

  # Usage
  audio = generate_speech("Hello, this is Gemini text-to-speech.", "Kore")
  ```

  ```typescript TypeScript theme={null}
  import { GoogleGenAI } from '@google/genai';
  import { task, workflow } from 'netra-sdk';

  const genai = new GoogleGenAI({ apiKey: process.env.GOOGLE_API_KEY });

  class GeminiTTSService {
    @task()
    async generateSpeech(text: string, voice: string = 'Kore'): Promise<Buffer> {
      const response = await genai.models.generateContent({
        model: 'gemini-2.5-flash-preview-tts',
        contents: text,
        config: {
          responseModalities: ['AUDIO'],
          speechConfig: {
            voiceConfig: {
              prebuiltVoiceConfig: {
                voiceName: voice
              }
            }
          }
        }
      });

      const audioData = response.candidates[0].content.parts[0].inlineData.data;
      return Buffer.from(audioData, 'base64');
    }

    @task()
    async generateMultilingualSpeech(text: string, voice: string = 'Aoede'): Promise<Buffer> {
      const response = await genai.models.generateContent({
        model: "gemini-3.1-flash-tts-preview",
        contents: text,
        config: {
          responseModalities: ['AUDIO'],
          speechConfig: {
            voiceConfig: {
              prebuiltVoiceConfig: {
                voiceName: voice
              }
            }
          }
        }
      });

      const audioData = response.candidates[0].content.parts[0].inlineData.data;
      return Buffer.from(audioData, 'base64');
    }

    @workflow()
    async createAudioBatch(texts: string[], voice: string = 'Kore'): Promise<Buffer[]> {
      const audioSegments: Buffer[] = [];

      for (const text of texts) {
        const audio = await this.generateSpeech(text, voice);
        audioSegments.push(audio);
      }

      return audioSegments;
    }
  }

  // Usage
  const ttsService = new GeminiTTSService();
  const audio = await ttsService.generateSpeech('Hello, this is Gemini text-to-speech.', 'Kore');
  ```
</CodeGroup>

### Manual Span Creation with Action Tracking

For detailed control over tracing:

<CodeGroup>
  ```python Python theme={null}
  import os
  import time
  from google import genai
  from netra import SpanWrapper, ActionModel, UsageModel

  client = genai.Client(api_key=os.environ.get("GOOGLE_API_KEY"))

  def generate_speech_with_tracking(text: str, voice: str = "Kore") -> bytes:
      """Generate speech with detailed tracking."""
      span = SpanWrapper("gemini-tts")
      span.start()
      
      try:
          start_time = time.time_ns()
          span.set_attribute("text_length", len(text))
          span.set_attribute("voice", voice)
          span.set_attribute("model", "gemini-2.5-flash-preview-tts")
          
          response = client.models.generate_content(
              model="gemini-2.5-flash-preview-tts",
              contents=text,
              config=genai.types.GenerateContentConfig(
                  response_modalities=["AUDIO"],
                  speech_config=genai.types.SpeechConfig(
                      voice_config=genai.types.VoiceConfig(
                          prebuilt_voice_config=genai.types.PrebuiltVoiceConfig(
                              voice_name=voice
                          )
                      )
                  )
              )
          )
          
          audio_data = response.candidates[0].content.parts[0].inline_data.data
          end_time = time.time_ns()
          duration_ms = (end_time - start_time) / 1_000_000
          
          action = ActionModel(
              start_time=str(start_time),
              action="API",
              action_type="TTS_SYNTHESIS",
              metadata={
                  "provider": "google-genai",
                  "model": "gemini-2.5-flash-preview-tts",
                  "voice": voice,
                  "text_length": str(len(text)),
                  "audio_size_bytes": str(len(audio_data)),
                  "latency_ms": str(duration_ms)
              },
              success=True
          )
          span.set_action([action])
          
          usage = UsageModel(
              model="gemini-2.5-flash-preview-tts",
              usage_type="characters",
              units_used=len(text),
              cost_in_usd=len(text) * 0.000008
          )
          span.set_usage([usage])
          
          span.set_status({"code": 1, "message": "Success"})
          span.end()
          
          return audio_data
          
      except Exception as e:
          span.set_error(e)
          span.set_status({"code": 2, "message": "Error"})
          span.end()
          raise

  # Usage
  audio = generate_speech_with_tracking("This is Gemini voice synthesis.", "Kore")
  ```

  ```typescript TypeScript theme={null}
  import { GoogleGenAI } from '@google/genai';
  import { SpanWrapper, ActionModel } from 'netra-sdk';

  const genai = new GoogleGenAI({ apiKey: process.env.GOOGLE_API_KEY });

  async function generateSpeechWithTracking(text: string, voice: string = 'Kore'): Promise<Buffer> {
    const span = new SpanWrapper('gemini-tts');
    span.start();

    try {
      const startTime = Date.now();
      span.setAttribute('text_length', text.length);
      span.setAttribute('voice', voice);
      span.setAttribute('model', 'gemini-2.5-flash-preview-tts');

      const response = await genai.models.generateContent({
        model: 'gemini-2.5-flash-preview-tts',
        contents: text,
        config: {
          responseModalities: ['AUDIO'],
          speechConfig: {
            voiceConfig: {
              prebuiltVoiceConfig: {
                voiceName: voice
              }
            }
          }
        }
      });

      const audioData = response.candidates[0].content.parts[0].inlineData.data;
      const audioBuffer = Buffer.from(audioData, 'base64');
      const duration = Date.now() - startTime;

      const action: ActionModel = {
        start_time: (startTime * 1000000).toString(),
        action: 'API',
        action_type: 'TTS_SYNTHESIS',
        metadata: {
          provider: 'google-genai',
          model: 'gemini-2.5-flash-preview-tts',
          voice: voice,
          text_length: text.length.toString(),
          audio_size_bytes: audioBuffer.length.toString(),
          latency_ms: duration.toString()
        },
        success: true
      };
      span.setAction([action]);

      span.setUsage({
        model: 'gemini-2.5-flash-preview-tts',
        usage_type: 'characters',
        units_used: text.length,
        cost_in_usd: text.length * 0.000008
      });

      span.setStatus({ code: 1, message: 'Success' });
      span.end();

      return audioBuffer;
    } catch (error) {
      span.setError(error as Error);
      span.setStatus({ code: 2, message: 'Error' });
      span.end();
      throw error;
    }
  }

  // Usage
  const audio = await generateSpeechWithTracking('This is Gemini voice synthesis.', 'Kore');
  ```
</CodeGroup>

## Supported Models

| Model                          | Description                                 |
| ------------------------------ | ------------------------------------------- |
| `gemini-3.1-flash-tts-preview` | Latest TTS generation with Gemini 3.1 Flash |
| `gemini-2.5-flash-preview-tts` | Fast TTS generation with Gemini 2.5 Flash   |
| `gemini-2.5-pro-preview-tts`   | High-quality TTS with Gemini 2.5 Pro        |

## 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 Audio Documentation](https://ai.google.dev/gemini-api/docs/audio) - Official Google Gemini audio documentation
