How do I stream a response from the Leiolai API?
Set stream to true on a supported finite Leiolai API request and read server-sent events using the rules for that endpoint. Leiolai Chat Completions, Responses, Messages, and text Completions use different event shapes and completion markers. Append delivered text, handle errors, and keep the endpoint’s terminal signal.
Read the Leiolai API reference
How do I append a Chat Completions answer?
Append choices[0].delta.content from each Leiolai Chat Completions text chunk. data: [DONE] ends the stream. A short answer may arrive in one chunk, so support both a single chunk and multiple chunks.
Chat Completions response fieldsHow do I get usage at the end of a chat stream?
Set stream_options.include_usage: true in the Leiolai request. The final usage-only chunk has an empty choices array and arrives before [DONE]; earlier chunks have usage: null. Handle the usage chunk separately so your text parser does not access choices[0] when choices is empty.
Understand input and output countsHow does a Responses stream differ?
Leiolai Responses sends named events. Append delta from response.output_text.delta, and read response.function_call_arguments.delta for function arguments. The terminal event is response.completed, response.incomplete, or response.failed. This format does not use a [DONE] marker.
Handle Responses output itemsHow does a Messages stream end?
Leiolai Messages begins with message_start. Append delta.text from content_block_delta when its type is text_delta. message_delta carries the stop reason and usage; message_stop ends the stream. Messages does not use [DONE].
Messages events and content blocksHow do I stream text Completions?
Append choices[0].text from each data chunk. The terminal chunk has empty text and a finish_reason, followed by data: [DONE]. Use this shape for /v1/completions rather than the Leiolai Chat Completions delta shape.
Use a text prompt and responseHow should a client handle streamed function arguments?
Collect all argument fragments for a Leiolai function call before parsing the JSON or running the function. Group fragments by the call identifier or index supplied by your endpoint. Preserve each call ID when sending the result back.
Complete a function-call round tripCan a stream fail after HTTP 200?
Yes. Once a stream has begun, the HTTP status remains 200. Continue reading the events for failure or refusal signals and inspect the terminal status. A finite Leiolai Chat Completions response can finish with content_filter; Responses can send response.failed.
Handle stream failuresIs a normal stream the same as continuous generation?
A finite stream delivers one response with a completion marker or terminal event. Experimental Leiolai continuous generation has a separate endpoint and permits new context while the response continues. Use its own event parser and session controls.
Start a continuous session