Skip to content

Speak with Gemini 3.1 Flash TTS

This example assumes using Google.Gemini; is in scope and apiKey contains your Google.Gemini API key.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
using var client = new GeminiClient(apiKey);

try
{
    // Gemini 3.1 Flash TTS supports 200+ audio tags for controlling vocal style,
    // pacing, and delivery. Tags are written inline as bracketed directives;
    // the GeminiAudioTags helper exposes constants for the commonly used ones.
    var text =
        $"{GeminiAudioTags.Cheerful} Hello! " +
        $"{GeminiAudioTags.Excited} This is Gemini 3.1 Flash TTS. " +
        $"{GeminiAudioTags.Calm} It sounds great.";

    var result = await client.SpeakAsync(
        text: text,
        voiceName: GeminiVoices.Puck,
        modelId: "gemini-3.1-flash-tts-preview");

}
catch (ApiException ex) when (IsTransientAvailabilityIssue(ex))
{
    AssertTransientAvailability(ex);
}
catch (ApiException ex) when (ex.StatusCode is System.Net.HttpStatusCode.BadRequest &&
                              ex.Message.Contains("only be used for TTS"))
{
}