skip to content

How would you design a model gateway's single stream format over providers that emit text deltas, tool-argument fragments, usage and mid-stream errors differently?

level: seniorimportance: should knowfreq 33%

answer

  1. typed events versus flat chunks
  2. own vocabulary, one terminal event
  3. translator is a per-stream state machine
  4. usage and finish before done
  5. status 200 is not success

basics

~20 s

Define the gateway's own event vocabulary with exactly one terminal event, translate each provider stream through a per-stream state machine, merge usage and finish into final events, and report mid-stream failures as in-stream errors, never as a clean finish.

solid answer

~40 s

Providers stream in two broad styles: typed event sequences that open, fill and close content parts, and flat chunks that each carry a delta for a choice, with the finish reason and usage arriving late or only on request. The gateway defines its own vocabulary: a start event with the resolved model, text deltas, a tool-call start, tool-argument fragments, part end, finish, usage, and exactly one terminal event, done or error. A per-stream state machine tracks open parts, presents a tool call delivered whole as start, one fragment and end so clients have one path, and merges usage reported at start and end into one final event. Once bytes flow, HTTP 200 is already sent, so a provider error or a dropped connection becomes an in-stream error event, never a finish.

code

json · 12 lines
json
[
  {"event": "start", "request_id": "gw-7f3a", "resolved_model": "provider-a-model-2026-06"},
  {"event": "text_delta", "part": 0, "text": "Checking your order"},
  {"event": "part_end", "part": 0},
  {"event": "tool_call_start", "part": 1, "call_id": "gw_t2_0", "name": "get_order"},
  {"event": "tool_args_delta", "part": 1, "fragment": "{\"order\": \"A1"},
  {"event": "tool_args_delta", "part": 1, "fragment": "7\"}"},
  {"event": "part_end", "part": 1},
  {"event": "finish", "reason": "tool_call", "raw": "provider-specific-value"},
  {"event": "usage", "input_total": 950, "output_total": 41, "status": "reported"},
  {"event": "done"}
]

go deeper

for a junior

Recall that providers stream deltas as server-sent events and that the gateway presents one stream format to every caller.

for a middle

Explain the typed-event and flat-chunk styles, the gateway's event vocabulary, and why tool arguments are complete only at part end.

for a senior

Design the per-stream state machine, merged usage, exactly one terminal event, in-stream errors after status 200, and chunk-boundary replay tests.

for a principal

Weigh how strict the stream contract is, and how much provider-specific stream detail the platform exposes versus hides as providers add new event types.

## Two families of provider stream Streaming responses usually travel as **server-sent events** over one HTTP response, but what the events mean differs: - **Typed event sequences.** The stream announces a message, opens a content part (text or a tool call), sends deltas into it, closes it, then sends a final event with the finish reason and usage. - **Flat chunks.** Every chunk has the same shape and carries a delta for one choice index: a text fragment, or a fragment of a tool call's arguments keyed by position. The finish reason comes in a late chunk; usage arrives in a final chunk, sometimes only when the request asked for it. Clients should not have to know which family a provider belongs to. The gateway's stream format is the contract. ## A gateway event vocabulary | event | carries | rule | |---|---|---| | `start` | gateway request id, resolved model | first event, exactly once | | `text_delta` | part index, text | UTF-8 complete; never splits a character | | `tool_call_start` | part index, call id, tool name | before any argument fragment | | `tool_args_delta` | part index, argument fragment as text | concatenated in order | | `part_end` | part index | arguments complete only after this | | `finish` | normalised reason, raw provider value | once, after the last part | | `usage` | normalised counters, status | once, after finish | | `done` / `error` | (error: normalised code, message) | exactly one terminal event | (Event names are illustrative.) The guarantees matter more than the names: one start, parts that open and close, finish and usage before the end, and **exactly one terminal event**. ## The translator is a state machine A provider chunk does not map one-to-one onto a gateway event, so each stream gets a small state machine: 1. **Track open parts.** Flat-chunk providers often do not say "part ended"; the translator closes a part when a delta for a different part arrives or when the finish reason appears. 2. **Normalise tool calls.** A provider that delivers a tool call whole is presented as `tool_call_start`, one `tool_args_delta` and `part_end`, so clients have one code path. Arguments are forwarded as text fragments and are valid JSON only once the part ends; acting on them earlier is a separate, well-known trap. 3. **Do not buffer the response.** Text is forwarded as it arrives, holding back only the trailing bytes of an incomplete character. How a proxy's buffering affects perceived latency is its own subject; the translator itself should not wait for the whole response. 4. **Merge usage.** Where a provider reports input usage when the stream opens and output usage at the end, the translator keeps the first and emits one merged `usage` event after `finish`. Where usage must be requested, the gateway adds that option to the upstream request itself. ## Errors after the first byte Once the first event is written, the **HTTP status is already 200** and cannot be changed. Providers signal later failures in two ways: an error event inside the stream, or simply closing the connection. The gateway must: - translate a provider's in-stream error into its own `error` event with a normalised code (overloaded, rate-limited, filtered, upstream disconnect, internal); - turn an upstream connection closing without a terminal event into an `error`, never into a `finish`; - document for clients that a stream ending without `done` is a failure, whatever the status code said. Faking a clean finish with whatever text arrived is the classic bug: the client stores half an answer as complete. Whether the gateway should switch to another provider partway through is a failover question; the stream format only has to make the failure explicit. ## Testing chunk boundaries Translators fail on boundaries, so tests should replay **recorded provider streams** with the bytes re-split at every possible point: - one server-sent event split across two network reads; - a **multi-byte UTF-8 character** split across two provider deltas, which the translator must not emit as two broken halves; - a tool-argument fragment that ends mid-string; - a stream cut at each position, which must always end in exactly one `error`. A streaming translator that passes these tests behaves the same for every provider behind every alias, which is the whole reason it sits in the gateway rather than in each application.

  • Why must a client treat a stream that ends without the terminal done event as failed?
    Because the status code was sent before generation began and says nothing about how it ended. A connection can drop, a proxy can time out, or the provider can fail mid-answer. If clients accept any closed stream as complete, half-answers get stored as finished. The gateway guarantees exactly one terminal event, so its absence is itself the error signal.
  • How do you keep the translator from splitting a multi-byte character when re-chunking text deltas?
    Decode bytes into text incrementally and emit only complete characters, holding back the trailing bytes of an incomplete sequence until the next delta arrives. Test it by replaying recorded streams re-split at every byte position, including inside characters outside the basic Latin range, and asserting the concatenated output matches the original exactly.

  • If the HTTP status was 200, the stream succeeded.
  • Each provider chunk maps one-to-one onto a gateway event, so no state is needed.
  • Tool arguments should be parsed as JSON on every fragment as it arrives.
  • Every provider includes usage in its stream without being asked.
  • A dropped upstream connection can be reported as a normal finish with the text so far.