Maaz Zindani

Loading the index…

↑↓ move↵ openesc close4133 pages
AI and LLM integration

Old Vercel AI SDK streaming code breaks after updating the ai package

Your error

error TS2305: Module '"ai"' has no exported member 'OpenAIStream'.1 more line it can show

Fix it fast

Most common cause, 1 of 3

The route uses the 2.x stream helpers

Install the provider package, call streamText, and return the UI message stream that useChat reads.

Fix
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()
}
Did this fix it?
Full steps for this cause: check, fix, verify

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.

  • Applies to ai 7.0, @ai-sdk/openai 4.0, TypeScript 5.9, Node.js 24
  • Updated
  • By Maaz Zindani, Senior Software Engineer

Which cause do you have?

Check the installed major version and list every import of the removed helpers.

Run this first
node -p "require('ai/package.json').version"
grep -rnE "OpenAIStream|StreamingTextResponse|AIStream|streamToResponse" app src lib 2>/dev/null

Then find the line you see in this list and open its fix:

  1. 1The route uses the 2.x stream helpersTS2305 for OpenAIStream or StreamingTextResponse on a version 4 or later
  2. 2Code written for 4.x calls toDataStreamResponseerror TS2551: Property 'toDataStreamResponse' does not exist on type 'StreamTextResult<...>'. Did you mean 'toTextStreamResponse'?
  3. 3The OpenAI client is passed where a provider model is expectedCode 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.

Check
grep -rn "from 'ai'" app src lib

Fix

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.

Fix
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.

Verify
npx tsc --noEmit

Confirmed by ai-sdk.dev

Did this fix it?

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.

Check
grep -rnE "toDataStreamResponse|convertToCoreMessages" app src lib

Fix

Swap the two calls. Use toTextStreamResponse when the client reads raw text, for example with fetch and a reader, rather than useChat.

Fix
// 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.

Verify
curl -N http://localhost:3000/api/chat

Confirmed by ai-sdk.dev

Did this fix it?

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.

Check
grep -rn "chat.completions.create" app src lib

Fix

Delete the manual request and give streamText a model from @ai-sdk/openai. The provider reads OPENAI_API_KEY from the environment by default.

Fix
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.

Verify
grep -rn "from 'openai'" app src lib || echo none

Confirmed by ai-sdk.dev

Did this fix it?

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:

Next step

Want this handled for good?

I look after servers, apps and releases end to end. Tell me what keeps breaking.