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

# Gemini 流式接口

> Google Gemini streamGenerateContent API 兼容格式

使用 Gemini 原生 `streamGenerateContent` API 以流式方式调用 Gemini 模型，适合需要边生成边展示的聊天、Agent 和长文本生成场景。

<RequestExample>
  ```bash cURL theme={null}
  curl -N -X POST "https://gogogotoken.ai/v1beta/models/gemini-2.5-pro:streamGenerateContent" \
    -H "Authorization: Bearer YOUR_API_KEY" \
    -H "Content-Type: application/json" \
    -d '{
      "contents": [{
        "role": "user",
        "parts": [{ "text": "Write a haiku about the sea." }]
      }]
    }'
  ```
</RequestExample>

<ResponseExample>
  ```json 200 theme={null}
  {
    "candidates": [
      {
        "content": {
          "role": "model",
          "parts": [
            { "text": "Blue waves breathe softly" }
          ]
        }
      }
    ]
  }
  ```
</ResponseExample>

## 接口说明

该接口保留 Gemini 原生流式请求结构。响应可能表现为 JSON 流或 SSE，具体取决于客户端与上游通道。

## 路径参数

<ParamField path="model" type="string" required default="gemini-2.5-pro">
  Gemini 模型 ID，例如 `gemini-2.5-pro`、`gemini-2.0-flash`。完整列表以 `GET /v1/models` 返回结果为准。
</ParamField>

## 请求头

<ParamField header="Authorization" type="string" required>
  API Key 鉴权信息，格式为 `Bearer YOUR_API_KEY`。
</ParamField>

<ParamField header="Content-Type" type="string" required>
  固定为 `application/json`。
</ParamField>

## 请求体

<ParamField body="contents" type="object[]" required default={[{ role: "user", parts: [{ text: "Write a haiku about the sea." }] }]}>
  对话内容数组。

  <ParamField body="role" type="string">
    消息角色，常用值为 `user` 或 `model`。
  </ParamField>

  <ParamField body="parts" type="object[]" required>
    内容片段数组，可包含文本、图片、文件或工具相关内容。
  </ParamField>
</ParamField>

<ParamField body="generationConfig" type="object">
  生成配置，例如 `temperature`、`maxOutputTokens`、`responseModalities`。
</ParamField>

<ParamField body="tools" type="object[]">
  Gemini 原生工具定义。
</ParamField>

## 响应体

<ResponseField name="candidates" type="object[]">
  模型候选输出片段数组。文本通常位于 `candidates[0].content.parts[0].text`。
</ResponseField>

<ResponseField name="usageMetadata" type="object">
  token 用量统计。部分流式片段中可能不会返回该字段。
</ResponseField>

## 错误码

| 状态码   | 含义                | 处理建议            |
| ----- | ----------------- | --------------- |
| `401` | API Key 无效或未传     | 检查请求头与令牌状态      |
| `403` | 无权限访问目标 Gemini 模型 | 检查 API Key 分组权限 |
| `429` | 触发限速              | 降低请求频率或调整套餐     |
| `5xx` | 上游或网关异常           | 稍后重试，并查看控制台日志   |
