Streaming Responses
Chat Completions streaming request
Section titled “Chat Completions streaming request”Set stream: true to receive incremental events in SSE format.
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 } }'SSE event shape
Section titled “SSE event shape”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]Client handling suggestions
Section titled “Client handling suggestions”- Parse each SSE
data:line independently. - Close the stream after receiving
[DONE]. - Pass
stream_options.include_usage=trueif the client needs usage details. - If the client disconnects, the request may still complete and generate usage or billing.
Python SDK example
Section titled “Python SDK example”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="")