@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.
@@ -572,6 +572,14 @@ var anthropicProviderOptions = z3.object({
572
572
  * This allows you to deactivate reasoning inputs for models that do not support them.
573
573
  */
574
574
  sendReasoning: z3.boolean().optional(),
575
+ /**
576
+ * Determines how structured outputs are generated.
577
+ *
578
+ * - `outputFormat`: Use the `output_format` parameter to specify the structured output format.
579
+ * - `jsonTool`: Use a special 'json' tool to specify the structured output format.
580
+ * - `auto`: Use 'outputFormat' when supported, otherwise use 'jsonTool' (default).
581
+ */
582
+ structuredOutputMode: z3.enum(["outputFormat", "jsonTool", "auto"]).optional(),
575
583
  /**
576
584
  * Configuration for enabling Claude's extended thinking.
577
585
  *
@@ -1910,7 +1918,7 @@ var AnthropicMessagesLanguageModel = class {
1910
1918
  providerOptions,
1911
1919
  stream
1912
1920
  }) {
1913
- var _a, _b, _c, _d, _e;
1921
+ var _a, _b, _c, _d, _e, _f;
1914
1922
  const warnings = [];
1915
1923
  if (frequencyPenalty != null) {
1916
1924
  warnings.push({
@@ -1954,27 +1962,33 @@ var AnthropicMessagesLanguageModel = class {
1954
1962
  });
1955
1963
  }
1956
1964
  }
1957
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
1958
- type: "function",
1959
- name: "json",
1960
- description: "Respond with a JSON object.",
1961
- inputSchema: responseFormat.schema
1962
- } : void 0;
1963
1965
  const anthropicOptions = await parseProviderOptions2({
1964
1966
  provider: "anthropic",
1965
1967
  providerOptions,
1966
1968
  schema: anthropicProviderOptions
1967
1969
  });
1970
+ const {
1971
+ maxOutputTokens: maxOutputTokensForModel,
1972
+ supportsStructuredOutput,
1973
+ isKnownModel
1974
+ } = getModelCapabilities(this.modelId);
1975
+ const structureOutputMode = (_a = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _a : "auto";
1976
+ const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
1977
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
1978
+ type: "function",
1979
+ name: "json",
1980
+ description: "Respond with a JSON object.",
1981
+ inputSchema: responseFormat.schema
1982
+ } : void 0;
1968
1983
  const cacheControlValidator = new CacheControlValidator();
1969
1984
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1970
1985
  prompt,
1971
- sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1986
+ sendReasoning: (_b = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _b : true,
1972
1987
  warnings,
1973
1988
  cacheControlValidator
1974
1989
  });
1975
- const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1976
- const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1977
- const { maxOutputTokens: maxOutputTokensForModel, knownModel } = getMaxOutputTokensForModel(this.modelId);
1990
+ const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
1991
+ const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
1978
1992
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1979
1993
  const baseArgs = {
1980
1994
  // model id:
@@ -1989,6 +2003,13 @@ var AnthropicMessagesLanguageModel = class {
1989
2003
  ...isThinking && {
1990
2004
  thinking: { type: "enabled", budget_tokens: thinkingBudget }
1991
2005
  },
2006
+ // structured output:
2007
+ ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
2008
+ output_format: {
2009
+ type: "json_schema",
2010
+ schema: responseFormat.schema
2011
+ }
2012
+ },
1992
2013
  // mcp servers:
1993
2014
  ...(anthropicOptions == null ? void 0 : anthropicOptions.mcpServers) && anthropicOptions.mcpServers.length > 0 && {
1994
2015
  mcp_servers: anthropicOptions.mcpServers.map((server) => ({
@@ -2006,7 +2027,7 @@ var AnthropicMessagesLanguageModel = class {
2006
2027
  ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
2007
2028
  container: {
2008
2029
  id: anthropicOptions.container.id,
2009
- skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
2030
+ skills: (_e = anthropicOptions.container.skills) == null ? void 0 : _e.map((skill) => ({
2010
2031
  type: skill.type,
2011
2032
  skill_id: skill.skillId,
2012
2033
  version: skill.version
@@ -2049,7 +2070,7 @@ var AnthropicMessagesLanguageModel = class {
2049
2070
  }
2050
2071
  baseArgs.max_tokens = maxTokens + thinkingBudget;
2051
2072
  }
2052
- if (knownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2073
+ if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
2053
2074
  if (maxOutputTokens != null) {
2054
2075
  warnings.push({
2055
2076
  type: "unsupported-setting",
@@ -2075,9 +2096,12 @@ var AnthropicMessagesLanguageModel = class {
2075
2096
  });
2076
2097
  }
2077
2098
  }
2078
- if (stream && ((_e = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _e : true)) {
2099
+ if (stream && ((_f = anthropicOptions == null ? void 0 : anthropicOptions.toolStreaming) != null ? _f : true)) {
2079
2100
  betas.add("fine-grained-tool-streaming-2025-05-14");
2080
2101
  }
2102
+ if (useStructuredOutput) {
2103
+ betas.add("structured-outputs-2025-11-13");
2104
+ }
2081
2105
  const {
2082
2106
  tools: anthropicTools2,
2083
2107
  toolChoice: anthropicToolChoice,
@@ -2954,17 +2978,49 @@ var AnthropicMessagesLanguageModel = class {
2954
2978
  };
2955
2979
  }
2956
2980
  };
2957
- function getMaxOutputTokensForModel(modelId) {
2958
- if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2959
- return { maxOutputTokens: 64e3, knownModel: true };
2981
+ function getModelCapabilities(modelId) {
2982
+ if (modelId.includes("claude-sonnet-4-5")) {
2983
+ return {
2984
+ maxOutputTokens: 64e3,
2985
+ supportsStructuredOutput: true,
2986
+ isKnownModel: true
2987
+ };
2988
+ } else if (modelId.includes("claude-opus-4-1")) {
2989
+ return {
2990
+ maxOutputTokens: 32e3,
2991
+ supportsStructuredOutput: true,
2992
+ isKnownModel: true
2993
+ };
2994
+ } else if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2995
+ return {
2996
+ maxOutputTokens: 64e3,
2997
+ supportsStructuredOutput: false,
2998
+ isKnownModel: true
2999
+ };
2960
3000
  } else if (modelId.includes("claude-opus-4-")) {
2961
- return { maxOutputTokens: 32e3, knownModel: true };
3001
+ return {
3002
+ maxOutputTokens: 32e3,
3003
+ supportsStructuredOutput: false,
3004
+ isKnownModel: true
3005
+ };
2962
3006
  } else if (modelId.includes("claude-3-5-haiku")) {
2963
- return { maxOutputTokens: 8192, knownModel: true };
3007
+ return {
3008
+ maxOutputTokens: 8192,
3009
+ supportsStructuredOutput: false,
3010
+ isKnownModel: true
3011
+ };
2964
3012
  } else if (modelId.includes("claude-3-haiku")) {
2965
- return { maxOutputTokens: 4096, knownModel: true };
3013
+ return {
3014
+ maxOutputTokens: 4096,
3015
+ supportsStructuredOutput: false,
3016
+ isKnownModel: true
3017
+ };
2966
3018
  } else {
2967
- return { maxOutputTokens: 4096, knownModel: false };
3019
+ return {
3020
+ maxOutputTokens: 4096,
3021
+ supportsStructuredOutput: false,
3022
+ isKnownModel: false
3023
+ };
2968
3024
  }
2969
3025
  }
2970
3026