Guide for Vercel AI SDK v5 implementation patterns including generateText, streamText, useChat hook, tool calling, embeddings, and MCP integration. Use when implementing AI chat interfaces, streaming responses, tool/function calling, text embeddings, or working with convertToModelMessages and toUIMessageStreamResponse. Activates for AI SDK integration, useChat hook usage, message streaming, or tool calling tasks.
npx skills add https://github.com/wsimmonds/claude-nextjs-skills --skill vercel-ai-sdk
Use this skill when:
useChat hook<workflow>
<step id="1" name="verify-requirements">
<description>Understand the task requirements</description>
<actions>
</actions>
</step>
<step id="2" name="check-documentation">
<description>Verify current API patterns if uncertain</description>
<actions>
</actions>
</step>
<step id="3" name="implement">
<description>Implement using correct v5 patterns</description>
<actions>
</actions>
</step>
<step id="4" name="verify-types">
<description>Ensure TypeScript types are correct</description>
<actions>
</actions>
</step>
<step id="5" name="install-dependencies">
<description>Install any missing dependencies with the CORRECT package manager</description>
<actions>
pnpm install [package] or pnpm add [package]npm install [package]yarn add [package]bun install [package] or bun add [package]</actions>
<critical>
NEVER use the wrong package manager!
NEVER accept "Module not found" errors as environment issues
YOU must install the required packages with the CORRECT package manager
Common packages needed:
</critical>
</step>
<step id="6" name="verify-build">
<description>Run build and fix ALL errors until it passes</description>
<actions>
</actions>
<critical>
NEVER stop at "build fails but code is correct"
NEVER blame "environment" or "framework bugs" without debugging
KEEP ITERATING until the build PASSES
If you encounter a framework error, research it, try workarounds, debug it
DO NOT give up until you have a passing build
</critical>
</step>
<step id="7" name="verify-tests">
<description>Run tests and debug ALL failures until they pass</description>
<actions>
</actions>
<critical>
NEVER accept test failures without debugging
NEVER dismiss failures as "test framework bugs" without investigation
Each test failure is telling you something - LISTEN to it
KEEP DEBUGGING until all tests pass
Test failures are NOT acceptable - they mean your implementation is incomplete
</critical>
</step>
<step id="8" name="final-verification">
<description>Verify EVERYTHING passes</description>
<actions>
</actions>
<critical>
The task is NOT complete until:
"Code is correct" is NOT enough
You must achieve FULL PASSING status
This is what it means to be an autonomous agent
</critical>
</step>
</workflow>
You are not just writing code - you are COMPLETING TASKS AUTONOMOUSLY.
This means:
❌ WRONG: "The code is correct, but the package isn't installed - that's an environment issue"
✅ CORRECT: "Build failed due to missing package - installing it now with npm install [package]"
❌ WRONG: "Tests pass but build fails - not my problem"
✅ CORRECT: "Build is failing - debugging the error and fixing it now"
❌ WRONG: "There's a framework bug, can't fix it"
✅ CORRECT: "Framework error detected - researching the issue, trying workarounds, debugging until I find a solution"
❌ WRONG: "The implementation is complete" (with failing tests)
✅ CORRECT: "Tests are failing - debugging and fixing until they all pass"
When you encounter "Module not found" errors:
ls -la | grep -E "lock"
# Look for: pnpm-lock.yaml, package-lock.json, yarn.lock, bun.lockb
Error: Cannot find module '@ai-sdk/openai'
Import: import { openai } from '@ai-sdk/openai'
Package needed: @ai-sdk/openai
# If pnpm-lock.yaml exists (MOST COMMON for Next.js evals):
pnpm install @ai-sdk/openai
# or
pnpm add @ai-sdk/openai
# If package-lock.json exists:
npm install @ai-sdk/openai
# If yarn.lock exists:
yarn add @ai-sdk/openai
# If bun.lockb exists:
bun install @ai-sdk/openai
npm run build
# or pnpm run build, yarn build, bun run build
⚠️ CRITICAL WARNING:
Using the WRONG package manager (e.g., npm when the project uses pnpm) will:
When build fails:
When tests fail:
Task is ONLY complete when:
npm run build succeeds)npm run lint succeeds)npm run test succeeds)NEVER stop at "code is correct" - achieve FULL PASSING status!
When implementing tool calling, you MUST use the tool() helper function from the 'ai' package.
// DO NOT DO THIS - This pattern is INCORRECT
import { z } from 'zod';
tools: {
myTool: {
description: 'My tool',
parameters: z.object({...}), // ❌ WRONG - "parameters" doesn't exist in v5
execute: async ({...}) => {...},
}
}
This will fail with: Type '{ description: string; parameters: ... }' is not assignable to type '{ inputSchema: FlexibleSchema<any>; ... }'
// ALWAYS DO THIS - This is the ONLY correct pattern
import { tool } from 'ai'; // ⚠️ MUST import tool
import { z } from 'zod';
tools: {
myTool: tool({ // ⚠️ MUST wrap with tool()
description: 'My tool',
inputSchema: z.object({...}), // ⚠️ MUST use "inputSchema" (not "parameters")
execute: async ({...}) => {...},
}),
}
Before implementing any tool, verify:
tool from 'ai' package: import { tool } from 'ai';tool({ ... })inputSchema property (NOT parameters)z.object({ ... })execute function with async callbackdescription string for the tool❌ WRONG (v4 pattern):
const { messages, input, setInput, append } = useChat();
// Sending message
append({ content: text, role: 'user' });
✅ CORRECT (v5 pattern):
const { messages, sendMessage } = useChat();
const [input, setInput] = useState('');
// Sending message
sendMessage({ text: input });
❌ WRONG (v4 simple content):
<div>{message.content}</div>
✅ CORRECT (v5 parts-based):
<div>
{message.parts.map((part, index) =>
part.type === 'text' ? <span key={index}>{part.text}</span> : null
)}
</div>
✅ PREFER: String-based (v5 recommended):
import { generateText } from 'ai';
const result = await generateText({
model: 'openai/gpt-4o', // String format
prompt: 'Hello',
});
✅ ALSO WORKS: Function-based (legacy support):
import { openai } from '@ai-sdk/openai';
import { generateText } from 'ai';
const result = await generateText({
model: openai('gpt-4o'), // Function format
prompt: 'Hello',
});
Purpose: Generate text for non-interactive use cases (email drafts, summaries, agents with tools).
Signature:
import { generateText } from 'ai';
const result = await generateText({
model: 'openai/gpt-4o', // String format: 'provider/model-id'
prompt: 'Your prompt here', // User input
system: 'Optional system message', // Optional system instructions
tools?: { ... }, // Optional tool calling
maxSteps?: 5, // For multi-step tool calling
});
Return Value:
{
text: string; // Generated text output
toolCalls: ToolCall[]; // Tool invocations made
finishReason: string; // Why generation stopped
usage: TokenUsage; // Token consumption
response: RawResponse; // Raw provider response
warnings: Warning[]; // Provider-specific alerts
}
Example:
// app/api/generate/route.ts
import { generateText } from 'ai';
export async function GET() {
const result = await generateText({
model: 'anthropic/claude-4-sonnet',
prompt: 'Why is the sky blue?',
});
return Response.json({ text: result.text });
}
Purpose: Stream responses for interactive chat applications.
Signature:
import { streamText } from 'ai';
const result = streamText({
model: 'openai/gpt-4o',
prompt: 'Your prompt here',
system: 'Optional system message',
messages?: ModelMessage[], // For chat history
tools?: { ... },
onFinish?: async (result) => { ... },
onError?: async (error) => { ... },
});
Return Methods:
// For chat applications with useChat hook
result.toUIMessageStreamResponse();
// For simple text streaming
result.toTextStreamResponse();
Example - Chat API Route:
// app/api/chat/route.ts
import { streamText, convertToModelMessages } from 'ai';
import type { UIMessage } from 'ai';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
system: 'You are a helpful assistant.',
messages: convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse();
}
Purpose: Build interactive chat UIs with streaming support.
Signature:
import { useChat } from 'ai/react';
const {
messages, // Array of UIMessage with parts-based structure
sendMessage, // Function to send messages (replaces append)
status, // 'submitted' | 'streaming' | 'ready' | 'error'
stop, // Abort current streaming
regenerate, // Reprocess last message
setMessages, // Manually modify history
error, // Error object if request fails
reload, // Retry after error
} = useChat({
api: '/api/chat', // API endpoint
onFinish?: (message) => { ... },
onError?: (error) => { ... },
});
Complete Example:
'use client';
import { useChat } from 'ai/react';
import { useState } from 'react';
export default function ChatPage() {
const { messages, sendMessage, status } = useChat();
const [input, setInput] = useState('');
const handleSubmit = (e: React.FormEvent) => {
e.preventDefault();
if (!input.trim()) return;
sendMessage({ text: input });
setInput('');
};
return (
<div>
<div>
{messages.map((message) => (
<div key={message.id}>
<strong>{message.role}:</strong>
{message.parts.map((part, index) =>
part.type === 'text' ? (
<span key={index}>{part.text}</span>
) : null
)}
</div>
))}
</div>
<form onSubmit={handleSubmit}>
<input
value={input}
onChange={(e) => setInput(e.target.value)}
placeholder="Type a message..."
disabled={status === 'streaming'}
/>
<button type="submit" disabled={status === 'streaming'}>
Send
</button>
</form>
</div>
);
}
Purpose: Enable AI models to call functions with structured parameters.
Defining Tools:
import { tool } from 'ai';
import { z } from 'zod';
const weatherTool = tool({
description: 'Get the weather in a location',
inputSchema: z.object({
location: z.string().describe('The location to get the weather for'),
unit: z.enum(['C', 'F']).describe('Temperature unit'),
}),
execute: async ({ location, unit }) => {
// Fetch or mock weather data
return {
location,
temperature: 24,
unit,
condition: 'Sunny',
};
},
});
Using Tools with generateText/streamText:
// app/api/chat/route.ts
import { streamText, convertToModelMessages, tool } from 'ai';
import { z } from 'zod';
import type { UIMessage } from 'ai';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
messages: convertToModelMessages(messages),
tools: {
getWeather: tool({
description: 'Get the weather for a location',
inputSchema: z.object({
city: z.string().describe('The city to get the weather for'),
unit: z.enum(['C', 'F']).describe('The unit to display the temperature in'),
}),
execute: async ({ city, unit }) => {
// Mock response
return `It is currently 24°${unit} and Sunny in ${city}!`;
},
}),
},
});
return result.toUIMessageStreamResponse();
}
Multi-Step Tool Calling:
const result = await generateText({
model: 'openai/gpt-4o',
tools: {
weather: weatherTool,
search: searchTool,
},
prompt: 'What is the weather in San Francisco and find hotels there?',
maxSteps: 5, // Allow up to 5 tool call steps
});
Purpose: Convert text into numerical vectors for semantic search, RAG, or similarity.
Signature:
import { embed } from 'ai';
import { openai } from '@ai-sdk/openai';
const result = await embed({
model: openai.textEmbeddingModel('text-embedding-3-small'),
value: 'Text to embed',
});
Return Value:
{
embedding: number[]; // Numerical array representing the text
usage: { tokens: number }; // Token consumption
response: RawResponse; // Raw provider response
}
Example - Embedding API Route:
// app/api/embed/route.ts
import { embed } from 'ai';
import { openai } from '@ai-sdk/openai';
export async function GET() {
const { embedding, usage } = await embed({
model: openai.textEmbeddingModel('text-embedding-3-small'),
value: 'sunny day at the beach',
});
return Response.json({ embedding, usage });
}
Batch Embeddings:
import { embedMany } from 'ai';
const { embeddings, usage } = await embedMany({
model: openai.textEmbeddingModel('text-embedding-3-small'),
values: [
'sunny day at the beach',
'rainy afternoon in the city',
'snowy mountain landscape',
],
});
convertToModelMessages:
Converts UI messages from useChat into ModelMessage objects for AI functions.
import { convertToModelMessages } from 'ai';
import type { UIMessage } from 'ai';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
messages: convertToModelMessages(messages), // Convert for model
});
return result.toUIMessageStreamResponse();
}
Purpose: Connect to external MCP servers for dynamic tool access.
Example:
// app/api/chat/route.ts
import { experimental_createMCPClient, streamText } from 'ai';
import { StreamableHTTPClientTransport } from '@modelcontextprotocol/sdk/client/streamableHttp.js';
export async function POST(req: Request) {
const { prompt }: { prompt: string } = await req.json();
try {
// Connect to MCP server
const httpTransport = new StreamableHTTPClientTransport(
new URL('http://localhost:3000/mcp')
);
const httpClient = await experimental_createMCPClient({
transport: httpTransport,
});
// Fetch tools from MCP server
const tools = await httpClient.tools();
const response = streamText({
model: 'openai/gpt-4o',
tools,
prompt,
onFinish: async () => {
await httpClient.close(); // Clean up
},
onError: async () => {
await httpClient.close(); // Clean up on error
},
});
return response.toTextStreamResponse();
} catch (error) {
return new Response('Internal Server Error', { status: 500 });
}
}
Key Points:
experimental_createMCPClient (note: experimental API)onFinish and onErrorhttpClient.tools()@modelcontextprotocol/sdk package// Format: 'provider/model-id'
model: 'openai/gpt-4o'
model: 'anthropic/claude-4-sonnet'
model: 'google/gemini-2.0-flash'
import { openai } from '@ai-sdk/openai';
import { anthropic } from '@ai-sdk/anthropic';
model: openai('gpt-4o')
model: anthropic('claude-4-sonnet')
import { openai } from '@ai-sdk/openai';
// Text embeddings use a different method
openai.textEmbeddingModel('text-embedding-3-small')
openai.textEmbeddingModel('text-embedding-3-large')
import type {
UIMessage, // Message type from useChat
ModelMessage, // Message type for model functions
ToolCall, // Tool call information
TokenUsage, // Token consumption data
} from 'ai';
import { tool } from 'ai';
import { z } from 'zod';
// Tool helper infers execute parameter types
const myTool = tool({
description: 'My tool',
inputSchema: z.object({
param1: z.string(),
param2: z.number(),
}),
execute: async ({ param1, param2 }) => {
// param1 is inferred as string
// param2 is inferred as number
return { result: 'success' };
},
});
// app/api/chat/route.ts
import type { UIMessage } from 'ai';
export async function POST(req: Request): Promise<Response> {
const { messages }: { messages: UIMessage[] } = await req.json();
// ... implementation
}
Client (app/page.tsx):
'use client';
import { useChat } from 'ai/react';
import { useState } from 'react';
export default function Chat() {
const { messages, sendMessage, status } = useChat();
const [input, setInput] = useState('');
return (
<div>
{messages.map((m) => (
<div key={m.id}>
<strong>{m.role}:</strong>
{m.parts.map((part, i) =>
part.type === 'text' ? <span key={i}>{part.text}</span> : null
)}
</div>
))}
<form onSubmit={(e) => {
e.preventDefault();
sendMessage({ text: input });
setInput('');
}}>
<input value={input} onChange={(e) => setInput(e.target.value)} />
<button disabled={status === 'streaming'}>Send</button>
</form>
</div>
);
}
Server (app/api/chat/route.ts):
import { streamText, convertToModelMessages } from 'ai';
import type { UIMessage } from 'ai';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
system: 'You are a helpful assistant.',
messages: convertToModelMessages(messages),
});
return result.toUIMessageStreamResponse();
}
Server with tool calling:
import { streamText, convertToModelMessages, tool } from 'ai';
import { z } from 'zod';
import type { UIMessage } from 'ai';
export async function POST(req: Request) {
const { messages }: { messages: UIMessage[] } = await req.json();
const result = streamText({
model: 'openai/gpt-4o',
messages: convertToModelMessages(messages),
tools: {
getWeather: tool({
description: 'Get weather for a city',
inputSchema: z.object({
city: z.string(),
}),
execute: async ({ city }) => {
// API call or mock data
return { city, temp: 72, condition: 'Sunny' };
},
}),
searchWeb: tool({
description: 'Search the web',
inputSchema: z.object({
query: z.string(),
}),
execute: async ({ query }) => {
// Search implementation
return { results: ['...'] };
},
}),
},
});
return result.toUIMessageStreamResponse();
}
// app/api/summarize/route.ts
import { generateText } from 'ai';
export async function POST(req: Request) {
const { text } = await req.json();
const result = await generateText({
model: 'anthropic/claude-4-sonnet',
system: 'You are a summarization expert.',
prompt: `Summarize this text:\n\n${text}`,
});
return Response.json({ summary: result.text });
}
// app/api/search/route.ts
import { embed } from 'ai';
import { openai } from '@ai-sdk/openai';
export async function POST(req: Request) {
const { query } = await req.json();
// Generate embedding for search query
const { embedding } = await embed({
model: openai.textEmbeddingModel('text-embedding-3-small'),
value: query,
});
// Use embedding for similarity search in vector database
// const results = await vectorDB.search(embedding);
return Response.json({ embedding, results: [] });
}
This is the most common and critical mistake. Always use tool() helper!
// ❌ WRONG - Plain object (WILL CAUSE BUILD FAILURE)
import { z } from 'zod';
tools: {
myTool: {
description: 'My tool',
parameters: z.object({ // ❌ Wrong property name
city: z.string(),
}),
execute: async ({ city }) => { ... },
},
}
// Build error: Type '{ description: string; parameters: ... }' is not assignable
// ✅ CORRECT - Use tool() helper (REQUIRED)
import { tool } from 'ai'; // ⚠️ MUST import tool
import { z } from 'zod';
tools: {
myTool: tool({ // ⚠️ MUST use tool() wrapper
description: 'My tool',
inputSchema: z.object({ // ⚠️ Use inputSchema (not parameters)
city: z.string(),
}),
execute: async ({ city }) => { ... },
}),
}
// ❌ WRONG - v4 pattern
const { input, setInput, append } = useChat();
append({ content: 'Hello', role: 'user' });
// ✅ CORRECT - v5 pattern
const { sendMessage } = useChat();
const [input, setInput] = useState('');
sendMessage({ text: 'Hello' });
// ❌ WRONG - v4 pattern
<div>{message.content}</div>
// ✅ CORRECT - v5 parts-based
<div>
{message.parts.map((part, i) =>
part.type === 'text' ? <span key={i}>{part.text}</span> : null
)}
</div>
// ❌ WRONG - passing UIMessages directly
const result = streamText({
model: 'openai/gpt-4o',
messages: messages, // UIMessage[] - type error
});
// ✅ CORRECT - convert to ModelMessage[]
const result = streamText({
model: 'openai/gpt-4o',
messages: convertToModelMessages(messages),
});
// ❌ WRONG - no cleanup
const httpClient = await experimental_createMCPClient({
transport: httpTransport,
});
const tools = await httpClient.tools();
const response = streamText({ model, tools, prompt });
return response.toTextStreamResponse();
// ✅ CORRECT - cleanup in callbacks
const response = streamText({
model,
tools,
prompt,
onFinish: async () => {
await httpClient.close();
},
onError: async () => {
await httpClient.close();
},
});
// ❌ WRONG - using text stream for useChat
return result.toTextStreamResponse(); // Won't work with useChat hook
// ✅ CORRECT - use UI message stream for useChat
return result.toUIMessageStreamResponse();
// ✅ ALSO CORRECT - text stream for non-chat scenarios
// For simple text streaming (not using useChat hook)
return result.toTextStreamResponse();
// ❌ WRONG - using regular model method
const { embedding } = await embed({
model: openai('text-embedding-3-small'), // Wrong method
value: 'text',
});
// ✅ CORRECT - use textEmbeddingModel
const { embedding } = await embed({
model: openai.textEmbeddingModel('text-embedding-3-small'),
value: 'text',
});
When migrating from v4 to v5, update:
append with sendMessage in useChatinput, setInput, handleInputChange from useChat destructuringconst [input, setInput] = useState('')message.content to message.parts.map(...){ text: input } structureconvertToModelMessages is used in API routestoUIMessageStreamResponse() is used (not v4 streaming methods)tool() helper with inputSchema'provider/model-id')UIMessage, ModelMessage)When implementing AI SDK features, ask:
useChat hookgenerateText or streamTextstreamText + toUIMessageStreamResponse()generateTextstreamText + toTextStreamResponse()tool() helper and inputSchema (zod)generateText or streamTextUIMessage[] with parts propertyconvertToModelMessages() to ModelMessage[]message.parts.map(...)'openai/gpt-4o'openai('gpt-4o')openai.textEmbeddingModel('text-embedding-3-small')embed for single valuesembedMany for batchestextEmbeddingModel() method| Task | Function | Key Parameters |
|------|----------|----------------|
| Generate text | generateText() | model, prompt, system, tools |
| Stream text | streamText() | model, messages, tools, onFinish |
| Chat UI | useChat() | api, onFinish, onError |
| Tool calling | tool() | description, inputSchema, execute |
| Text embedding | embed() | model, value |
| Batch embedding | embedMany() | model, values |
| Message conversion | convertToModelMessages() | messages (UIMessage[]) |
| MCP integration | experimental_createMCPClient() | transport |
When in doubt, check the official documentation:
Remember: AI SDK v5 uses string-based model specification, parts-based messages, sendMessage instead of append, and requires convertToModelMessages in API routes.
Convert PyTorch AT_DISPATCH macros to AT_DISPATCH_V2 format in ATen C++ code. Use when porting AT_DISPATCH_ALL_TYPES_AND*, AT_DISPATCH_FLOATING_TYPES*, or other dispatch macros to the new v2 API. For ATen kernel files, CUDA kernels, and native operator implementations.
Write docstrings for PyTorch functions and methods following PyTorch conventions. Use when writing or updating docstrings in PyTorch code.
Statistical models library for Python. Use when you need specific model classes (OLS, GLM, mixed models, ARIMA) with detailed diagnostics, residuals, and inference. Best for econometrics, time series, rigorous inference with coefficient tables. For guided statistical test selection with APA reporting use statistical-analysis.
Answer questions about the AI SDK and help build AI-powered features. Use when developers: (1) Ask about AI SDK functions like generateText, streamText, ToolLoopAgent, embed, or tools, (2) Want to build AI agents, chatbots, RAG systems, or text generation features, (3) Have questions about AI providers (OpenAI, Anthropic, Google, etc.), streaming, tool calling, structured output, or embeddings, (4) Use React hooks like useChat or useCompletion. Triggers on: "AI SDK", "Vercel AI SDK", "generateText", "streamText", "add AI to my app", "build an agent", "tool calling", "structured output", "useChat".
Create an llms.txt file from scratch based on repository structure following the llms.txt specification at https://llmstxt.org/
Use when working directly with the `esm` Python SDK, ESM3 or ESMC model IDs, Forge/Biohub inference clients, or ESMFold2 folding workflows.
Modal is a serverless cloud platform for running Python on demand, including on-demand GPUs. Use when deploying or serving AI/ML models, running GPU-accelerated workloads (training, fine-tuning, inference), serving web endpoints, scheduling batch jobs, or scaling Python code to cloud containers with the Modal SDK.
Use Therapeutics Data Commons through the PyTDC Python package for registry discovery, approved dataset access, task-aware splits, evaluator metrics, benchmark groups, and bounded molecular-oracle workflows.
Take wsimmonds/vercel-ai-sdk from the repository into ~/.claude/skills for personal
use, or into .claude/skills inside a project.
The agent identifies a skill by the name field in its header. Two skills with the
same name cannot sit side by side — one of them will be ignored.
The instructions reference npm, yarn, pnpm.
Without those the skill loads but fails at the first command.