Skip to main content
By default, the Swytcho API waits until the model finishes generating before returning a response. Streaming changes that: the API sends each token as it is produced using server-sent events (SSE), so your application can begin displaying output immediately. This dramatically reduces perceived latency for long responses and is essential for building chat interfaces that feel fast and alive.

How Streaming Works

Set "stream": true in your request body. The API responds with a stream of newline-delimited events, each prefixed with data: . Each event contains a partial response chunk in JSON. When generation is complete, the stream sends a final data: [DONE] sentinel and closes. A raw stream looks like this:
Each delta.content field contains the next piece of text. Concatenate them in order to reconstruct the full response.

Code Examples

Streaming Response Format

Each chunk follows this structure:
The final chunk before [DONE] sets finish_reason to "stop", "length", or another terminal value — the same reasons as non-streaming responses. Use this to detect truncation.

End of Stream

The stream terminates with:
Always check for this sentinel explicitly in your parsing loop. Do not rely solely on the connection closing, as network proxies can sometimes close the stream prematurely.

Error Handling

Errors that occur before streaming begins are returned as standard HTTP error responses (4xx or 5xx) with a JSON body. Errors that occur mid-stream are delivered as a final SSE event with an error field instead of choices.
Python
Streaming is supported for the /v1/chat/completions and /v1/completions endpoints. The /v1/embeddings endpoint does not support streaming — embedding requests always return a complete response in a single JSON payload.