Skip to main content
Streaming returns model output incrementally so your application can display text before generation finishes. Choose the protocol exposed by your endpoint: Model APIs support OpenAI Chat Completions, OpenAI Responses, and Anthropic Messages; custom Truss models can yield raw chunks. For real-time transcription over WebSocket, use the speech quickstart.

Enable streaming

Set BASETEN_API_KEY to your Baseten API key. Install the dependencies listed in your selected tab. The Model APIs examples use a hosted model; custom Truss streaming requires a deployed model whose code yields output.
To stream a response:Install openai, httpx, and httpx-sse with pip install openai httpx httpx-sse. Run the following script. It reads raw server-sent events so it can check both [DONE] and the finish reason.
The text arrives incrementally; the output blocks show the final terminal output. Model wording and chunk boundaries can vary.

Inspect server-sent events with cURL

To inspect the Chat Completions event stream: Run the following command. --no-buffer displays events as they arrive:
The stream contains data: records and ends with data: [DONE]. Check the preceding chunk’s finish_reason as well. For more information, see Chat Completions streaming.

Handle incomplete responses

For Chat Completions and Messages, check the terminal event and stop reason before accepting the text. A token limit can end generation with a completion marker while leaving an unfinished answer. Responses reports this as response.incomplete, with the cause in incomplete_details.reason. The SDK procedures above raise an error if their terminal event is missing. The custom Truss client reads raw response chunks, which have no standard model-completion marker. If your application needs to distinguish complete output from an interrupted generation, define a terminal record in your model’s streaming format and check it in the client. cURL displays the wire format without validating it. Transport and API errors can occur after HTTP 200. Keep partial text separate from a retried generation. For more information, see completion markers and recovery, read timeouts, and bounded retries.

Time to first token

Streaming changes when the caller sees output. Time to first token (TTFT) measures how long the caller waits before receiving the first generated token; total generation time includes the rest of the response. The diagram compares streaming and non-streaming delivery for the same illustrative generation. Both finish together; the streaming client can display partial output earlier. These timings are illustrative, not measured latency.