@ai-sdk/anthropic 3.0.137 → 3.0.138

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.
@@ -152,7 +152,7 @@ The following optional provider options are available for Anthropic models:
152
152
 
153
153
  - `"outputFormat"`: Use the `output_format` parameter to specify the structured output format.
154
154
  - `"jsonTool"`: Use a special `"json"` tool to specify the structured output format.
155
- - `"auto"`: Use `"outputFormat"` when supported, otherwise fall back to `"jsonTool"` (default).
155
+ - `"auto"`: Use `"outputFormat"` when the model and schema support it, otherwise fall back to `"jsonTool"` (default). Schemas with recursive references, open objects, or more than ten optional properties use the JSON-tool fallback to avoid rejected requests or lost data.
156
156
 
157
157
  - `metadata` _object_
158
158
 
@@ -205,6 +205,12 @@ Likewise, a `required` or named `toolChoice` is downgraded to
205
205
  `auto` with a warning; instruct the model to use the tool in your prompt and
206
206
  verify that a tool call was made.
207
207
 
208
+ The JSON-tool fallback limits which application tools are available:
209
+ `toolChoice: 'none'` sends only the internal JSON response tool, and a named tool
210
+ choice sends only that application tool alongside the JSON response tool. The
211
+ fallback can produce a JSON response without calling the named application tool;
212
+ verify the selected call if your application requires it.
213
+
208
214
  When the JSON tool cannot be forced (for example with manual thinking) and the
209
215
  model answers in text without calling it, the text of its final answer is
210
216
  returned. Text before a tool call is still dropped.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "3.0.137",
3
+ "version": "3.0.138",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -37,7 +37,7 @@
37
37
  },
38
38
  "dependencies": {
39
39
  "@ai-sdk/provider": "3.0.19",
40
- "@ai-sdk/provider-utils": "4.0.63"
40
+ "@ai-sdk/provider-utils": "4.0.64"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "20.17.24",
@@ -449,6 +449,11 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
449
449
  (this.config.supportsNativeStructuredOutput ?? true) &&
450
450
  modelSupportsStructuredOutput;
451
451
 
452
+ const supportsResponseSchema =
453
+ responseFormat?.type !== 'json' ||
454
+ responseFormat.schema == null ||
455
+ supportsNativeStructuredOutputSchema(responseFormat.schema);
456
+
452
457
  const supportsStrictTools =
453
458
  (this.config.supportsStrictTools ?? true) &&
454
459
  modelSupportsStructuredOutput;
@@ -457,7 +462,9 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
457
462
  anthropicOptions?.structuredOutputMode ?? 'auto';
458
463
  let useStructuredOutput =
459
464
  structureOutputMode === 'outputFormat' ||
460
- (structureOutputMode === 'auto' && supportsStructuredOutput);
465
+ (structureOutputMode === 'auto' &&
466
+ supportsStructuredOutput &&
467
+ supportsResponseSchema);
461
468
 
462
469
  // The JSON response tool relies on forced tool use, which some models
463
470
  // reject (e.g. Opus 5.5, Fable 5.1). Fall back to native structured
@@ -1007,7 +1014,15 @@ export class AnthropicMessagesLanguageModel implements LanguageModelV3 {
1007
1014
  } = await prepareTools(
1008
1015
  jsonResponseTool != null
1009
1016
  ? {
1010
- tools: [...(tools ?? []), jsonResponseTool],
1017
+ tools: [
1018
+ ...(tools ?? []).filter(
1019
+ tool =>
1020
+ toolChoice?.type !== 'none' &&
1021
+ (toolChoice?.type !== 'tool' ||
1022
+ tool.name === toolChoice.toolName),
1023
+ ),
1024
+ jsonResponseTool,
1025
+ ],
1011
1026
  toolChoice: { type: 'required' },
1012
1027
  disableParallelToolUse: true,
1013
1028
  cacheControlValidator,
@@ -8,6 +8,7 @@ import {
8
8
  type SharedV3ProviderMetadata,
9
9
  } from '@ai-sdk/provider';
10
10
  import {
11
+ stringifyWithBigIntAndCycles,
11
12
  convertBase64ToUint8Array,
12
13
  convertToBase64,
13
14
  parseProviderOptions,
@@ -585,7 +586,7 @@ export async function convertToAnthropicMessagesPrompt({
585
586
  case 'json':
586
587
  case 'error-json':
587
588
  default:
588
- contentValue = JSON.stringify(output.value);
589
+ contentValue = stringifyWithBigIntAndCycles(output.value);
589
590
  break;
590
591
  }
591
592
 
@@ -899,7 +900,7 @@ export async function convertToAnthropicMessagesPrompt({
899
900
  type: 'tool_use',
900
901
  id: part.toolCallId,
901
902
  name: part.toolName,
902
- input: part.input,
903
+ input: parseToolCallInput(part.input),
903
904
  ...(caller && { caller }),
904
905
  cache_control: cacheControl,
905
906
  });
@@ -1564,3 +1565,21 @@ function getAnthropicCaller(
1564
1565
 
1565
1566
  return caller?.type === 'direct' ? { type: 'direct' } : undefined;
1566
1567
  }
1568
+
1569
+ function parseToolCallInput(input: unknown): unknown {
1570
+ if (typeof input === 'string') {
1571
+ try {
1572
+ const parsed = secureJsonParse(input);
1573
+ if (
1574
+ typeof parsed === 'object' &&
1575
+ parsed !== null &&
1576
+ !Array.isArray(parsed)
1577
+ ) {
1578
+ return parsed;
1579
+ }
1580
+ } catch {
1581
+ // Preserve malformed input for replay.
1582
+ }
1583
+ }
1584
+ return input;
1585
+ }
@@ -6,3 +6,4 @@ export { anthropicTools } from '../anthropic-tools';
6
6
  export type { AnthropicMessagesModelId } from '../anthropic-messages-options';
7
7
  export { prepareTools } from '../anthropic-prepare-tools';
8
8
  export { sanitizeJsonSchema } from '../sanitize-json-schema';
9
+ export { supportsNativeStructuredOutputSchema } from '../supports-native-structured-output-schema';