Vercel AI SDK Next.js Fix 5 min read

Vercel AI SDK Not Working? Fix Streaming, useChat & Edge Runtime Errors

Troubleshoot Vercel AI SDK errors — streaming broken in Next.js App Router, useChat hook stuck on loading, provider API key not configured, Edge Runtime incompatibility, and 504 timeout on Vercel Hobby.

Vercel AI SDK live status

Vercel AI SDK — live status

Updated every 5 minutes · Full incident history →

Full status →

Common errors and fixes

Provider API key not configured

The Vercel AI SDK does not use a single API key — each provider reads its own environment variable. Set all keys you need in .env.local:

# .env.local — one var per provider
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...
GOOGLE_GENERATIVE_AI_API_KEY=AIza...
MISTRAL_API_KEY=...
GROQ_API_KEY=gsk_...
Important: add all vars to Vercel project dashboard (Settings → Environment Variables) for production — .env.local only works locally and is gitignored.

Streaming broken — wrong response type

The most common mistake is returning a plain Response or calling the wrong method. Your App Router route must call result.toDataStreamResponse():

// ✅ Correct — returns a streaming response
import { streamText } from 'ai';
import { openai } from '@ai-sdk/openai';

export async function POST(req: Request) {
  const { messages } = await req.json();
  const result = await streamText({
    model: openai('gpt-4o'),
    messages,
  });
  return result.toDataStreamResponse(); // ← must use this
}

// ❌ Wrong — breaks streaming
return new Response(JSON.stringify(result)); // sends once, no streaming
return result.toTextStreamResponse();       // wrong format for useChat

Also note: if using generateText (not streamText), you get a complete response — use streamText for streaming.

useChat hook not working

Correct React client component setup with error handling:

'use client';
import { useChat } from 'ai/react';

export default function Chat() {
  const { messages, input, handleInputChange, handleSubmit, isLoading, error } = useChat({
    api: '/api/chat',          // must match your route path
    onError: (e) => console.error('Stream error:', e),
  });

  return (
    <div>
      {messages.map(m => (
        <div key={m.id}>
          <b>{m.role}:</b> {m.content}
        </div>
      ))}
      {error && <p style={{color:'red'}}>Error: {error.message}</p>}
      <form onSubmit={handleSubmit}>
        <input value={input} onChange={handleInputChange} disabled={isLoading} />
        <button type="submit" disabled={isLoading}>Send</button>
      </form>
    </div>
  );
}
  • Missing 'use client': useChat is a React hook — the component must be a client component.
  • Wrong api path: the default is /api/chat — if your route is elsewhere, pass it explicitly via the api prop.
  • Not handling the error state: always add an onError callback and render the error value to surface server errors to users.

Edge Runtime vs Node.js Runtime

Control the runtime with an export at the top of your route file:

// Add to your route file to control runtime
export const runtime = 'edge';    // no timeout for streaming, no fs access
// OR
export const runtime = 'nodejs';  // default, 10s hobby / 60s pro timeout

export const maxDuration = 60;    // Pro/Enterprise only — extend Node timeout

Provider Edge Runtime support:

  • ✅ Edge compatible: openai, anthropic, google, mistral, groq
  • ⚠️ May need Node.js runtime: AWS Bedrock, Azure OpenAI
  • Crypto error: if you see "The Edge Runtime does not support Node.js crypto module", switch to runtime = 'nodejs'.

Tool calls / structured output not working

The most common mistake with tool calls is forgetting maxSteps, which is required for multi-step tool use:

import { streamText, tool } from 'ai';
import { z } from 'zod';

const result = await streamText({
  model: openai('gpt-4o'),
  messages,
  tools: {
    getWeather: tool({
      description: 'Get weather for a location',
      parameters: z.object({
        location: z.string().describe('City name'),
      }),
      execute: async ({ location }) => {
        return { temperature: 22, condition: 'sunny' };
      },
    }),
  },
  maxSteps: 5, // ← required for multi-step tool use (default: 1)
});

Common issue: tool results not appearing — add maxSteps: 5 to allow the model to continue after tool execution.

🔔

Know when the Vercel AI SDK has an outage

Free email alerts. Star Vercel AI on Prismix — no credit card needed.

FAQ

Vercel AI SDK vs LangChain.js — which to use?

Vercel AI SDK is lightweight, TypeScript-first, and optimized for streaming UIs with React hooks. LangChain.js has more features (chains, agents, retrievers) but is heavier. For chat UIs and simple completion endpoints, use the AI SDK. For complex agent pipelines or RAG with vector stores, consider LangChain.

Does the AI SDK work outside Next.js / Vercel?

Yes. The ai package works in any Node.js or Edge environment (Express, Hono, Remix, SvelteKit, Deno). The useChat/useCompletion hooks are React-specific. The streaming helpers work with any framework that supports ReadableStream.

How to add conversation memory / history?

Pass the full messages array from useChat back to your API route — the SDK does not persist history automatically. For server-side persistence, store messages in a database and load them on route init. For multi-turn context, ensure you pass the complete messages array (not just the latest message) to streamText.

Monitor related services