@ai-sdk/anthropic 4.0.94 → 4.0.95

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.
@@ -290,7 +290,7 @@ The following optional provider options are available for Anthropic models:
290
290
  Determines how structured outputs are generated. Optional.
291
291
  - `"outputFormat"`: Use the `output_format` parameter to specify the structured output format.
292
292
  - `"jsonTool"`: Use a special `"json"` tool to specify the structured output format.
293
- - `"auto"`: Use `"outputFormat"` when supported, otherwise fall back to `"jsonTool"` (default).
293
+ - `"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.
294
294
 
295
295
  - `metadata` _object_
296
296
 
@@ -366,6 +366,12 @@ Likewise, a `required` or named `toolChoice` is downgraded to
366
366
  `auto` with a warning; instruct the model to use the tool in your prompt and
367
367
  verify that a tool call was made.
368
368
 
369
+ The JSON-tool fallback limits which application tools are available:
370
+ `toolChoice: 'none'` sends only the internal JSON response tool, and a named tool
371
+ choice sends only that application tool alongside the JSON response tool. The
372
+ fallback can produce a JSON response without calling the named application tool;
373
+ verify the selected call if your application requires it.
374
+
369
375
  When the JSON tool cannot be forced (for example with manual thinking) and the
370
376
  model answers in text without calling it, the text of its final answer is
371
377
  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": "4.0.94",
3
+ "version": "4.0.95",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -36,7 +36,7 @@
36
36
  },
37
37
  "dependencies": {
38
38
  "@ai-sdk/provider": "4.0.35",
39
- "@ai-sdk/provider-utils": "5.0.72"
39
+ "@ai-sdk/provider-utils": "5.0.73"
40
40
  },
41
41
  "devDependencies": {
42
42
  "@ai-sdk/test-server": "2.0.4",
@@ -524,6 +524,11 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
524
524
  (config.supportsNativeStructuredOutput ?? true) &&
525
525
  modelSupportsStructuredOutput;
526
526
 
527
+ const supportsResponseSchema =
528
+ responseFormat?.type !== 'json' ||
529
+ responseFormat.schema == null ||
530
+ supportsNativeStructuredOutputSchema(responseFormat.schema);
531
+
527
532
  const supportsStrictTools =
528
533
  (config.supportsStrictTools ?? true) && modelSupportsStructuredOutput;
529
534
 
@@ -531,7 +536,9 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
531
536
  anthropicOptions?.structuredOutputMode ?? 'auto';
532
537
  let useStructuredOutput =
533
538
  structureOutputMode === 'outputFormat' ||
534
- (structureOutputMode === 'auto' && supportsStructuredOutput);
539
+ (structureOutputMode === 'auto' &&
540
+ supportsStructuredOutput &&
541
+ supportsResponseSchema);
535
542
 
536
543
  // The JSON response tool relies on forced tool use, which some models
537
544
  // reject (e.g. Opus 5.5, Fable 5.1). Fall back to native structured
@@ -1134,7 +1141,15 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1134
1141
  } = await prepareTools(
1135
1142
  jsonResponseTool != null
1136
1143
  ? {
1137
- tools: [...(tools ?? []), jsonResponseTool],
1144
+ tools: [
1145
+ ...(tools ?? []).filter(
1146
+ tool =>
1147
+ toolChoice?.type !== 'none' &&
1148
+ (toolChoice?.type !== 'tool' ||
1149
+ tool.name === toolChoice.toolName),
1150
+ ),
1151
+ jsonResponseTool,
1152
+ ],
1138
1153
  toolChoice: { type: 'required' },
1139
1154
  disableParallelToolUse: true,
1140
1155
  cacheControlValidator,
@@ -9,6 +9,7 @@ import {
9
9
  type SharedV4ProviderMetadata,
10
10
  } from '@ai-sdk/provider';
11
11
  import {
12
+ stringifyWithBigIntAndCycles,
12
13
  convertBase64ToUint8Array,
13
14
  convertToBase64,
14
15
  getTopLevelMediaType,
@@ -703,7 +704,7 @@ export async function convertToAnthropicPrompt({
703
704
  case 'json':
704
705
  case 'error-json':
705
706
  default:
706
- contentValue = JSON.stringify(output.value);
707
+ contentValue = stringifyWithBigIntAndCycles(output.value);
707
708
  break;
708
709
  }
709
710
 
@@ -1739,6 +1740,21 @@ function getAnthropicCaller(
1739
1740
 
1740
1741
  // wrap invalid tool call input because Anthropic requires it to be an object
1741
1742
  function toAnthropicToolInput(input: unknown): JSONObject {
1743
+ if (typeof input === 'string') {
1744
+ try {
1745
+ const parsed = secureJsonParse(input);
1746
+ if (
1747
+ typeof parsed === 'object' &&
1748
+ parsed !== null &&
1749
+ !Array.isArray(parsed)
1750
+ ) {
1751
+ return parsed as JSONObject;
1752
+ }
1753
+ } catch {
1754
+ // malformed JSON falls through to rawInvalidInput wrapper
1755
+ }
1756
+ }
1757
+
1742
1758
  return typeof input === 'object' && input !== null && !Array.isArray(input)
1743
1759
  ? (input as JSONObject)
1744
1760
  : { rawInvalidInput: input as JSONValue };
@@ -13,4 +13,5 @@ export type {
13
13
  } from '../anthropic-language-model-options';
14
14
  export { prepareTools } from '../anthropic-prepare-tools';
15
15
  export { sanitizeJsonSchema } from '../sanitize-json-schema';
16
+ export { supportsNativeStructuredOutputSchema } from '../supports-native-structured-output-schema';
16
17
  export { AnthropicSkills } from '../skills/anthropic-skills';