Skip to content
docs

Streaming

To stream a response, set "stream": true. The router sends it as a Server-Sent Events (SSE) stream: tokens arrive as they are generated instead of in one block at the end. The stream format matches the endpoint. The /v1/responses stream is covered in Responses API.

POST /v1/chat/completions returns an OpenAI-compatible stream. Each event is a data: line with a chat.completion.chunk object, and the text is in choices[].delta.content. The stream ends with a data: [DONE] line.

Terminal window
curl https://api.waibee.com/v1/chat/completions \
-H "Authorization: Bearer $WAIBEE_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "model": "anthropic/claude-sonnet-4.6", "stream": true,
"messages": [{ "role": "user", "content": "Hello!" }] }'
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant","content":"Hello! How"}}]}
data: {"object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" are"}}]}
data: [DONE]

POST /v1/messages returns a stream of Anthropic-format events. Each event is a pair of event: and data: lines. The events come in this order: message_start, then content_block_start, a series of content_block_delta events with text, content_block_stop, message_delta, and message_stop. If the response has several blocks (reasoning, text, a tool call), the three content_block_* events repeat for each one. This format has no data: [DONE] line: the stream ends after message_stop.

Terminal window
curl https://api.waibee.com/v1/messages \
-H "x-api-key: $WAIBEE_API_KEY" \
-H "Content-Type: application/json" \
-d '{ "model": "anthropic/claude-sonnet-4.6", "stream": true, "max_tokens": 1024,
"messages": [{ "role": "user", "content": "Hello!" }] }'
event: message_start
data: {"type":"message_start","message":{"role":"assistant","content":[]}}
event: content_block_delta
data: {"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":"Hello! How"}}
event: message_stop
data: {"type":"message_stop"}

An error before the stream starts (API key, balance, validation, DLP, the model rejecting the request) comes as an ordinary HTTP response with a code from Errors.

Once the stream has started, the HTTP status stays 200, and the error arrives as an error event:

  • /v1/chat/completions: event: error, with a chunk in data that has an error field (code and message) and finish_reason: "error", followed by data: [DONE];
  • /v1/messages: event: error with the body {"type": "error", "error": {"type": ..., "code": ..., "message": ...}}. Use error.code to find the cause: one error.type covers several codes;
  • /v1/responses: see Responses API.
  • The stream can contain SSE comment lines (they start with :). These are keep-alives: the router sends them while the model is silent, for example while it reasons. Ignore them.
  • Which events carry token counters depends on the stream format; see Tokens and usage.
  • The official SDKs (openai, anthropic) parse the stream for you: turn on streaming in the call (stream=True), and the text arrives in parts. See Integrations.