|
| 1 | +--- |
| 2 | +title: Migrate AI SDK 4.1 to 4.2 |
| 3 | +description: Learn how to upgrade AI SDK 4.1 to 4.2. |
| 4 | +--- |
| 5 | + |
| 6 | +# Migrate AI SDK 4.1 to 4.2 |
| 7 | + |
| 8 | +<Note> |
| 9 | + Check out the [AI SDK 4.2 release blog |
| 10 | + post](https://vercel.com/blog/ai-sdk-4-2) for more information about the |
| 11 | + release. |
| 12 | +</Note> |
| 13 | + |
| 14 | +This guide will help you upgrade to AI SDK 4.2: |
| 15 | + |
| 16 | +## Stable APIs |
| 17 | + |
| 18 | +The following APIs have been moved to stable and no longer have the `experimental_` prefix: |
| 19 | + |
| 20 | +- `customProvider` |
| 21 | +- `providerOptions` (renamed from `providerMetadata` for provider-specific inputs) |
| 22 | +- `providerMetadata` (for provider-specific outputs) |
| 23 | +- `toolCallStreaming` option for `streamText` |
| 24 | + |
| 25 | +## Dependency Versions |
| 26 | + |
| 27 | +AI SDK requires a non-optional `zod` dependency with version `^3.23.8`. |
| 28 | + |
| 29 | +## UI Message Parts |
| 30 | + |
| 31 | +In AI SDK 4.2, we've redesigned how `useChat` handles model outputs with message parts and multiple steps. |
| 32 | +This is a significant improvement that simplifies rendering complex, multi-modal AI responses in your UI. |
| 33 | + |
| 34 | +### What's Changed |
| 35 | + |
| 36 | +Assistant messages with tool calling now get combined into a single message with multiple parts, rather than creating separate messages for each step. |
| 37 | +This change addresses two key developments in AI applications: |
| 38 | + |
| 39 | +1. **Diverse Output Types**: Models now generate more than just text; they produce reasoning steps, sources, and tool calls. |
| 40 | +2. **Interleaved Outputs**: In multi-step agent use-cases, these different output types are frequently interleaved. |
| 41 | + |
| 42 | +### Benefits of the New Approach |
| 43 | + |
| 44 | +Previously, `useChat` stored different output types separately, which made it challenging to maintain the correct sequence in your UI when these elements were interleaved in a response, |
| 45 | +and led to multiple consecutive assistant messages when there were tool calls. For example: |
| 46 | + |
| 47 | +```javascript |
| 48 | +message.content = "Final answer: 42"; |
| 49 | +message.reasoning = "First I'll calculate X, then Y..."; |
| 50 | +message.toolInvocations = [{toolName: "calculator", args: {...}}]; |
| 51 | +``` |
| 52 | + |
| 53 | +This structure was limiting. The new message parts approach replaces separate properties with an ordered array that preserves the exact sequence: |
| 54 | + |
| 55 | +```javascript |
| 56 | +message.parts = [ |
| 57 | + { type: "text", text: "Final answer: 42" }, |
| 58 | + { type: "reasoning", reasoning: "First I'll calculate X, then Y..." }, |
| 59 | + { type: "tool-invocation", toolInvocation: { toolName: "calculator", args: {...} } }, |
| 60 | +]; |
| 61 | +``` |
| 62 | + |
| 63 | +### Migration |
| 64 | + |
| 65 | +Existing applications using the previous message format will need to update their UI components to handle the new `parts` array. |
| 66 | +The fields from the previous format are still available for backward compatibility, but we recommend migrating to the new format for better support of multi-modal and multi-step interactions. |
| 67 | + |
| 68 | +You can use the `useChat` hook with the new message parts as follows: |
| 69 | + |
| 70 | +```javascript |
| 71 | +function Chat() { |
| 72 | + const { messages } = useChat(); |
| 73 | + return ( |
| 74 | + <div> |
| 75 | + {messages.map(message => |
| 76 | + message.parts.map((part, i) => { |
| 77 | + switch (part.type) { |
| 78 | + case 'text': |
| 79 | + return <p key={i}>{part.text}</p>; |
| 80 | + case 'source': |
| 81 | + return <p key={i}>{part.source.url}</p>; |
| 82 | + case 'reasoning': |
| 83 | + return <div key={i}>{part.reasoning}</div>; |
| 84 | + case 'tool-invocation': |
| 85 | + return <div key={i}>{part.toolInvocation.toolName}</div>; |
| 86 | + case 'file': |
| 87 | + return ( |
| 88 | + <img |
| 89 | + key={i} |
| 90 | + src={`data:${part.mimeType};base64,${part.data}`} |
| 91 | + /> |
| 92 | + ); |
| 93 | + } |
| 94 | + }), |
| 95 | + )} |
| 96 | + </div> |
| 97 | + ); |
| 98 | +} |
| 99 | +``` |
0 commit comments