@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.
@@ -1176,4 +1176,15 @@ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cache
1176
1176
  */
1177
1177
  declare function sanitizeJsonSchema(schema: JSONSchema7): JSONSchema7;
1178
1178
 
1179
- export { AnthropicMessagesLanguageModel, type AnthropicMessagesModelId, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema };
1179
+ /**
1180
+ * Whether Anthropic's native structured outputs (`output_config.format`) can
1181
+ * represent a schema without rejecting it or losing data.
1182
+ *
1183
+ * Rejected: self-referencing schemas, and schemas with too many optional
1184
+ * properties. Lossy: open objects (maps, free-form objects), which native
1185
+ * structured outputs close with `additionalProperties: false`, so the model
1186
+ * can only return `{}` for them.
1187
+ */
1188
+ declare function supportsNativeStructuredOutputSchema(schema: JSONSchema7): boolean;
1189
+
1190
+ export { AnthropicMessagesLanguageModel, type AnthropicMessagesModelId, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema, supportsNativeStructuredOutputSchema };
@@ -1176,4 +1176,15 @@ declare function prepareTools({ tools, toolChoice, disableParallelToolUse, cache
1176
1176
  */
1177
1177
  declare function sanitizeJsonSchema(schema: JSONSchema7): JSONSchema7;
1178
1178
 
1179
- export { AnthropicMessagesLanguageModel, type AnthropicMessagesModelId, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema };
1179
+ /**
1180
+ * Whether Anthropic's native structured outputs (`output_config.format`) can
1181
+ * represent a schema without rejecting it or losing data.
1182
+ *
1183
+ * Rejected: self-referencing schemas, and schemas with too many optional
1184
+ * properties. Lossy: open objects (maps, free-form objects), which native
1185
+ * structured outputs close with `additionalProperties: false`, so the model
1186
+ * can only return `{}` for them.
1187
+ */
1188
+ declare function supportsNativeStructuredOutputSchema(schema: JSONSchema7): boolean;
1189
+
1190
+ export { AnthropicMessagesLanguageModel, type AnthropicMessagesModelId, anthropicTools, getModelCapabilities, prepareTools, sanitizeJsonSchema, supportsNativeStructuredOutputSchema };
@@ -24,7 +24,8 @@ __export(index_exports, {
24
24
  anthropicTools: () => anthropicTools,
25
25
  getModelCapabilities: () => getModelCapabilities,
26
26
  prepareTools: () => prepareTools,
27
- sanitizeJsonSchema: () => sanitizeJsonSchema
27
+ sanitizeJsonSchema: () => sanitizeJsonSchema,
28
+ supportsNativeStructuredOutputSchema: () => supportsNativeStructuredOutputSchema
28
29
  });
29
30
  module.exports = __toCommonJS(index_exports);
30
31
 
@@ -2791,7 +2792,7 @@ async function convertToAnthropicMessagesPrompt({
2791
2792
  case "json":
2792
2793
  case "error-json":
2793
2794
  default:
2794
- contentValue = JSON.stringify(output.value);
2795
+ contentValue = (0, import_provider_utils16.stringifyWithBigIntAndCycles)(output.value);
2795
2796
  break;
2796
2797
  }
2797
2798
  const toolsetName = getAnthropicToolsetName({
@@ -3029,7 +3030,7 @@ async function convertToAnthropicMessagesPrompt({
3029
3030
  type: "tool_use",
3030
3031
  id: part.toolCallId,
3031
3032
  name: part.toolName,
3032
- input: part.input,
3033
+ input: parseToolCallInput(part.input),
3033
3034
  ...caller && { caller },
3034
3035
  cache_control: cacheControl
3035
3036
  });
@@ -3514,6 +3515,18 @@ function getAnthropicCaller(providerOptions) {
3514
3515
  }
3515
3516
  return (caller == null ? void 0 : caller.type) === "direct" ? { type: "direct" } : void 0;
3516
3517
  }
3518
+ function parseToolCallInput(input) {
3519
+ if (typeof input === "string") {
3520
+ try {
3521
+ const parsed = (0, import_provider_utils16.secureJsonParse)(input);
3522
+ if (typeof parsed === "object" && parsed !== null && !Array.isArray(parsed)) {
3523
+ return parsed;
3524
+ }
3525
+ } catch (e) {
3526
+ }
3527
+ }
3528
+ return input;
3529
+ }
3517
3530
 
3518
3531
  // src/map-anthropic-stop-reason.ts
3519
3532
  function mapAnthropicStopReason({
@@ -4106,9 +4119,10 @@ var AnthropicMessagesLanguageModel = class {
4106
4119
  }
4107
4120
  const isAnthropicModel = isKnownModel || this.modelId.includes("claude-");
4108
4121
  const supportsStructuredOutput = ((_b = this.config.supportsNativeStructuredOutput) != null ? _b : true) && modelSupportsStructuredOutput;
4122
+ const supportsResponseSchema = (responseFormat == null ? void 0 : responseFormat.type) !== "json" || responseFormat.schema == null || supportsNativeStructuredOutputSchema(responseFormat.schema);
4109
4123
  const supportsStrictTools = ((_c = this.config.supportsStrictTools) != null ? _c : true) && modelSupportsStructuredOutput;
4110
4124
  const structureOutputMode = (_d = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _d : "auto";
4111
- let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
4125
+ let useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput && supportsResponseSchema;
4112
4126
  if (!useStructuredOutput && rejectsForcedToolUse && modelSupportsStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && supportsNativeStructuredOutputSchema(responseFormat.schema)) {
4113
4127
  warnings.push({
4114
4128
  type: "unsupported",
@@ -4481,7 +4495,12 @@ var AnthropicMessagesLanguageModel = class {
4481
4495
  betas: toolsBetas
4482
4496
  } = await prepareTools(
4483
4497
  jsonResponseTool != null ? {
4484
- tools: [...tools != null ? tools : [], jsonResponseTool],
4498
+ tools: [
4499
+ ...(tools != null ? tools : []).filter(
4500
+ (tool) => (toolChoice == null ? void 0 : toolChoice.type) !== "none" && ((toolChoice == null ? void 0 : toolChoice.type) !== "tool" || tool.name === toolChoice.toolName)
4501
+ ),
4502
+ jsonResponseTool
4503
+ ],
4485
4504
  toolChoice: { type: "required" },
4486
4505
  disableParallelToolUse: true,
4487
4506
  cacheControlValidator,
@@ -6900,6 +6919,7 @@ var anthropicTools = {
6900
6919
  anthropicTools,
6901
6920
  getModelCapabilities,
6902
6921
  prepareTools,
6903
- sanitizeJsonSchema
6922
+ sanitizeJsonSchema,
6923
+ supportsNativeStructuredOutputSchema
6904
6924
  });
6905
6925
  //# sourceMappingURL=index.js.map