Three vendor stream formats
All three formats below are carried over the same server-sent events framing, so one parser serves them all. What differs is the content of each event: where the text is, how tool calls are split, where usage numbers appear and how the end is marked. The field names here are taken from each vendor's documentation, linked in every section; check there for anything newer.
OpenAI Chat Completions
Described in the Chat Completions reference.
- Each event's
datais a JSON chunk whoseobjectischat.completion.chunk. The events carry noeventfield. - Text arrives in
choices[].delta.content; tool calls arrive inchoices[].delta.tool_calls, keyed by anindex, with the function name in the first fragment and the arguments spread as string pieces over later ones. finish_reasonis set on the choice when generation for it stops.- The stream ends with the literal line
data: [DONE], which is not JSON and must be checked before parsing. - With
stream_optionsset to include usage, one more chunk is sent before[DONE]. Itschoicesarray is always empty and itsusagefield holds the totals for the whole request; on earlier chunksusageis null. The reference warns that this final chunk may not arrive if the stream is interrupted.
OpenAI Responses
Described in the Responses streaming events reference.
- Every event's JSON has a
typenaming the event, for exampleresponse.createdat the start. - Text arrives as
response.output_text.deltaevents, with the new text indeltaalongsideitem_id,output_index,content_indexandsequence_number. - Function-call arguments arrive as
response.function_call_arguments.deltaevents, again with the fragment indelta. - The stream ends with
response.completed, or withresponse.failedwhen the response failed. A separateerrorevent carriescode,messageandparam. - Events carry a
sequence_number, which makes gaps and reordering easy to detect in logs.
Anthropic Messages
Described in the streaming documentation.
- The sequence is
message_start, then for each content block acontent_block_start, any number ofcontent_block_deltaevents and acontent_block_stop, then one or moremessage_deltaevents and finallymessage_stop.pingevents can appear anywhere. - The delta inside
content_block_deltahas its own type, for exampletext_deltawith atextfield,input_json_deltawith apartial_jsonfield for tool input, andthinking_deltawith athinkingfield. A thinking block also gets asignature_deltajust before it stops, and optional features add types of their own, such ascitations_deltawhen citations are enabled. Skip delta types you do not handle. message_deltacarries the stop reason and ausageobject. The documentation stresses that its token counts are cumulative, so the last value is the total; adding them up counts tokens twice.- Errors during the stream arrive as an
errorevent. The documentation also says new event types may be added, so unknown types should be skipped, not treated as failures.
Side by side
| Question | Chat Completions | Responses | Messages |
|---|---|---|---|
| How to recognise an event | Shape of the JSON chunk | type field | event line and type field |
| New text | choices[].delta.content | response.output_text.delta | text_delta in content_block_delta |
| Tool arguments | delta.tool_calls[].function.arguments pieces | response.function_call_arguments.delta | input_json_delta pieces |
| Usage | Optional final chunk with empty choices | In the completed response | Cumulative, in message_delta |
| End marker | data: [DONE] | response.completed | message_stop |
| Error in stream | Not covered here; check the reference | error event | error event |
Supporting more than one
Normalise early. Convert each vendor's events into a small internal set (text added, tool arguments added, usage updated, finished, failed) right after parsing, and let the rest of the application see only those. The parser from earlier stays shared; only one function per vendor knows the field names above, and when a vendor adds an event type, that function is the only place to change.