Skip to Content
unii.docs
Api

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

POST/v1/chat/completions

Streaming 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_here

Request body

The request body accepts the same parameters as Chat completions, with stream set to true:

ParameterTypeDescription
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.
temperaturenumber
Controls randomness. Range: 0 to 2. Default: 1.
max_tokensinteger
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:

chunk.json
{"id": "chatcmpl-abc123","object": "chat.completion.chunk","choices": [  {    "index": 0,    "delta": {      "role": "assistant",      "content": "RAG"    },    "finish_reason": null  }]}
FieldTypeDescription
idstringThe completion ID
objectstringAlways chat.completion.chunk
choices[index].deltaobjectThe new content fragment
choices[index].finish_reasonstringnull 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

On this page