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

# Gemini 流式对话示例

> Gemini `streamGenerateContent` 的流式对话示例。

## 推荐 endpoint

* [Gemini /v1beta/models/{model}:streamGenerateContent](/api-reference/endpoints/gemini/text-stream-generate-content)

## 最小请求

```json theme={null}
{
  "model": "gemini-2.5-flash",
  "contents": [
    {
      "role": "user",
      "parts": [{ "text": "边生成边解释 SSE 流式输出。" }]
    }
  ]
}
```

## cURL 示例

```bash theme={null}
curl "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: $GEMINI_API_KEY" \
  -H "Content-Type: application/json" \
  -N \
  -d '{
    "contents": [
      {
        "role": "user",
        "parts": [{ "text": "边生成边解释 SSE 流式输出。" }]
      }
    ]
  }'
```

## Python 示例

```python theme={null}
from google import genai

client = genai.Client(api_key="<GEMINI_API_KEY>")

stream = client.models.generate_content_stream(
    model="gemini-2.5-flash",
    contents="边生成边解释 SSE 流式输出。",
)

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

## Node.js 示例

```js theme={null}
import { GoogleGenAI } from "@google/genai";

const ai = new GoogleGenAI({ apiKey: process.env.GEMINI_API_KEY });

const stream = await ai.models.generateContentStream({
  model: "gemini-2.5-flash",
  contents: "边生成边解释 SSE 流式输出。"
});

for await (const chunk of stream) {
  process.stdout.write(chunk.text || "");
}
```

## 最佳实践

* 逐段累积 `parts[].text`，不要假设每个 chunk 都是完整句子
* 流式和结构化输出可以组合，但建议由服务端统一处理 partial JSON
* 对延迟敏感的场景优先选 Flash 类模型
