Skip to content
Home

Streaming Responses

Set stream: true to receive incremental events in SSE format.

Terminal window
curl -N "${BASE_URL}/v1/chat/completions" \
-H "Authorization: Bearer ${API_KEY}" \
-H "Content-Type: application/json" \
-d '{
"model": "gpt-4.1",
"messages": [
{
"role": "user",
"content": "Output three troubleshooting suggestions in sequence"
}
],
"stream": true,
"stream_options": {
"include_usage": true
}
}'
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":"First"}}]}
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{"content":" suggestion"}}]}
data: {"id":"chatcmpl_xxx","object":"chat.completion.chunk","choices":[{"delta":{},"finish_reason":"stop"}],"usage":{"prompt_tokens":20,"completion_tokens":30,"total_tokens":50}}
data: [DONE]
  • Parse each SSE data: line independently.
  • Close the stream after receiving [DONE].
  • Pass stream_options.include_usage=true if the client needs usage details.
  • If the client disconnects, the request may still complete and generate usage or billing.
stream = client.chat.completions.create(
model="gpt-4.1",
messages=[{"role": "user", "content": "Output three troubleshooting suggestions in sequence"}],
stream=True,
stream_options={"include_usage": True},
)
for chunk in stream:
if chunk.choices and chunk.choices[0].delta.content:
print(chunk.choices[0].delta.content, end="")