Skip to content

Commit 5bc638d

Browse files
authoredMar 21, 2025··
AI SDK 4.2 (#5290)
1 parent e1f970a commit 5bc638d

14 files changed

+158
-21
lines changed
 

‎.changeset/big-squids-brush.md

+34
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
---
2+
'ai': minor
3+
'@ai-sdk/amazon-bedrock': minor
4+
'@ai-sdk/anthropic': minor
5+
'@ai-sdk/azure': minor
6+
'@ai-sdk/cerebras': minor
7+
'@ai-sdk/codemod': minor
8+
'@ai-sdk/cohere': minor
9+
'@ai-sdk/deepinfra': minor
10+
'@ai-sdk/deepseek': minor
11+
'@ai-sdk/fal': minor
12+
'@ai-sdk/fireworks': minor
13+
'@ai-sdk/google': minor
14+
'@ai-sdk/google-vertex': minor
15+
'@ai-sdk/groq': minor
16+
'@ai-sdk/luma': minor
17+
'@ai-sdk/mistral': minor
18+
'@ai-sdk/openai': minor
19+
'@ai-sdk/openai-compatible': minor
20+
'@ai-sdk/perplexity': minor
21+
'@ai-sdk/provider': minor
22+
'@ai-sdk/provider-utils': minor
23+
'@ai-sdk/react': minor
24+
'@ai-sdk/replicate': minor
25+
'@ai-sdk/solid': minor
26+
'@ai-sdk/svelte': minor
27+
'@ai-sdk/togetherai': minor
28+
'@ai-sdk/ui-utils': minor
29+
'@ai-sdk/valibot': minor
30+
'@ai-sdk/vue': minor
31+
'@ai-sdk/xai': minor
32+
---
33+
34+
AI SDK 4.2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
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+
```
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Migrate AI SDK 4.0 to 4.1
3+
description: Learn how to upgrade AI SDK 4.0 to 4.1.
4+
---
5+
6+
# Migrate AI SDK 4.0 to 4.1
7+
8+
<Note>
9+
Check out the [AI SDK 4.1 release blog
10+
post](https://vercel.com/blog/ai-sdk-4-1) for more information about the
11+
release.
12+
</Note>
13+
14+
No breaking changes in this release.

‎content/docs/08-migration-guides/index.mdx

+6
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,14 @@ collapsed: true
66

77
# Migration Guides
88

9+
- [ Migrate AI SDK 4.1 to 4.2 ](/docs/migration-guides/migration-guide-4-2)
10+
- [ Migrate AI SDK 4.0 to 4.1 ](/docs/migration-guides/migration-guide-4-1)
911
- [ Migrate AI SDK 3.4 to 4.0 ](/docs/migration-guides/migration-guide-4-0)
1012
- [ Migrate AI SDK 3.3 to 3.4 ](/docs/migration-guides/migration-guide-3-4)
1113
- [ Migrate AI SDK 3.2 to 3.3 ](/docs/migration-guides/migration-guide-3-3)
1214
- [ Migrate AI SDK 3.1 to 3.2 ](/docs/migration-guides/migration-guide-3-2)
1315
- [ Migrate AI SDK 3.0 to 3.1 ](/docs/migration-guides/migration-guide-3-1)
16+
17+
## Versioning
18+
19+
- [ Versioning ](/docs/migration-guides/versioning)

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -79,4 +79,4 @@
7979
"esbuild"
8080
]
8181
}
82-
}
82+
}

‎packages/ai/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -90,14 +90,11 @@
9090
},
9191
"peerDependencies": {
9292
"react": "^18 || ^19 || ^19.0.0-rc",
93-
"zod": "^3.0.0"
93+
"zod": "^3.23.8"
9494
},
9595
"peerDependenciesMeta": {
9696
"react": {
9797
"optional": true
98-
},
99-
"zod": {
100-
"optional": true
10198
}
10299
},
103100
"engines": {

‎packages/provider-utils/package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@
5151
"zod": "3.23.8"
5252
},
5353
"peerDependencies": {
54-
"zod": "^3.0.0"
55-
},
56-
"peerDependenciesMeta": {
57-
"zod": {
58-
"optional": true
59-
}
54+
"zod": "^3.23.8"
6055
},
6156
"engines": {
6257
"node": ">=18"

‎packages/react/package.json

+1-4
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,9 @@
5353
},
5454
"peerDependencies": {
5555
"react": "^18 || ^19 || ^19.0.0-rc",
56-
"zod": "^3.0.0"
56+
"zod": "^3.23.8"
5757
},
5858
"peerDependenciesMeta": {
59-
"react": {
60-
"optional": true
61-
},
6259
"zod": {
6360
"optional": true
6461
}

‎packages/ui-utils/package.json

+1-6
Original file line numberDiff line numberDiff line change
@@ -51,12 +51,7 @@
5151
"zod": "3.23.8"
5252
},
5353
"peerDependencies": {
54-
"zod": "^3.0.0"
55-
},
56-
"peerDependenciesMeta": {
57-
"zod": {
58-
"optional": true
59-
}
54+
"zod": "^3.23.8"
6055
},
6156
"engines": {
6257
"node": ">=18"

0 commit comments

Comments
 (0)
Please sign in to comment.