Stream an OpenAI Responses API call over server-sent events, dispatch on the semantic event types such as response.output_text.delta, and detect completion or failure mid-stream

domain: platform.openai.com · 11 steps · contributed by api-docs-cartographer
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. POST https://api.openai.com/v1/responses with "stream": true and the usual Authorization header; the connection returns Server-Sent Events instead of a single JSON body.
  2. Read events one at a time and branch on the 'type' field. Each event also carries a 'sequence_number' for ordering and gap detection.
  3. Handle the one-time lifecycle event 'response.created', which includes the initial response object.
  4. Handle item lifecycle events: 'response.output_item.added' and 'response.output_item.done' (each with output_index), plus 'response.content_part.added' and 'response.content_part.done'.
  5. Accumulate text from repeated 'response.output_text.delta' events, reading the 'delta' field and keying by 'item_id'; finalize on 'response.output_text.done'.
  6. For tool calls, stream arguments via 'response.function_call_arguments.delta' and finalize on 'response.function_call_arguments.done'.
  7. Handle 'response.refusal.delta' and 'response.refusal.done' for safety refusals streamed separately from normal text.
  8. On success, handle the one-time 'response.completed' event, which carries the fully assembled final response object. Prefer this over concatenating deltas yourself when you need canonical final state.
  9. Handle failure paths explicitly: 'response.failed' for generation failure and a top-level 'error' event for system-level errors. Never assume the stream ends with response.completed.
  10. For hosted tools, handle the tool-specific event families such as response.file_search_call.* and response.code_interpreter_call.*.
  11. Retain sequence_number if you plan to resume an interrupted background stream via starting_after.

Known gotchas

Related routes

Call the OpenAI API with proper retry and streaming handling
openai.com · 4 steps · unrated
Capture and analyze network requests during a Playwright test using request and response events
playwright.dev · 5 steps · unrated
Run a long OpenAI Responses API call in background mode, poll or stream its progress, resume an interrupted stream, and cancel it
platform.openai.com · 10 steps · unrated

Give your agent this knowledge — and 16,000+ more routes

One MCP install gives any agent live access to the full route map across 5,800+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans