Skip to main content
Streaming delivers model output as it is generated, reducing time to first token. Yelu uses Server-Sent Events (SSE) for compatible Chat Completions and Responses requests.

Enable streaming

Set stream to true. For Chat Completions, each data: line contains a chat.completion.chunk. A terminal data: [DONE] marker ends the stream.

Event shape

Parser rules

  • Do not assume network chunks align with SSE lines; buffer until a newline.
  • Ignore blank lines and non-data: fields you do not use.
  • Concatenate text deltas in order.
  • Accumulate tool-call names and arguments across multiple deltas before parsing JSON.
  • Treat the response as incomplete until the terminal event arrives.
  • Handle UTF-8 with a streaming decoder so multibyte characters are not split incorrectly.
  • Cancel the request when the user leaves or the application no longer needs output.

Responses API events

The Responses API emits typed events rather than Chat Completions chunks. Read each event’s type, handle text delta events, and ignore unknown event types for forward compatibility. Store the final response object or usage event if your workflow needs it.
An HTTP 200 only confirms that the stream opened. Errors can still arrive inside the stream or the connection can end before a terminal event. Preserve partial text only if your product explicitly supports partial results.

Production checklist

  • Set separate connect, idle-read, and total request deadlines.
  • Disable proxy buffering for SSE routes you control.
  • Flush downstream data promptly when relaying a stream to a browser.
  • Bound output with max_completion_tokens or max_output_tokens.
  • Record time to first byte, time to first token, total duration, finish reason, and completion state.
  • Retry only if no irreversible downstream action was taken; a restarted stream generates a new response.
Last modified on July 13, 2026