Streaming

Server-sent events, chunk by chunk, with usage in the last chunk.

On this page

Set "stream": true on a Chat Completions request and the answer arrives as server-sent events while the model writes it.

Make a streamed request

curl

curl -N https://api.dipoleml.com/v1/chat/completions \  -H "Authorization: Bearer $RAVEN_API_KEY" \  -H "Content-Type: application/json" \  -d '{    "model": "Raven Max",    "stream": true,    "messages": [{"role": "user", "content": "Explain caching in three sentences"}]  }'

python

import osfrom openai import OpenAIclient = OpenAI(    base_url="https://api.dipoleml.com/v1",    api_key=os.environ["RAVEN_API_KEY"],)stream = client.chat.completions.create(    model="Raven Max",    stream=True,    messages=[{"role": "user", "content": "Explain caching in three sentences"}],)for chunk in stream:    if chunk.choices and chunk.choices[0].delta.content:        print(chunk.choices[0].delta.content, end="", flush=True)

The wire format

The answer has the type text/event-stream. Each event is one data: line holding a JSON chunk. Join choices[0].delta.content in order to rebuild the answer.

event stream (shape)
data: {"id":"<completion id>","object":"chat.completion.chunk","created":1794000000,"model":"Raven Max","choices":[{"index":0,"delta":{"role":"assistant","content":""},"finish_reason":null}]}data: {"id":"<completion id>","object":"chat.completion.chunk","created":1794000000,"model":"Raven Max","choices":[{"index":0,"delta":{"content":"Caching keeps"},"finish_reason":null}]}data: {"id":"<completion id>","object":"chat.completion.chunk","created":1794000000,"model":"Raven Max","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}data: {"id":"<completion id>","object":"chat.completion.chunk","created":1794000000,"model":"Raven Max","choices":[],"usage":{"prompt_tokens":412,"completion_tokens":96,"total_tokens":508,"prompt_tokens_details":{"cached_tokens":0}}}data: [DONE]
  • Usage comes last. Every stream ends with a chunk whose choices is empty and which carries usage, then data: [DONE]. You do not have to ask for it. Code that reads choices[0] must skip that chunk, as the Python sample does.
  • Reasoning may stream first. Some deltas carry the model's reasoning in reasoning_content, reasoning or reasoning_details instead of content. See Reasoning.
  • Tool calls stream in pieces. See streaming tool calls.

Errors inside a stream

A request that is refused before the model starts gets a normal error answer with its HTTP status (Errors and limits). Once the stream has started, the status is already 200, so a failure shows up in the stream itself: a chunk with an error object, then data: [DONE].

event stream (an interrupted answer)
data: {"error":{"message":"The response was interrupted. Try again.","type":"server_error","code":"stream_parse_error"},"model":"Raven Max"}data: [DONE]

Treat a stream as complete only when a chunk has carried a finish_reason. A stream that ends without one, or holds an error chunk, did not finish.

When to retry

Retry a stream only if no content has arrived yet: retry the request with a growing wait, up to three times. If part of the answer already arrived, show what you have and let the person ask again, since a second attempt starts the answer over.