跳到主要内容
非官方简体中文翻译,内容版权归 Mastra 所有,遵循 Apache-2.0 许可。

Migrate from AI SDK v4 to v5

Looking for integration docs? See Using AI SDK.

Follow the official AI SDK v5 Migration Guide for all AI SDK core breaking changes, package updates, and API changes.

This guide covers only the Mastra-specific aspects of the migration.

  • Data compatibility: New data stored in v5 format will no longer work if you downgrade from v5 to v4
  • Backup recommendation: Keep DB backups from before you upgrade to v5

Memory and storage
Memory and storage的直接链接

Mastra automatically handles AI SDK v4 data using its internal MessageList class, which manages format conversion, including v4 to v5. No database migrations are required. Your existing messages are translated on the fly and continue working after you upgrade.

Message format conversion
Message format conversion的直接链接

For cases where you need to manually convert messages between AI SDK and Mastra formats, use the convertMessages() utility:

import { convertMessages } from '@mastra/core/agent'

// Convert AI SDK v4 messages to v5
const aiv5Messages = convertMessages(aiv4Messages).to('AIV5.UI')

// Convert Mastra messages to AI SDK v5
const aiv5ModelMessages = convertMessages(mastraMessages).to('AIV5.Model')

// Supported output formats:
// 'Mastra.V2', 'AIV4.UI', 'AIV4.Core', 'AIV5.UI', 'AIV5.Model', 'AIV6.UI'

This utility is helpful when you want to fetch messages directly from your storage DB and convert them for use in AI SDK.

Type inference for tools
Type inference for tools的直接链接

When using tools with TypeScript in AI SDK v5, Mastra provides type inference helpers to ensure type safety for your tool inputs and outputs.

InferUITool
inferuitool的直接链接

The InferUITool type helper infers the input and output types of a single Mastra tool:

app/types.ts
import { InferUITool, createTool } from '@mastra/core/tools'
import { z } from 'zod'

const weatherTool = createTool({
id: 'get-weather',
description: 'Get the current weather',
inputSchema: z.object({
location: z.string().describe('The city and state'),
}),
outputSchema: z.object({
temperature: z.number(),
conditions: z.string(),
}),
execute: async inputData => {
return {
temperature: 72,
conditions: 'sunny',
}
},
})

// Infer the types from the tool
type WeatherUITool = InferUITool<typeof weatherTool>
// This creates:
// {
// input: { location: string };
// output: { temperature: number; conditions: string };
// }

InferUITools
inferuitools的直接链接

The InferUITools type helper infers the input and output types of multiple tools:

app/mastra/tools.ts
import { InferUITools, createTool } from '@mastra/core/tools'
import { z } from 'zod'

// Using weatherTool from the previous example
const tools = {
weather: weatherTool,
calculator: createTool({
id: 'calculator',
description: 'Perform basic arithmetic',
inputSchema: z.object({
operation: z.enum(['add', 'subtract', 'multiply', 'divide']),
a: z.number(),
b: z.number(),
}),
outputSchema: z.object({
result: z.number(),
}),
execute: async inputData => {
// implementation...
return { result: 0 }
},
}),
}

// Infer types from the tool set
export type MyUITools = InferUITools<typeof tools>
// This creates:
// {
// weather: { input: { location: string }; output: { temperature: number; conditions: string } };
// calculator: { input: { operation: "add" | "subtract" | "multiply" | "divide"; a: number; b: number }; output: { result: number } };
// }

These type helpers provide full TypeScript support when using Mastra tools with AI SDK v5 UI components, ensuring type safety across your application.