Enable streaming
SetBASETEN_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.
- Chat Completions
- Responses
- Anthropic Messages
- Custom Truss
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.Inspect server-sent events with cURL
To inspect the Chat Completions event stream: Run the following command.--no-buffer displays events as they arrive:
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 asresponse.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.