Old Vercel AI SDK streaming code breaks after updating the ai package
Fix it fast
Most common cause, 1 of 3The route uses the 2.x stream helpers
Install the provider package, call streamText, and return the UI message stream that useChat reads.
npm install ai @ai-sdk/openai
// app/api/chat/route.ts
import { streamText, convertToModelMessages, type UIMessage } from 'ai'
import { openai } from '@ai-sdk/openai'
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json()
const result = streamText({
model: openai('gpt-4o-mini'),
messages: await convertToModelMessages(messages),
})
return result.toUIMessageStreamResponse()
}Short answer
OpenAIStream, StreamingTextResponse and the other AI SDK 2.x stream helpers were removed in AI SDK 4.0. Current releases stream through streamText from 'ai' with a provider such as @ai-sdk/openai, and the route returns result.toUIMessageStreamResponse() for useChat, or result.toTextStreamResponse() for plain text. Rewrite the route instead of pinning the old package.
Which cause do you have?
Check the installed major version and list every import of the removed helpers.
node -p "require('ai/package.json').version"
grep -rnE "OpenAIStream|StreamingTextResponse|AIStream|streamToResponse" app src lib 2>/dev/nullThen find the line you see in this list and open its fix:
- 1The route uses the 2.x stream helpers
TS2305 for OpenAIStream or StreamingTextResponse on a version 4 or later - 2Code written for 4.x calls toDataStreamResponse
error TS2551: Property 'toDataStreamResponse' does not exist on type 'StreamTextResult<...>'. Did you mean 'toTextStreamResponse'? - 3The OpenAI client is passed where a provider model is expected
Code still creates new OpenAI() from the openai package and calls openai.chat.completions.create before streaming
What does the error look like?
error TS2305: Module '"ai"' has no exported member 'OpenAIStream'.error TS2305: Module '"ai"' has no exported member 'StreamingTextResponse'.
Cause 1 of 3
The route uses the 2.x stream helpers
You seeTS2305 for OpenAIStream or StreamingTextResponse on a version 4 or later
Check
The 4.0 migration guide lists AIStream, StreamingTextResponse and streamToResponse as removed and points to streamText with a response method instead. OpenAIStream belonged to the same family of legacy helpers.
grep -rn "from 'ai'" app src libFix
Install the provider package, call streamText, and return the UI message stream that useChat reads. In ai 7, convertToModelMessages returns a Promise, so await it.
npm install ai @ai-sdk/openai
// app/api/chat/route.ts
import { streamText, convertToModelMessages, type UIMessage } from 'ai'
import { openai } from '@ai-sdk/openai'
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json()
const result = streamText({
model: openai('gpt-4o-mini'),
messages: await convertToModelMessages(messages),
})
return result.toUIMessageStreamResponse()
}Verify
tsc passes and a POST to the route streams tokens back.
npx tsc --noEmitConfirmed by ai-sdk.dev
Cause 2 of 3
Code written for 4.x calls toDataStreamResponse
You seeerror TS2551: Property 'toDataStreamResponse' does not exist on type 'StreamTextResult<...>'. Did you mean 'toTextStreamResponse'?
Check
The 5.0 migration guide renames the message and stream model: convertToCoreMessages became convertToModelMessages, and the response for useChat is the UI message stream. The current streamText reference lists toUIMessageStreamResponse and toTextStreamResponse.
grep -rnE "toDataStreamResponse|convertToCoreMessages" app src libFix
Swap the two calls. Use toTextStreamResponse when the client reads raw text, for example with fetch and a reader, rather than useChat.
// plain text stream for a simple fetch() client
export async function GET() {
const result = streamText({ model: openai('gpt-4o-mini'), prompt: 'Say hi' })
return result.toTextStreamResponse()
}Verify
curl prints the reply as it is generated.
curl -N http://localhost:3000/api/chatConfirmed by ai-sdk.dev
Cause 3 of 3
The OpenAI client is passed where a provider model is expected
You seeCode still creates new OpenAI() from the openai package and calls openai.chat.completions.create before streaming
Check
The old pattern made the request with the OpenAI SDK and handed the raw stream to OpenAIStream. streamText makes the request itself through a provider model, so the manual create call and the openai package import go away.
grep -rn "chat.completions.create" app src libFix
Delete the manual request and give streamText a model from @ai-sdk/openai. The provider reads OPENAI_API_KEY from the environment by default.
import { openai } from '@ai-sdk/openai'
const model = openai('gpt-4o-mini') // replaces new OpenAI() + create({ stream: true })Verify
The route works with only OPENAI_API_KEY set and no openai package import left.
grep -rn "from 'openai'" app src lib || echo noneConfirmed by ai-sdk.dev
How do I stop it coming back?
- Pin ai and its provider packages to the same major version and upgrade them together.
- Read the migration guide for each major version you skip; 3.x to 7.x crosses several renames.
- Keep model calls in one module so a library rewrite touches one file.
Sources
How was this page made?
I drafted this page with AI assistance from the official documents listed under Sources. Every cause above names the document that confirms it. I have not reproduced this fix on a machine of my own yet, so read it as documented guidance, not a test report. Found a mistake? Tell me.
Which stack is behind this page?
Projects of mine built on the same technologies:
- TaxiDexMy own multi-tenant taxi dispatch SaaS: API, dispatcher portal, driver and passenger apps, and the servers underneath.
- QistLockA NestJS and React SaaS driving an Android Device Owner agent that locks financed phones when payments lapse.
- WorkforceA field app, an admin console and a self-hosted Node.js server for push and reconciliation.