Realtime TTS-2 is live. Built for realtime conversation that feels human. Read the Realtime TTS-2 announcement

Text generation

Streaming

Stream chat completions from Inworld Inference as server-sent events: chunk format, keepalive chunks, usage, tool calls, time limits, and how errors are reported mid-stream.

Set stream: true on a chat completion to receive the response as it is generated. The router uses server-sent events (SSE) in OpenAI's chat completions format. Image output can stream too; see Image output.

Stream a response

cURL
curl --no-buffer --request POST \
  --url https://api.inworld.ai/v1/chat/completions \
  --header "Authorization: Basic $INWORLD_API_KEY" \
  --header 'Content-Type: application/json' \
  --data '{
    "model": "openai/gpt-5.4",
    "messages": [{"role": "user", "content": "Write a haiku about routing."}],
    "stream": true
  }'

Stream format

Each event is a data: line with a JSON chunk, followed by a blank line. The stream ends with data: [DONE].

text
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","model":"openai/gpt-5.4","choices":[{"index":0,"delta":{"role":"assistant","content":null},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","model":"openai/gpt-5.4","choices":[{"index":0,"delta":{"content":"Requests"},"finish_reason":null}]}

data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","model":"openai/gpt-5.4","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}

data: [DONE]
FieldDescription
idThe same chatcmpl- identifier on every chunk of a response.
objectAlways chat.completion.chunk.
modelThe provider and model that served the request, which can differ from the one you asked for after routing or a fallback.
choices[].deltaThe new content since the last chunk: content, tool_calls, reasoning, audio, or images.
choices[].finish_reasonnull until the last chunk, then stop, length, tool_calls, or content_filter.
metadataOn the first chunk only: the request ID and the routing attempts that led to this model.

Keepalive chunks

Once a stream has started, the router sends a keepalive chunk whenever the model has sent nothing for 30 seconds, for example while it reasons without returning reasoning text. This keeps proxies and HTTP clients that close idle connections from ending the stream. A keepalive is an ordinary chunk with an empty delta and finish_reason: null, with the same id and model as the rest of the stream:

text
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","created":1763400000,"model":"openai/gpt-5.4","choices":[{"index":0,"delta":{},"finish_reason":null}]}

It carries no content, so skip any chunk whose delta has nothing to read. The OpenAI SDKs need no change: delta.content is None on such a chunk, as on the first and last chunks of every stream. If your code counts chunks, don't count these as content.

Keepalives start only after the first chunk. Before it, while the router is choosing a model and waiting for it to start, nothing is sent: the response has no status or headers yet. So a read or idle timeout just over 30 seconds is enough only once the stream has started. Before the first chunk, each model attempt can wait up to 90 seconds and fallbacks add more, up to the request's 10-minute limit. Set the read timeout for the first chunk, and the total timeout, to cover the time limits below, or set a time to first token timeout to fall back sooner.

Usage

Token usage is included in the stream whenever the provider reports it. You do not need stream_options to get it.

stream_options.include_usage changes only the shape of a chunk that carries usage and no content:

  • With "stream_options": {"include_usage": true}, its choices array is empty, as in OpenAI's API.
  • Without it, its choices array has one entry with an empty delta, so code that reads choices[0] on every chunk keeps working.

Read usage from whichever chunk includes it:

Python
for chunk in stream:
    if chunk.usage:
        print(chunk.usage.prompt_tokens, chunk.usage.completion_tokens)

Tool calls and reasoning

  • Tool calls arrive as delta.tool_calls fragments that you assemble by index. See Tool calling.
  • Reasoning text arrives in delta.reasoning for models that return it. See Reasoning.

Image output

Read generated images from choices[].delta.images[].image_url.url. Each entry contains an image data URL or a download URL; decode or download it rather than concatenating it with text deltas. Some models return progressive previews when you set image_config.partial_images. A preview is not an additional final image. See Image generation for previews and saving images.

Errors

How an error reaches you depends on when it happens:

  • Before the first chunk: you get a normal JSON error response with its HTTP status code, not a stream. Fallbacks run at this stage, so you only see an error if every model failed.
  • After the stream has started: the HTTP status is already 200. The router sends one data: {"error": {"message": "..."}} event and closes the stream without data: [DONE]. The router does not fall back to another model once the first chunk has been sent.

Treat a stream that contains an error event, or that ends without [DONE], as a failed request.

Time limits

These limits apply to every chat completion, streamed or not:

LimitWhat happens
On a streamed request (stream: true), the model sends no first chunk within 90 secondsThat attempt fails, and the router tries your next fallback. To fall back sooner, set a time to first token timeout.
After the stream has started, the model sends no data at all for 2 minutesThe stream is ended. Keepalive chunks from the router don't count as data from the model.
One model attempt runs for 5 minutes, counted from when the router sends it your requestThe response is ended, whether or not it is streamed.
The whole request runs for 10 minutes, counting every fallback attemptThe request is ended.

When the 5-minute or 10-minute limit ends a request before its first chunk, you get a 504 error with type: "timeout_error", and no further fallback is tried. Once the stream has started, any limit ends it with an error event and no data: [DONE], as described in Errors. Tokens the model generated before the response was ended can still be billed.

To get a long answer within these limits, lower max_tokens or the reasoning effort, or split the task into several requests. The OpenAI and Anthropic SDKs time out after 10 minutes by default, so their timeout does not end a request before these limits do.

Streaming on other APIs

  • Responses API streams typed events in the OpenAI Responses format. Its keepalive is an SSE comment line, : keepalive.
  • Anthropic Messages API streams in Anthropic's event format. Its keepalive is a ping event, as on Anthropic's API.
  • Voice responses add audio chunks to the chat completions stream.

Next steps