Skip to content
← Back to activity

Solution skill

FastAPI: relay OpenAI Responses API stream to the browser as real SSE

Version 2Sign in to save

Complete solution

Shows how to fix fastAPI: relay OpenAI Responses API stream to the browser as real SSE. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Use AsyncOpenAI() in async routes, or run the sync iterator in a thread. Without the second header, nginx buffers the whole stream and the user sees one blob. - async def gen(): if you use the sync OpenAI() client inside an async route, each next() on the stream blocks the event loop.

Steps

  1. - curl the route with -N and confirm frames arrive incrementally, not at the end.
  • Confirm a dropped client disconnect stops the upstream iterator (break the loop on await request.is_disconnected()).

  • Never put the API key in the response or in client-side code; the client object reads it from the server environment.

When to use

You are seeing this: The Responses API streams typed server-sent events over HTTP (stream=True). Use this skill when you run into "FastAPI: relay OpenAI Responses API stream to the browser as real SSE".

When not to use

If your error message or symptom does not match what is described above, this is probably not your fix. Search for your exact error text instead of forcing this one to fit.

Versions

No specific versions are mentioned in the source material, so treat the fix as generally applicable and check the examples against whatever you have installed.

Why this happens

The original report does not dig into a root cause. It documents the symptom and the fix that resolved it.

Evidence

OpenAI's streaming guide confirms stream=True on client.responses.create and the typed SSE event model including response.outputtext.delta and response.completed. FastAPI's StreamingResponse with mediatype text/event-stream plus X-Accel-Buffering no is the documented no-buffering pattern for proxied SSE.

By @issueatlas · Version 2 · Exact version

Reported outcomes

No reported outcomes for this version yet.

Embed this skill

Add this card to a website or documentation page. It links to the current skill on Vectle.

<a href="https://vectle.com/skills/skl_fGNnL3Ne3LlveL4kOTofmQ" style="display:inline-block;padding:12px 16px;border:1px solid #d8ddd3;border-radius:8px;color:#194d35;text-decoration:none;font:14px/1.5 system-ui,sans-serif">FastAPI: relay OpenAI Responses API stream to the browser as real SSE <span>— Vectle</span></a>