@ai-sdk/anthropic 3.0.0-beta.56 → 3.0.0-beta.57

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.
@@ -586,6 +586,14 @@ var anthropicProviderOptions = import_v43.z.object({
586
586
  * This allows you to deactivate reasoning inputs for models that do not support them.
587
587
  */
588
588
  sendReasoning: import_v43.z.boolean().optional(),
589
+ /**
590
+ * Determines how structured outputs are generated.
591
+ *
592
+ * - `outputFormat`: Use the `output_format` parameter to specify the structured output format.
593
+ * - `jsonTool`: Use a special 'json' tool to specify the structured output format.
594
+ * - `auto`: Use 'outputFormat' when supported, otherwise use 'jsonTool' (default).
595
+ */
596
+ structuredOutputMode: import_v43.z.enum(["outputFormat", "jsonTool", "auto"]).optional(),
589
597
  /**
590
598
  * Configuration for enabling Claude's extended thinking.
591
599
  *
@@ -1899,7 +1907,7 @@ var AnthropicMessagesLanguageModel = class {
1899
1907
  providerOptions,
1900
1908
  stream
1901
1909
  }) {
1902
- var _a, _b, _c, _d, _e;
1910
+ var _a, _b, _c, _d, _e, _f;
1903
1911
  const warnings = [];
1904
1912
  if (frequencyPenalty != null) {
1905
1913
  warnings.push({
@@ -1943,27 +1951,33 @@ var AnthropicMessagesLanguageModel = class {
1943
1951
  });
1944
1952
  }
1945
1953
  }
1946
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
1947
- type: "function",
1948
- name: "json",
1949
- description: "Respond with a JSON object.",
1950
- inputSchema: responseFormat.schema
1951
- } : void 0;
1952
1954
  const anthropicOptions = await (0, import_provider_utils11.parseProviderOptions)({
1953
1955
  provider: "anthropic",
1954
1956
  providerOptions,
1955
1957
  schema: anthropicProviderOptions
1956
1958
  });
1959
+ const {
1960
+ maxOutputTokens: maxOutputTokensForModel,
1961
+ supportsStructuredOutput,
1962
+ isKnownModel
1963
+ } = getModelCapabilities(this.modelId);
1964
+ const structureOutputMode = (_a = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _a : "auto";
1965
+ const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
1966
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
1967
+ type: "function",
1968
+ name: "json",
1969
+ description: "Respond with a JSON object.",
1970
+ inputSchema: responseFormat.schema
1971
+ } : void 0;
1957
1972
  const cacheControlValidator = new CacheControlValidator();
1958
1973
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1959
1974
  prompt,
1960
- sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1975
+ sendReasoning: (_b = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _b : true,
1961
1976
  warnings,
1962
1977
  cacheControlValidator
1963
1978
  });
1964
- const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1965
- const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1966
- const { maxOutputTokens: maxOutputTokensForModel, knownModel } = getMaxOutputTokensForModel(this.modelId);
1979
+ const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
1980
+ const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
1967
1981
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1968
1982
  const baseArgs = {
1969
1983
  // model id:
@@ -1978,6 +1992,13 @@ var AnthropicMessagesLanguageModel = class {
1978
1992
  ...isThinking && {
1979
1993
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1980
1994
  },
1995
+ // structured output:
1996
+ ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
1997
+ output_format: {
1998
+ type: "json_schema",
1999
+ schema: responseFormat.schema
2000
+ }
2001
+ },
1981
2002
  // mcp servers:
1982
2003
  ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1983
2004
  mcp_servers: anthropicOptions.mcpServers.map((server) => ({
@@ -1995,7 +2016,7 @@ var AnthropicMessagesLanguageModel = class {
1995
2016
  ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
1996
2017
  container: {
1997
2018
  id: anthropicOptions.container.id,
1998
- skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
2019
+ skills: (_e = anthropicOptions.container.skills) == null ? void 0 : _e.map((skill) => ({
1999
2020
  type: skill.type,
2000
2021
  skill_id: skill.skillId,
2001
2022
  version: skill.version
@@ -2038,7 +2059,7 @@ var AnthropicMessagesLanguageModel = class {
2038
2059
  }
2039
2060
  baseArgs.max_tokens = maxTokens + thinkingBudget;
2040
2061
  }
2041
- if (knownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2062
+ if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2042
2063
  if (maxOutputTokens != null) {
2043
2064
  warnings.push({
2044
2065
  type: "unsupported-setting",
@@ -2064,9 +2085,12 @@ var AnthropicMessagesLanguageModel = class {
2064
2085
  });
2065
2086
  }
2066
2087
  }
2067
- if (stream && ((_e = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _e : true)) {
2088
+ if (stream && ((_f = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _f : true)) {
2068
2089
  betas.add("fine-grained-tool-streaming-2025-05-14");
2069
2090
  }
2091
+ if (useStructuredOutput) {
2092
+ betas.add("structured-outputs-2025-11-13");
2093
+ }
2070
2094
  const {
2071
2095
  tools: anthropicTools2,
2072
2096
  toolChoice: anthropicToolChoice,
@@ -2943,17 +2967,49 @@ var AnthropicMessagesLanguageModel = class {
2943
2967
  };
2944
2968
  }
2945
2969
  };
2946
- function getMaxOutputTokensForModel(modelId) {
2947
- if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2948
- return { maxOutputTokens: 64e3, knownModel: true };
2970
+ function getModelCapabilities(modelId) {
2971
+ if (modelId.includes("claude-sonnet-4-5")) {
2972
+ return {
2973
+ maxOutputTokens: 64e3,
2974
+ supportsStructuredOutput: true,
2975
+ isKnownModel: true
2976
+ };
2977
+ } else if (modelId.includes("claude-opus-4-1")) {
2978
+ return {
2979
+ maxOutputTokens: 32e3,
2980
+ supportsStructuredOutput: true,
2981
+ isKnownModel: true
2982
+ };
2983
+ } else if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2984
+ return {
2985
+ maxOutputTokens: 64e3,
2986
+ supportsStructuredOutput: false,
2987
+ isKnownModel: true
2988
+ };
2949
2989
  } else if (modelId.includes("claude-opus-4-")) {
2950
- return { maxOutputTokens: 32e3, knownModel: true };
2990
+ return {
2991
+ maxOutputTokens: 32e3,
2992
+ supportsStructuredOutput: false,
2993
+ isKnownModel: true
2994
+ };
2951
2995
  } else if (modelId.includes("claude-3-5-haiku")) {
2952
- return { maxOutputTokens: 8192, knownModel: true };
2996
+ return {
2997
+ maxOutputTokens: 8192,
2998
+ supportsStructuredOutput: false,
2999
+ isKnownModel: true
3000
+ };
2953
3001
  } else if (modelId.includes("claude-3-haiku")) {
2954
- return { maxOutputTokens: 4096, knownModel: true };
3002
+ return {
3003
+ maxOutputTokens: 4096,
3004
+ supportsStructuredOutput: false,
3005
+ isKnownModel: true
3006
+ };
2955
3007
  } else {
2956
- return { maxOutputTokens: 4096, knownModel: false };
3008
+ return {
3009
+ maxOutputTokens: 4096,
3010
+ supportsStructuredOutput: false,
3011
+ isKnownModel: false
3012
+ };
2957
3013
  }
2958
3014
  }
2959
3015