Streaming
Why Use Streaming
How It Works
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"The"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" capital"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" of"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" France"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" is"},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" Paris."},"finish_reason":null}]}
data: {"id":"chatcmpl-abc","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}
data: [DONE]Key Differences from Non-streaming
Aspect
Non-streaming
Streaming
The delta Object
delta ObjectGetting Usage Statistics
Code Examples
cURL
Python (OpenAI SDK -- Synchronous)
Python (OpenAI SDK -- Async)
Python (requests -- Manual SSE Parsing)
JavaScript (OpenAI SDK)
JavaScript (fetch -- Browser/Edge)
React (Next.js with Vercel AI SDK)
Streaming with Function Calls
Error Handling
Connection Errors
Incomplete Streams
Timeouts
Collecting the Full Response
Next Steps
Last updated