Streaming text and detecting failure
Consume Chat Completions SSE and recognize partial or failed output.
Also available as Markdown · all agent guides in one file
Key points
- HTTP 200 only means the stream started. Check every JSON event for
errorbefore accepting text. [DONE]alone does not prove success. A disconnect before the protocol finishes is incomplete output.- Execute streamed tool calls only after the complete call has arrived and the turn has finished successfully.
For a chat model that supports streaming, set stream: true on POST /v1/chat/completions. The response uses text/event-stream. This example requires curl and jq, plus AETHER_API_KEY and an exact AETHER_MODEL chosen from the catalogue.
jq -n --arg model "$AETHER_MODEL" \
'{model: $model, messages: [{role: "user", content: "Say hello briefly."}], stream: true}' \
| curl --no-buffer --fail-with-body --silent --show-error \
https://api.aetherapi.dev/v1/chat/completions \
-H "Authorization: Bearer $AETHER_API_KEY" \
-H 'Content-Type: application/json' --data-binary @-Client parsing rules
- Use an SSE parser that buffers across network chunks, decodes UTF-8 incrementally and dispatches events at blank lines.
- A TCP/read chunk is not an entire event. Ignore comment/keepalive lines.
- For Chat Completions, parse
dataJSON, readchoices[].delta.contentand track each choice separately. - Some events contain role or usage only, or
choices: []; do not assume content is present. - If tools are streamed, accumulate
delta.tool_callsby index, joining fragmentedfunction.nameandfunction.arguments. - Parse arguments and execute only after the complete tool call has arrived and the turn has finished successfully.
Detect failure
- Check every JSON event for
errorbefore accepting text. HTTP 200 only means the stream started. - A live chat failure can contain
data: {"error": {...}}followed bydata: [DONE];[DONE]alone does not prove success. - A disconnect before the protocol finishes is incomplete output. Keep it marked partial, do not execute tools from it, and do not splice a retried answer onto it.
finish_reason: lengthalso means the answer was truncated.
Other protocols and testing
/v1/responsesand/v1/messagesuse their own named SSE events.- Responses errors can appear as
event: errororresponse.failed; Anthropic usesevent: error. - Use a parser for the chosen protocol rather than treating these as chat delta objects.
- Do not assume that switching endpoints makes streaming available for every model.
For deterministic parser checks, use the enabled sandbox's success and stream_error scenarios. stream_error emits partial text or tool-call arguments, then sandbox_simulated_stream_error without a successful completion (no [DONE] on chat). See the sandbox document before testing; no production enablement is assumed.