ai 6.0.330 → 6.0.332
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +17 -0
- package/dist/index.d.mts +20 -2
- package/dist/index.d.ts +20 -2
- package/dist/index.js +379 -267
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +379 -267
- package/dist/index.mjs.map +1 -1
- package/dist/internal/index.js +1 -1
- package/dist/internal/index.mjs +1 -1
- package/docs/02-foundations/02-providers-and-models.mdx +1 -0
- package/docs/04-ai-sdk-ui/02-chatbot.mdx +9 -10
- package/docs/04-ai-sdk-ui/03-chatbot-tool-usage.mdx +70 -33
- package/docs/07-reference/01-ai-sdk-core/02-stream-text.mdx +8 -1
- package/docs/09-troubleshooting/12-use-chat-an-error-occurred.mdx +3 -1
- package/package.json +4 -4
- package/src/generate-text/stream-text-result.ts +21 -2
- package/src/generate-text/stream-text.ts +25 -3
- package/src/ui/validate-ui-messages.ts +424 -266
package/dist/internal/index.js
CHANGED
|
@@ -164,7 +164,7 @@ function detectMediaType({
|
|
|
164
164
|
var import_provider_utils2 = require("@ai-sdk/provider-utils");
|
|
165
165
|
|
|
166
166
|
// src/version.ts
|
|
167
|
-
var VERSION = true ? "6.0.
|
|
167
|
+
var VERSION = true ? "6.0.332" : "0.0.0-test";
|
|
168
168
|
|
|
169
169
|
// src/util/download/download.ts
|
|
170
170
|
var download = async ({
|
package/dist/internal/index.mjs
CHANGED
|
@@ -155,6 +155,7 @@ Here are the capabilities of popular models:
|
|
|
155
155
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-opus-4-0` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
156
156
|
| [Anthropic](/providers/ai-sdk-providers/anthropic) | `claude-sonnet-4-0` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
157
157
|
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `gemini-4-carbon` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
158
|
+
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `gemini-4-hydrogen` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
158
159
|
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `gemini-3.8-flash` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
159
160
|
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `gemini-3.1-pro-preview` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
160
161
|
| [Google Generative AI](/providers/ai-sdk-providers/google-generative-ai) | `gemini-3-pro-preview` | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> | <Check size={18} /> |
|
|
@@ -726,16 +726,15 @@ With `streamText`, you can control how error messages and usage information are
|
|
|
726
726
|
|
|
727
727
|
### Error Messages
|
|
728
728
|
|
|
729
|
-
|
|
730
|
-
|
|
731
|
-
|
|
732
|
-
|
|
733
|
-
|
|
734
|
-
|
|
735
|
-
|
|
736
|
-
errors
|
|
737
|
-
|
|
738
|
-
errors before they reach the client.
|
|
729
|
+
Stream errors are masked by default. The default message is "An error occurred."
|
|
730
|
+
|
|
731
|
+
Failed tool calls keep the error text that the model received, so stored UI
|
|
732
|
+
messages preserve the history sent back on later requests. A custom `onError`
|
|
733
|
+
also handles failed tool calls by default and may replace that text. Set
|
|
734
|
+
`redactToolErrors: false` when your callback should handle stream errors only.
|
|
735
|
+
Tool execution errors marked `providerExecuted: true` always bypass `onError`.
|
|
736
|
+
See [Errors](/docs/ai-sdk-ui/chatbot-tool-usage#errors) for safe tool errors
|
|
737
|
+
and redaction details.
|
|
739
738
|
|
|
740
739
|
You can forward error messages or send your own error message by providing an `onError` function:
|
|
741
740
|
|
|
@@ -687,37 +687,51 @@ export async function POST(req: Request) {
|
|
|
687
687
|
|
|
688
688
|
## Errors
|
|
689
689
|
|
|
690
|
-
Language models can make
|
|
691
|
-
|
|
692
|
-
|
|
693
|
-
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
700
|
-
|
|
701
|
-
|
|
702
|
-
|
|
690
|
+
Language models can make invalid tool calls, and tools can fail while they run.
|
|
691
|
+
|
|
692
|
+
By default, failed tool-call errors are sent to the client with the same text
|
|
693
|
+
that `streamText` uses for the model's tool result. During a multi-step call,
|
|
694
|
+
the model receives that text too. Preserving it in UI messages keeps the
|
|
695
|
+
history consistent on later requests.
|
|
696
|
+
|
|
697
|
+
<Note type="warning">
|
|
698
|
+
Tool errors can contain server details such as host names, file paths, or
|
|
699
|
+
credentials. A tool call can also end the model request: the client may receive
|
|
700
|
+
the error before the model has received it in a later step. To keep details
|
|
701
|
+
away from the model and the client, catch the error in the tool's
|
|
702
|
+
`execute` function and throw an error with a safe message.
|
|
703
|
+
</Note>
|
|
703
704
|
|
|
704
705
|
```tsx
|
|
705
|
-
|
|
706
|
-
|
|
707
|
-
|
|
708
|
-
}
|
|
706
|
+
const weatherTool = tool({
|
|
707
|
+
description: 'Get the weather in a location',
|
|
708
|
+
inputSchema: z.object({ location: z.string() }),
|
|
709
|
+
execute: async ({ location }) => {
|
|
710
|
+
try {
|
|
711
|
+
return await getWeather(location);
|
|
712
|
+
} catch (error) {
|
|
713
|
+
console.error(error);
|
|
714
|
+
throw new Error('The weather service is unavailable.');
|
|
715
|
+
}
|
|
716
|
+
},
|
|
717
|
+
});
|
|
718
|
+
```
|
|
709
719
|
|
|
710
|
-
|
|
711
|
-
|
|
712
|
-
|
|
720
|
+
The `execute` function does not run for an invalid tool call. Its validation
|
|
721
|
+
error may include the input generated by the model or details from the input
|
|
722
|
+
schema. Set `redactToolErrors: true` to replace this text before it reaches
|
|
723
|
+
the client.
|
|
713
724
|
|
|
714
|
-
|
|
715
|
-
|
|
716
|
-
|
|
725
|
+
Tool execution errors marked `providerExecuted: true` are always sent
|
|
726
|
+
unchanged. String errors pass through; other values are serialized with
|
|
727
|
+
`JSON.stringify` to preserve provider error data. Invalid tool calls of
|
|
728
|
+
provider-executed tools use `onError`.
|
|
717
729
|
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
|
|
730
|
+
### Redacting tool errors
|
|
731
|
+
|
|
732
|
+
Set `redactToolErrors: true` to replace failed tool-call errors with the result
|
|
733
|
+
of `onError`. When no callback is provided, the replacement is
|
|
734
|
+
"An error occurred."
|
|
721
735
|
|
|
722
736
|
```tsx
|
|
723
737
|
const result = streamText({
|
|
@@ -725,18 +739,41 @@ const result = streamText({
|
|
|
725
739
|
});
|
|
726
740
|
|
|
727
741
|
return result.toUIMessageStreamResponse({
|
|
728
|
-
|
|
742
|
+
redactToolErrors: true,
|
|
729
743
|
});
|
|
730
744
|
```
|
|
731
745
|
|
|
732
|
-
|
|
746
|
+
When you provide a custom `onError`, it handles failed tool calls by default.
|
|
747
|
+
Set `redactToolErrors: false` to use it for stream errors only. Provider-
|
|
748
|
+
executed tool execution errors are never redacted.
|
|
749
|
+
|
|
750
|
+
Redacting a failed tool-call error changes the history sent to the model on a
|
|
751
|
+
later request. Models that bind reasoning to earlier messages can reject that
|
|
752
|
+
history. To avoid this, set `sendReasoning: false`. For Anthropic's temporary
|
|
753
|
+
recovery option, see [Changing tools or instructions](https://platform.claude.com/docs/en/build-with-claude/preserved-thinking/change-tools-or-instructions).
|
|
754
|
+
|
|
755
|
+
### Tool error text and message history
|
|
756
|
+
|
|
757
|
+
During a server-side multi-step call, the model receives the full error text
|
|
758
|
+
before it continues. If you redact that text for the UI, converting the stored
|
|
759
|
+
messages with `convertToModelMessages` changes the tool result in the next
|
|
760
|
+
request. Models that bind reasoning to earlier messages can reject the changed
|
|
761
|
+
history. With Anthropic adaptive thinking, `thinking.blockBinding.prefixMismatchBehavior`
|
|
762
|
+
can be set to `drop_block` to drop reasoning that no longer matches. A call
|
|
763
|
+
that ends after the failed tool may send the error text to the client before
|
|
764
|
+
the model has received it.
|
|
765
|
+
|
|
766
|
+
### Stream errors
|
|
767
|
+
|
|
768
|
+
Errors from the model provider are masked by default and appear as
|
|
769
|
+
"An error occurred." in the UI. Customize them with `onError`:
|
|
733
770
|
|
|
734
771
|
```tsx
|
|
735
|
-
|
|
736
|
-
|
|
737
|
-
|
|
738
|
-
|
|
772
|
+
return result.toUIMessageStreamResponse({
|
|
773
|
+
onError: error => {
|
|
774
|
+
console.error(error);
|
|
775
|
+
return 'The request failed.';
|
|
739
776
|
},
|
|
740
|
-
|
|
777
|
+
redactToolErrors: false,
|
|
741
778
|
});
|
|
742
779
|
```
|
|
@@ -3571,7 +3571,14 @@ To see `streamText` in action, check out [these examples](#examples).
|
|
|
3571
3571
|
type: '(error: unknown) => string',
|
|
3572
3572
|
isOptional: true,
|
|
3573
3573
|
description:
|
|
3574
|
-
'Process
|
|
3574
|
+
'Process stream errors. Also handles failed tool calls when redactToolErrors is enabled, which defaults to true when onError is provided. Provider-executed tool execution errors bypass this callback; invalid tool calls of provider-executed tools still use it.',
|
|
3575
|
+
},
|
|
3576
|
+
{
|
|
3577
|
+
name: 'redactToolErrors',
|
|
3578
|
+
type: 'boolean',
|
|
3579
|
+
isOptional: true,
|
|
3580
|
+
description:
|
|
3581
|
+
'Replace failed tool-call error text with the result of onError. Defaults to true when onError is provided, and false otherwise.',
|
|
3575
3582
|
},
|
|
3576
3583
|
{
|
|
3577
3584
|
name: 'consumeSseStream',
|
|
@@ -14,6 +14,8 @@ I am using [`useChat`](/docs/reference/ai-sdk-ui/use-chat) and I get the error "
|
|
|
14
14
|
Error messages from `streamText` are masked by default when using `toDataStreamResponse` for security reasons (secure-by-default).
|
|
15
15
|
This prevents leaking sensitive information to the client.
|
|
16
16
|
|
|
17
|
+
Failed tool calls keep the error text the model received by default. A custom `onError` also handles failed tool calls; set `redactToolErrors: false` to handle stream errors only. See [tool error handling](/docs/ai-sdk-ui/chatbot-tool-usage#errors).
|
|
18
|
+
|
|
17
19
|
## Solution
|
|
18
20
|
|
|
19
21
|
To forward error details to the client or to log errors, use the `getErrorMessage` function when calling `toDataStreamResponse`.
|
|
@@ -42,7 +44,7 @@ const result = streamText({
|
|
|
42
44
|
});
|
|
43
45
|
|
|
44
46
|
return result.toUIMessageStreamResponse({
|
|
45
|
-
|
|
47
|
+
onError: errorHandler,
|
|
46
48
|
});
|
|
47
49
|
```
|
|
48
50
|
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "ai",
|
|
3
|
-
"version": "6.0.
|
|
3
|
+
"version": "6.0.332",
|
|
4
4
|
"description": "AI SDK by Vercel - build apps like ChatGPT, Claude, Gemini, and more with a single interface for any model using the Vercel AI Gateway or go direct to OpenAI, Anthropic, Google, or any other model provider.",
|
|
5
5
|
"license": "Apache-2.0",
|
|
6
6
|
"sideEffects": false,
|
|
@@ -45,7 +45,7 @@
|
|
|
45
45
|
},
|
|
46
46
|
"dependencies": {
|
|
47
47
|
"@opentelemetry/api": "^1.9.0",
|
|
48
|
-
"@ai-sdk/gateway": "3.0.
|
|
48
|
+
"@ai-sdk/gateway": "3.0.237",
|
|
49
49
|
"@ai-sdk/provider": "3.0.19",
|
|
50
50
|
"@ai-sdk/provider-utils": "4.0.66"
|
|
51
51
|
},
|
|
@@ -58,8 +58,8 @@
|
|
|
58
58
|
"tsx": "^4.19.2",
|
|
59
59
|
"typescript": "5.8.3",
|
|
60
60
|
"zod": "3.25.76",
|
|
61
|
-
"@ai-sdk/google": "3.0.
|
|
62
|
-
"@ai-sdk/openai": "3.0.
|
|
61
|
+
"@ai-sdk/google": "3.0.146",
|
|
62
|
+
"@ai-sdk/openai": "3.0.143",
|
|
63
63
|
"@ai-sdk/perplexity": "3.1.12",
|
|
64
64
|
"@ai-sdk/test-server": "1.0.6",
|
|
65
65
|
"@vercel/ai-tsconfig": "0.0.0"
|
|
@@ -99,14 +99,33 @@ export type UIMessageStreamOptions<UI_MESSAGE extends UIMessage> = {
|
|
|
99
99
|
/**
|
|
100
100
|
* Process an error, e.g. to log it. Default to `() => 'An error occurred.'`.
|
|
101
101
|
*
|
|
102
|
+
* This callback handles stream errors. It also handles failed tool calls
|
|
103
|
+
* (tool execution errors and invalid tool calls) when `redactToolErrors` is
|
|
104
|
+
* enabled, which is the default when this callback is provided.
|
|
105
|
+
*
|
|
102
106
|
* Tool execution errors marked `providerExecuted: true` bypass this callback.
|
|
103
107
|
* Their strings pass through unchanged; other values are JSON-stringified to
|
|
104
|
-
* preserve provider error data for model-message round trips.
|
|
105
|
-
*
|
|
108
|
+
* preserve provider error data for model-message round trips. Invalid tool
|
|
109
|
+
* calls of provider-executed tools always use it.
|
|
106
110
|
*
|
|
107
111
|
* @returns error message to include in the data stream.
|
|
108
112
|
*/
|
|
109
113
|
onError?: (error: unknown) => string;
|
|
114
|
+
|
|
115
|
+
/**
|
|
116
|
+
* Replace the error text of failed tool calls (tool execution errors and
|
|
117
|
+
* invalid tool calls) with the result of `onError`.
|
|
118
|
+
*
|
|
119
|
+
* By default, the client receives the same error text as the model. UI
|
|
120
|
+
* messages are sent back to the model on the next request, so a different
|
|
121
|
+
* text changes the history that the model's reasoning and answers are based
|
|
122
|
+
* on. Some models reject such a history, for example Anthropic models with
|
|
123
|
+
* preserved thinking. To keep error details away from the client, throw a
|
|
124
|
+
* safe error from the tool's `execute` function.
|
|
125
|
+
*
|
|
126
|
+
* Defaults to `true` when `onError` is provided, and `false` otherwise.
|
|
127
|
+
*/
|
|
128
|
+
redactToolErrors?: boolean;
|
|
110
129
|
};
|
|
111
130
|
|
|
112
131
|
export type ConsumeStreamOptions = {
|
|
@@ -2741,10 +2741,26 @@ class DefaultStreamTextResult<
|
|
|
2741
2741
|
sendSources = false,
|
|
2742
2742
|
sendStart = true,
|
|
2743
2743
|
sendFinish = true,
|
|
2744
|
-
onError
|
|
2744
|
+
onError: onErrorOption,
|
|
2745
|
+
redactToolErrors = onErrorOption != null,
|
|
2745
2746
|
}: UIMessageStreamOptions<UI_MESSAGE> = {}): AsyncIterableStream<
|
|
2746
2747
|
InferUIMessageChunk<UI_MESSAGE>
|
|
2747
2748
|
> {
|
|
2749
|
+
const onError = onErrorOption ?? (() => 'An error occurred.');
|
|
2750
|
+
// UI messages become the next request's history, so keep this text aligned
|
|
2751
|
+
// with what the model received unless the application opts into redaction.
|
|
2752
|
+
const getToolErrorText = (error: unknown) => {
|
|
2753
|
+
if (redactToolErrors) {
|
|
2754
|
+
return onError(error);
|
|
2755
|
+
}
|
|
2756
|
+
|
|
2757
|
+
try {
|
|
2758
|
+
return getErrorMessage(error) ?? onError(error);
|
|
2759
|
+
} catch {
|
|
2760
|
+
return onError(error);
|
|
2761
|
+
}
|
|
2762
|
+
};
|
|
2763
|
+
|
|
2748
2764
|
let outcome: UIMessageStreamOutcome = { status: 'unknown' };
|
|
2749
2765
|
let hasFatalFailure = false;
|
|
2750
2766
|
|
|
@@ -3004,7 +3020,9 @@ class DefaultStreamTextResult<
|
|
|
3004
3020
|
? { toolMetadata: part.toolMetadata }
|
|
3005
3021
|
: {}),
|
|
3006
3022
|
...(dynamic != null ? { dynamic } : {}),
|
|
3007
|
-
errorText:
|
|
3023
|
+
errorText: part.providerExecuted
|
|
3024
|
+
? onError(part.error)
|
|
3025
|
+
: getToolErrorText(part.error),
|
|
3008
3026
|
...(part.title != null ? { title: part.title } : {}),
|
|
3009
3027
|
});
|
|
3010
3028
|
} else {
|
|
@@ -3088,7 +3106,7 @@ class DefaultStreamTextResult<
|
|
|
3088
3106
|
? typeof part.error === 'string'
|
|
3089
3107
|
? part.error
|
|
3090
3108
|
: JSON.stringify(part.error)
|
|
3091
|
-
:
|
|
3109
|
+
: getToolErrorText(part.error),
|
|
3092
3110
|
...(part.providerExecuted != null
|
|
3093
3111
|
? { providerExecuted: part.providerExecuted }
|
|
3094
3112
|
: {}),
|
|
@@ -3228,6 +3246,7 @@ class DefaultStreamTextResult<
|
|
|
3228
3246
|
sendFinish,
|
|
3229
3247
|
sendStart,
|
|
3230
3248
|
onError,
|
|
3249
|
+
redactToolErrors,
|
|
3231
3250
|
...init
|
|
3232
3251
|
}: UIMessageStreamResponseInit & UIMessageStreamOptions<UI_MESSAGE> = {},
|
|
3233
3252
|
) {
|
|
@@ -3243,6 +3262,7 @@ class DefaultStreamTextResult<
|
|
|
3243
3262
|
sendFinish,
|
|
3244
3263
|
sendStart,
|
|
3245
3264
|
onError,
|
|
3265
|
+
redactToolErrors,
|
|
3246
3266
|
}),
|
|
3247
3267
|
...init,
|
|
3248
3268
|
});
|
|
@@ -3266,6 +3286,7 @@ class DefaultStreamTextResult<
|
|
|
3266
3286
|
sendFinish,
|
|
3267
3287
|
sendStart,
|
|
3268
3288
|
onError,
|
|
3289
|
+
redactToolErrors,
|
|
3269
3290
|
...init
|
|
3270
3291
|
}: UIMessageStreamResponseInit &
|
|
3271
3292
|
UIMessageStreamOptions<UI_MESSAGE> = {}): Response {
|
|
@@ -3280,6 +3301,7 @@ class DefaultStreamTextResult<
|
|
|
3280
3301
|
sendFinish,
|
|
3281
3302
|
sendStart,
|
|
3282
3303
|
onError,
|
|
3304
|
+
redactToolErrors,
|
|
3283
3305
|
}),
|
|
3284
3306
|
...init,
|
|
3285
3307
|
});
|