Streaming output and interrupt handling

Correctly parse SSE, delta content, completion events, and connection interruptions.

Initiate streaming request

Set stream: true in supported Chat or Responses requests. Use curl -N on the command line to turn off output buffering. The server or reverse proxy cannot cache the entire response, otherwise you will see a one-time output at the end.

Parse by event instead of network block

A TCP read does not equal an SSE event, and UTF-8 characters may cross read boundaries. First incrementally decode, then split the event according to SSE empty line boundary, and merge multiple lines data:. Ignore comments or heartbeats and finally parse JSON by protocol.

Chat text usually comes from choices[].delta.content; empty deltas, character fragments, and tool fragments are all legal. [DONE] is the end mark of part of the protocol. Responses and Gemini should be processed according to their respective event rules.

Interruptions and failures

After HTTP has returned 200, an upstream error may still occur or the flow may be terminated prematurely. Preserve received content and clearly mark it as incomplete to avoid putting half-JSON or half-tool calls into the business. Terminates reading and subsequent tool execution on user cancellation.

An outage does not guarantee that inference will not occur or billing will not occur. Do not automatically resend the entire build request; check the call log and task status first. When the final usage is missing, you cannot determine that the cost is zero.

Chat streaming example

curl -N --fail-with-body https://dunduntoken.com/v1/chat/completions   -H "Authorization: Bearer $DUNDUN_API_KEY"   -H "Content-Type: application/json"   -d '{"model":"MODEL_ID","stream":true,"messages":[{"role":"user","content":"用三句话解释流式输出"}]}'

The following only illustrates the boundaries of the SSE frame, not the full response:

data: {"choices":[{"delta":{"content":"你好"}}]}

data: [DONE]

Protocol processing comparison

Agreement Text increment Completion judgment
Chat delta in choices finish_reason and protocol end tag
Responses Process output_text delta by event type completed / failed and other final states
Messages Corresponding content in content_block_delta message_stop and preamble end information
Gemini candidates/parts in SSE data finishReason and complete flow status

Application implementation points

Maintain a buffer of outstanding events; parse JSON only after complete data is available. Display text and accumulated tool parameters are processed separately, and execution is prohibited until tool parameters are completed. Don’t render all events as user text. Terminates background reading on disconnect, retaining part of the output and allowing the user to decide whether to continue.

Test before going online

It is a tool stage that verifies the appearance of the first word, normal final state, active cancellation, agent disconnection and no text for a long time. If the browser only receives content at the end, compare the direct service and reverse proxy paths, and check compression, middleware, and proxy buffering.