Enabling Streaming
Setstream: true in your request body to switch from a single JSON response to a stream of SSE events. The OpenAI SDK handles the underlying SSE protocol for you, exposing each chunk as an iterable object.
SSE Format
When streaming is enabled, Meliai sends the response as a sequence of server-sent events over the HTTP connection. Each event is a line prefixed withdata: followed by a JSON object. The stream is terminated by the special sentinel data: [DONE].
A single chunk looks like this:
data: [DONE] will have finish_reason set to "stop" (or another stop reason) and an empty delta.content. If you are parsing the raw SSE stream yourself, skip any line that does not start with data: and stop processing when you encounter data: [DONE].
Getting Usage in Streaming Mode
By default, token usage is not included in streaming responses. To receive a final chunk containing prompt and completion token counts, setstream_options.include_usage to true.
Python
finish_reason chunk and before data: [DONE]. Its choices array is empty, and it carries a populated usage field identical in structure to a non-streaming response.