Skip to content
Open
Prev Previous commit
Next Next commit
ai/openai: rebuild streamed chat completions
Give the chat transport the stream assembler the Responses transport
has. A streamed request also asks for stream_options.include_usage,
without which a stream reports no token counts.

Each chunk's deltas are folded into the choice they name. Text fields
are concatenated. A tool call delta continues the call its index names,
takes its id, type and name from the first delta carrying them, and
concatenates its argument fragments; a repeated name or type must
match. Some servers give every call index 0 or leave the index out and
send each call whole, so a delta naming a new id starts a new call and
one without an index continues the last call. finish_reason, usage and
service_tier keep the last value sent. Text that never arrived is left
out of the rebuilt message, as a plain reply that only calls tools has
no content while the first chunk of its stream carries an empty one.

The stream ends with data: [DONE]. One that ends without it is still
complete when every choice has finished, other than with "error", as
some servers close after the last chunk; anything else was cut off and
is transient.

An error object inside the stream fails the way the HTTP status it
names would, given as a number, a numeric string, or a kind such as
server_error, which OpenAI sends with a null code. An error none of
these place is fatal. With streaming on, extra_body may not set stream
or stream_options.

The client streams only through with_stream, so nothing changes until a
setting enables it.

Link: #947
Signed-off-by: Shang En Sim <sim@shangen.org>
  • Loading branch information
12458 committed Nov 26, 2026
commit 20c7e24427929b3a1cfdd893f6a7ec42496b9aea
Loading