> ## 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 streaming generation

Identical to [generateContent](/en/api-reference/gemini/generate-content), but **returned as a stream** — chunks are pushed as they're generated, reducing time-to-first-token.

## Request

```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": "Tell a 100-word story"}]}]
  }'
```

Note the query parameter `?alt=sse` — without it, the response is a JSON array (one complete candidate per item);
**only with `alt=sse` do you get a standard SSE stream** (the `data: {...}` form).

## Stream chunks

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

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

...

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

The last chunk usually carries `finishReason` and `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="Tell a 100-word story",
)
for chunk in stream:
    print(chunk.text, end="", flush=True)
```

## Notes

* Streaming responses also support `tools` / function calling / thinking mode — those fields appear within some chunk.
* The client must handle retries after an SSE disconnect (it is not idempotent — a retry is billed again).
