Event Streams for API Clients
Sections What the format guarantees Every format has an explicit end Errors after a 200 Partial tool calls Recovering

Streams that end early

A streamed response can stop at any point: the client's own read timeout fires, the network drops, the server restarts, or the service reports an error after it had already started answering. The client has to tell a stream that finished from one that merely stopped, and it has to do that without trusting the last half event it received.

What the format guarantees

The event stream specification says that when the body ends in the middle of an event, before its final empty line, that event is discarded. A parser that follows the rule never hands over half a JSON payload. A parser that flushes its buffer at the end of the body does, and the resulting JSON error then hides the real problem, which is that the connection ended.

Every format has an explicit end

Because a dropped connection and a finished response both end the body, each streaming API marks the real end with something the client can check:

FormatMarker that the response is complete
OpenAI Chat CompletionsA final data: [DONE] line
OpenAI ResponsesA response.completed event; a response.failed event marks failure
Anthropic MessagesA message_stop event

The details of each are in the format comparison. The rule is the same for all of them: if the body ends and the marker never arrived, treat the response as incomplete, however much text came through.

Errors after a 200

The status code is sent before the first event. If something goes wrong later, the server cannot change it, so the error arrives as an event inside the stream. Anthropic's streaming documentation shows an error event carrying an object with type and message, for example an overloaded_error; its error reference points out that such errors do not follow the usual status-code handling. The Responses API has an error event of its own, with code, message and param fields.

A client therefore needs two error paths: one for the status line, and one inside the event loop that turns an error event into the same exception the status path would raise.

Partial tool calls

Tool-call arguments are streamed as fragments of a JSON string. Anthropic calls them input_json_delta events with a partial_json field; Chat Completions sends pieces of function.arguments. The fragments only form valid JSON once the call is complete. If the stream stops in the middle, the arguments must be thrown away, not repaired and executed. Executing a guessed argument list is how a cut connection turns into a wrong action.

Recovering