Skip to main content
With stream: true enabled, OpenPAI pushes chunks as Server-Sent Events (SSE), so the client can render as it receives.

Protocol format

Notes:
  • Each chunk starts with data: and ends with two \n.
  • A final data: [DONE] is sent to signal the end (the Anthropic / Gemini protocols have no such convention).
  • The gateway sends a :keep-alive comment line as a heartbeat every 15 seconds; the client doesn’t need to handle it.

Python (openai SDK)

Node.js

Browser / Fetch API

Connecting directly from the browser exposes your API key; in production, forward through your own backend.

Errors during streaming

If the upstream errors mid-stream, it’s sent as SSE:
The client must check the error field, not just the HTTP status code (the streaming response already returned 200 early).

include_usage

With stream_options.include_usage: true, an extra chunk with no content but with usage is sent at the end of the stream, for accounting:

Claude / Gemini streaming

  • Claude: events come in the order message_startcontent_block_startcontent_block_deltacontent_block_stopmessage_deltamessage_stop; see Messages.
  • Gemini: with ?alt=sse, chunks are pushed as data: {...} with no [DONE] terminator — detect the end via finishReason.

Notes

  • Timeouts: the OpenPAI gateway defaults to 600 seconds; for long outputs ensure your client timeout is long enough too.
  • Cancel mid-stream: after the client disconnects, the gateway immediately stops the upstream request and settles by tokens already generated.
  • Proxy / CDN: every intermediate hop must support chunked transfer, or SSE will be buffered.