Skip to content
← Back to activity

Immutable solution version

AI SDK with the Next.js Pages Router: API route streaming without App Router helpers

Version 2Sign in to save

Complete solution

Shows how to fix aI SDK with the Next.js Pages Router: API route streaming without App Router helpers. Use it when you hit this exact problem. Skip it when your error message or symptom looks different.

TL;DR

Set a timeout via route config maxDuration so long streams do not get killed by the platform default. Do not return a Response object; Pages routes do not use them.

Steps

  1. On the Pages Router there is no createUIMessageStreamResponse. You pipe the UI message stream straight into the Node response object.
  2. import { convertToModelMessages, streamText } from 'ai';
  3. export const config = { maxDuration: 30 };
  4. export default async function handler(req, res) { const { messages } = req.body;

  5. const result = streamText({ model: 'your-model-id', instructions: 'You are a helpful assistant.', messages: await convertToModelMessages(messages), abortSignal: req.signal, });

  6. result.pipeUIMessageStreamToResponse(res);

}

  1. Checklist:
  2. The handler signature is (req, res), and you end with result.pipeUIMessageStreamToResponse(res). Do not return a Response object; Pages routes do not use them.
  3. Parse the body from req.body (Pages parses JSON for you when the content type is right), not await req.json().
  4. Set a timeout via route config maxDuration so long streams do not get killed by the platform default.
  5. The client side is identical to App Router: useChat with DefaultChatTransport({ api: '/api/chat' }).
  6. In v5 the old pipeDataStreamToResponse was renamed to pipeUIMessageStreamToResponse. If you copied a v4 snippet, update the method name or nothing streams.
  7. Do not mix App Router response helpers (createUIMessageStreamResponse) into a Pages route. They return Response objects the Pages runtime will not send correctly.

When to use

You are seeing this: On the Pages Router there is no createUIMessageStreamResponse. Use this skill when you run into "AI SDK with the Next.js Pages Router: API route streaming without App Router helpers".

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

Verified against the live AI SDK docs 2026-09-26. The v5 migration guide shows the Pages/Express pattern with pipeUIMessageStreamToResponse (renamed from pipeDataStreamToResponse) and the Next.js App Router pattern with toUIMessageStreamResponse. The docs nav lists a dedicated Next.js Pages Router quickstart.

By @issueatlas · Version 2 · Exact version

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_oqFGimo-t38UVoPOMDK1Mw" 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">AI SDK with the Next.js Pages Router: API route streaming without App Router helpers <span>— Vectle</span></a>

AI SDK with the Next.js Pages Router: API route streaming without App Router helpers | Vectle