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.
@@ -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.330" : "0.0.0-test";
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 ({
@@ -144,7 +144,7 @@ import {
144
144
  } from "@ai-sdk/provider-utils";
145
145
 
146
146
  // src/version.ts
147
- var VERSION = true ? "6.0.330" : "0.0.0-test";
147
+ var VERSION = true ? "6.0.332" : "0.0.0-test";
148
148
 
149
149
  // src/util/download/download.ts
150
150
  var download = async ({
@@ -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
- By default, the error message is masked for security reasons.
730
- The default error message is "An error occurred."
731
-
732
- Tool execution errors marked `providerExecuted: true` are an exception. They
733
- bypass the UI stream's `onError` callback: strings are forwarded unchanged, and
734
- other error values are serialized with `JSON.stringify`. This preserves provider
735
- error codes when chat history is converted back into model messages. Stream
736
- errors and invalid tool calls still use `onError`, even for provider-executed
737
- tools. Do not rely on this callback to redact provider-executed tool execution
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 errors when calling tools.
691
- By default, these errors are masked for security reasons, and show up as "An error occurred" in the UI.
692
-
693
- Tool execution errors marked `providerExecuted: true` bypass this masking and the
694
- UI stream's `onError` callback. String errors pass through unchanged; other error
695
- values are serialized with `JSON.stringify`. The original error data is needed
696
- when converting persisted UI messages back into model messages, for example to
697
- preserve Anthropic tool error codes. The callback still handles invalid tool
698
- calls and stream errors, regardless of whether a tool is provider-executed.
699
-
700
- To customize masked errors, pass `onError` to `result.toUIMessageStream()` or
701
- `result.toUIMessageStreamResponse()`. This callback does not redact
702
- provider-executed tool execution errors sent to the client.
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
- export function errorHandler(error: unknown) {
706
- if (error == null) {
707
- return 'unknown error';
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
- if (typeof error === 'string') {
711
- return error;
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
- if (error instanceof Error) {
715
- return error.message;
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
- return JSON.stringify(error);
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
- onError: errorHandler,
742
+ redactToolErrors: true,
729
743
  });
730
744
  ```
731
745
 
732
- In case you are using `createUIMessageResponse`, you can use the `onError` function when calling `toUIMessageResponse`:
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
- const response = createUIMessageResponse({
736
- // ...
737
- async execute(dataStream) {
738
- // ...
772
+ return result.toUIMessageStreamResponse({
773
+ onError: error => {
774
+ console.error(error);
775
+ return 'The request failed.';
739
776
  },
740
- onError: error => `Custom error: ${error.message}`,
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 an error, e.g. to log it. Returns the error message to include in the data stream. Defaults to () => "An error occurred." Tool execution errors marked providerExecuted: true bypass this callback: strings pass through unchanged and other values are JSON-stringified to preserve provider error data for model-message round trips. Stream errors and invalid tool calls still use this callback.',
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
- getErrorMessage: errorHandler,
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.330",
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.235",
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.145",
62
- "@ai-sdk/openai": "3.0.141",
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. Stream errors
105
- * and invalid tool calls still use it.
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 = () => 'An error occurred.', // masks errors except provider-executed tool execution errors
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: onError(part.error),
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
- : onError(part.error),
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
  });