Routeway supports Server-Sent Events (SSE) streaming using the same format as the OpenAI API. Any client that works with OpenAI streaming will work with Routeway out of the box.
How It Works
Set"stream": true in your request. The response will be delivered as a series of data: lines, each containing a JSON chunk. The stream ends with data: [DONE].
delta object instead of a full message. Text arrives through delta.content. The final chunk sets finish_reason to indicate why the model stopped.
Basic Example
- Python
- Node.js
- cURL
Streaming with Tool Calls
When a model decides to call a tool, arguments arrive incrementally throughdelta.tool_calls. Accumulate the chunks before executing the function.
Include Usage Stats
To receive token usage at the end of a stream, passstream_options:
usage object after [DONE]:
Handling Errors in Streams
Errors can appear mid-stream as a regular SSE data frame containing anerror key. Always check for this before assuming a chunk contains choices.
Best Practices
Flush output immediately
Use
flush=True (Python) or process.stdout.write (Node.js) so tokens render in real time.Handle reconnects
Network interruptions can cut streams short. Track the last received content and retry if needed.
Accumulate tool args
Tool call arguments arrive in fragments. Always concatenate before calling
JSON.parse.Check finish_reason
length means the model hit max_tokens. stop means a natural end. tool_calls means a function was requested.