Stream response
Stream chat completion responses in real time using Server-Sent Events (SSE).
Overview
The stream response endpoint returns chat completions in real time using Server-Sent Events (SSE). Instead of waiting for the full response, you receive chunks as they are generated.
Endpoint
/v1/chat/completionsStreaming uses the same endpoint as regular chat completions. Set stream: true in the request body.
Authentication
This endpoint requires a Bearer token in the Authorization header:
Authorization: Bearer sk_unii_your_api_key_hereRequest body
The request body accepts the same parameters as Chat completions, with stream set to true:
| Parameter | Type | Description |
|---|---|---|
model* | string | The model to use. Use unii-latest for the current default. |
messages* | array[object] | An array of message objects with role and content. |
stream* | boolean | Must be set to true to enable streaming.This is the only difference from the regular chat completions endpoint. |
temperature | number | Controls randomness. Range: 0 to 2. Default: 1. |
max_tokens | integer | Maximum number of tokens to generate. |
Request examples
curl -X POST https://api.unii.dev/v1/chat/completions \-H "Authorization: Bearer $UNII_API_KEY" \-H "Content-Type: application/json" \-d '{ "model": "unii-latest", "messages": [ { "role": "user", "content": "Explain RAG in detail." } ], "stream": true}'Response format
The response is an SSE stream. Each event is prefixed with data: and contains a JSON chunk:
data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"role":"assistant"},"finish_reason":null}]}data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":"RAG"},"finish_reason":null}]}data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{"content":" stands"},"finish_reason":null}]}data: {"id":"chatcmpl-abc123","object":"chat.completion.chunk","choices":[{"index":0,"delta":{},"finish_reason":"stop"}]}data: [DONE]Chunk structure
Each chunk contains:
{"id": "chatcmpl-abc123","object": "chat.completion.chunk","choices": [ { "index": 0, "delta": { "role": "assistant", "content": "RAG" }, "finish_reason": null }]}| Field | Type | Description |
|---|---|---|
id | string | The completion ID |
object | string | Always chat.completion.chunk |
choices[index].delta | object | The new content fragment |
choices[index].finish_reason | string | null during streaming, "stop" on the last chunk |
End of stream
The stream ends with a final chunk containing finish_reason: "stop" followed by data: [DONE].
Note
The final chunk may have an empty delta object. Use
finish_reason to detect the end of the stream.
Errors
If an error occurs during streaming, the SDK throws an exception. With raw HTTP, the connection closes with an error status code.
Important
The streaming protocol details shown on this page are illustrative. Verify the actual event format and chunk structure against the latest SDK documentation before using them in production.
Next steps
- Read the Chat completions endpoint for non-streaming requests.
- Try the Streaming guide for implementation examples.
- See the API Overview for a full list of endpoints.