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

# Gemini 3.8 Flash-Lite TTS

> Gemini 3.8 Flash-Lite TTS: 通过 Gemini 原生接口进行文本转语音，支持结构化朗读风格和 WAV 输出。

Gemini 3.8 Flash-Lite TTS 侧重低延迟和高吞吐语音生成。Google 公布支持 101 种语言。

Gemini 3.8 Flash-Lite TTS 接收文本并输出音频。厂商限制为 8,192 输入 tokens、16,384 输出 tokens；实际限制与可用性取决于 MixRoute 路由。示例使用现有 Gemini 原生 generateContent 路径，不假定支持 Interactions 或 OpenAI 音频接口。

## 请求参数

parts\[].text 只写需要朗读的原文，朗读指令放入 parts\[].speechMetadata.style。不要加 Say cheerfully 之类前缀，它可能被直接读出。SDK 或说明文档可能使用 speech\_metadata 命名；REST JSON 字段为 speechMetadata。3.8 的非流式输出默认 WAV，不再沿用旧版默认裸 PCM。

| 字段 | 说明 |
| - | - |
| `contents[].parts[].text` | 必填，仅放需要朗读的文本。 |
| `contents[].parts[].speechMetadata.style` | 可选的语气和表达方式指令。 |
| `generationConfig.responseModalities` | \["AUDIO"] |
| `generationConfig.speechConfig` | 使用 generationConfig.responseModalities=\["AUDIO"]，并配置 speechConfig.voiceConfig.prebuiltVoiceConfig.voiceName，例如 Kore。多说话人请求中，每项 speechMetadata.speaker 须对应 speakerVoiceConfigs 配置，不要同时传单音色与多说话人配置。 |

## 请求示例

设置 MIXROUTE\_API\_KEY 环境变量。Python 示例仅使用标准库。

```bash theme={null}
curl --request POST "https://api.mixroute.ai/v1/models/gemini-3.8-flash-lite-tts:generateContent" \
  --header "Authorization: Bearer $MIXROUTE_API_KEY" \
  --header "Content-Type: application/json" \
  --data '{
  "contents": [
    {
      "parts": [
        {
          "text": "Welcome to MixRoute.",
          "speechMetadata": {
            "style": "Warm, clear and unhurried."
          }
        }
      ]
    }
  ],
  "generationConfig": {
    "responseModalities": [
      "AUDIO"
    ],
    "speechConfig": {
      "voiceConfig": {
        "prebuiltVoiceConfig": {
          "voiceName": "Kore"
        }
      }
    }
  }
}'
```

## 读取音频结果

读取 candidates\[].content.parts\[].inlineData.data 和 mimeType。Base64 解码后直接写入 WAV 文件，不要再次添加 WAV 头。Python 示例会拒绝缺失音频或非 WAV 数据，避免静默生成损坏文件。自定义音色、流式等厂商能力仍须路由单独支持。

```python theme={null}
import base64
import json
import os
from pathlib import Path
from urllib.request import Request, urlopen

payload = {
    "contents": [
        {
            "parts": [
                {
                    "text": "Welcome to MixRoute.",
                    "speechMetadata": {
                        "style": "Warm, clear and unhurried."
                    }
                }
            ]
        }
    ],
    "generationConfig": {
        "responseModalities": [
            "AUDIO"
        ],
        "speechConfig": {
            "voiceConfig": {
                "prebuiltVoiceConfig": {
                    "voiceName": "Kore"
                }
            }
        }
    }
}
request = Request(
    "https://api.mixroute.ai/v1/models/gemini-3.8-flash-lite-tts:generateContent",
    data=json.dumps(payload).encode("utf-8"),
    headers={
        "Authorization": "Bearer " + os.environ["MIXROUTE_API_KEY"],
        "Content-Type": "application/json",
    },
    method="POST",
)
with urlopen(request, timeout=120) as response:
    result = json.load(response)

audio = next((
    part["inlineData"]
    for candidate in result.get("candidates", [])
    for part in candidate.get("content", {}).get("parts", [])
    if part.get("inlineData", {}).get("mimeType", "").startswith("audio/")
), None)
if not audio:
    reasons = [c.get("finishReason") for c in result.get("candidates", [])]
    raise RuntimeError(f"No audio: finishReason={reasons}; promptFeedback={result.get('promptFeedback')}")
mime_type = audio["mimeType"].split(";", 1)[0].strip().lower()
if mime_type not in ("audio/wav", "audio/x-wav"):
    raise RuntimeError(f"Expected WAV, received {mime_type}")
wav = base64.b64decode(audio["data"], validate=True)
if wav[:4] != b"RIFF" or wav[8:12] != b"WAVE":
    raise RuntimeError("The audio data is not a WAV file")
Path("speech.wav").write_bytes(wav)
print("speech.wav")
```


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.