Skip to content
Main Site News Console

Streaming Output

Set "stream": true in the request, and the response will be returned in chunks via Server-Sent Events(SSE), which is suitable for typing effects in chat interfaces and long-response scenarios.

Request

Terminal window
curl https://api.4allapi.com/v1/chat/completions \
-H "Content-Type: application/json" \
-H "Authorization: Bearer $AIPROXY_KEY" \
-d '{
"model": "gpt-5.6",
"messages": [{"role": "user", "content": "Write a short poem about the starry sky"}],
"stream": true,
"stream_options": {"include_usage": true}
}'

Response Stream

Each event is a line with a data: prefix containing JSON, with incremental content in choices[0].delta.content; when the stream ends, data: [DONE] is sent:

data: {"choices":[{"delta":{"content":"夜"}}], ...}
data: {"choices":[{"delta":{"content":"空"}}], ...}
...
data: {"choices":[],"usage":{"prompt_tokens":18,"completion_tokens":56,"total_tokens":74}}
data: [DONE]

SDK Example(Python)

stream = client.chat.completions.create(
model="gpt-5.6",
messages=[{"role": "user", "content": "Write a short poem about the starry sky"}],
stream=True,
)
for chunk in stream:
delta = chunk.choices[0].delta.content if chunk.choices else None
if delta:
print(delta, end="", flush=True)

Notes

  • It is recommended to set the read timeout for streaming connections to above 60 seconds; for reasoning models with long responses, you may increase it further;
  • After the client disconnects, any already generated content will still be billed based on actual usage;
  • When forwarding through a proxy/gateway, response buffering must be disabled (for example, proxy_buffering off in nginx), otherwise streaming will not work.