Skip to content
← Back to activity

Immutable solution version

Flask: stream OpenAI Responses API output as SSE without hanging workers

Version 2Sign in to save

Complete solution

Shows how to fix flask: stream OpenAI Responses API output as SSE without hanging workers. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Use gevent or eventlet workers for SSE routes, or run a separate gunicorn with more workers for the streaming blueprint. - Create the OpenAI client once at app startup, not per request. The sync client is safe to share across threads; per-request clients just add TLS handshake cost.

Steps

    • With gunicorn sync workers, one open SSE stream holds one worker for the whole

    session. Four workers means the fifth concurrent user waits. Use gevent or eventlet workers for SSE routes, or run a separate gunicorn with more workers for the streaming blueprint.

  • Create the OpenAI client once at app startup, not per request. The sync client is safe to share across threads; per-request clients just add TLS handshake cost.

    • Open two browser tabs and confirm both streams progress at once (catches the

    single-worker stall).

  • Kill a client mid-stream and confirm the generator exits instead of running to response.completed in the background, burning tokens.

When to use

You are seeing this: Flask routes are sync, which is actually convenient: the sync OpenAI client's stream=True iterator works directly in a generator. Use this skill when you run into "Flask: stream OpenAI Responses API output as SSE without hanging workers".

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 defines the typed SSE events (response.outputtext.delta, response.completed) for client.responses.create(stream=True). Flask's streamwith_context is required to keep request context alive in streaming generators; gunicorn sync workers block one worker per open stream.

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_F_fDwJVuI9SMLGW2hzsujw" 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">Flask: stream OpenAI Responses API output as SSE without hanging workers <span>— Vectle</span></a>

Flask: stream OpenAI Responses API output as SSE without hanging workers | Vectle