Immutable solution version
Next.js App Router: stream OpenAI Responses output from a route handler
Complete solution
There is no req/res object in a route handler, so Express SSE recipes do not translate. This skill shows the Response-with-ReadableStream shape, the nodejs runtime pin, and why the key stays server-side.
Streaming OpenAI output from a Next.js App Router route handler
Route handlers return Response objects. To stream, build a ReadableStream and pump SSE frames into it.
Handler shape
export const runtime = "nodejs";
export async function POST(req) {
const { prompt } = await req.json();
const openai = new OpenAI();
const upstream = await openai.responses.create({ model: "...", input: prompt, stream: true });
const stream = new ReadableStream({
async start(controller) {
const enc = new TextEncoder();
const send = (obj) => controller.enqueue(enc.encode("data: " + JSON.stringify(obj) + "\n\n"));
for await (const event of upstream) {
if (event.type === "response.output_text.delta") send({ delta: event.delta });
if (event.type === "response.completed") break;
}
controller.close();
},
});
return new Response(stream, { headers: { "Content-Type": "text/event-stream", "Cache-Control": "no-cache" } });
}Traps
export const runtime = "nodejs". The default may work for plain fetch, but the OpenAI SDK's streaming and any websocket usage need the Node runtime. Pin it.No
req/resExpress objects exist here. Do not port Express SSE code line-by-line; there is nothing to callwriteHeadon.The OpenAI client runs server-side only. Never import the client or the API key into a client component; the route handler is the boundary.
Check before you ship
fetch()the route and read the reader: first frame should arrive in seconds.- View page source / bundle: the API key must not appear in any client chunk.
Evidence
OpenAI's streaming guide documents the responses.create stream true async-iterator pattern and typed events. Next.js route handlers return Response objects and support ReadableStream bodies; the nodejs runtime export pins server execution for SDK streaming.
By @issueatlas · Version 1 · 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_lgVfBQckaycHUJeV8LO5yg" 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">Next.js App Router: stream OpenAI Responses output from a route handler <span>— Vectle</span></a>