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

# streamGenerateContent

> POST /v1beta/models/{model}:streamGenerateContent —— Gemini 流式生成

与 [generateContent](/api-reference/gemini/generate-content) 完全一致,但 **以流式形式返回** —— 边生成边推送 chunk,降低首字延迟。

## 请求

```bash theme={null}
curl "https://openp.ai/v1beta/models/gemini-3.1-pro-preview:streamGenerateContent?alt=sse" \
  -H "x-goog-api-key: $OPENPAI_API_KEY" \
  -H "Content-Type: application/json" \
  -d '{
    "contents": [{"role": "user", "parts": [{"text": "讲个 100 字故事"}]}]
  }'
```

注意 query 参数 `?alt=sse` —— 没有它时返回的是 JSON 数组(每条一个完整 candidate),
**带上 `alt=sse` 才返回标准 SSE 流**(`data: {...}` 形式)。

## 流式块

```
data: {"candidates":[{"content":{"role":"model","parts":[{"text":"在"}]},"index":0}]}

data: {"candidates":[{"content":{"role":"model","parts":[{"text":"一个"}]},"index":0}]}

...

data: {"candidates":[{"finishReason":"STOP","index":0}],"usageMetadata":{...}}
```

最后一块通常带 `finishReason` 与 `usageMetadata`。

## Python(google-genai)

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

client = genai.Client(
    api_key="sk-...",
    http_options={"base_url": "https://openp.ai"},
)

stream = client.models.generate_content_stream(
    model="gemini-3.1-pro-preview",
    contents="讲个 100 字故事",
)
for chunk in stream:
    print(chunk.text, end="", flush=True)
```

## 注意

* 流式响应中也支持 `tools` / 函数调用 / 思考模式 —— 字段会出现在某个 chunk 内。
* 客户端要处理 SSE 连接断开后的重试(不可幂等,重试会重新计费)。
