@ai-sdk/anthropic 2.0.47 → 2.0.49

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.
@@ -531,6 +531,20 @@ var anthropicFilePartProviderOptions = z3.object({
531
531
  });
532
532
  var anthropicProviderOptions = z3.object({
533
533
  sendReasoning: z3.boolean().optional(),
534
+ /**
535
+ * Determines how structured outputs are generated.
536
+ *
537
+ * - `outputFormat`: Use the `output_format` parameter to specify the structured output format.
538
+ * - `jsonTool`: Use a special 'json' tool to specify the structured output format (default).
539
+ * - `auto`: Use 'outputFormat' when supported, otherwise use 'jsonTool'.
540
+ */
541
+ structuredOutputMode: z3.enum(["outputFormat", "jsonTool", "auto"]).optional(),
542
+ /**
543
+ * Configuration for enabling Claude's extended thinking.
544
+ *
545
+ * When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer.
546
+ * Requires a minimum budget of 1,024 tokens and counts towards the `max_tokens` limit.
547
+ */
534
548
  thinking: z3.object({
535
549
  type: z3.union([z3.literal("enabled"), z3.literal("disabled")]),
536
550
  budgetTokens: z3.number().optional()
@@ -1758,6 +1772,7 @@ var AnthropicMessagesLanguageModel = class {
1758
1772
  return (_c = (_b = (_a = this.config).supportedUrls) == null ? void 0 : _b.call(_a)) != null ? _c : {};
1759
1773
  }
1760
1774
  async getArgs({
1775
+ userSuppliedBetas,
1761
1776
  prompt,
1762
1777
  maxOutputTokens,
1763
1778
  temperature,
@@ -1772,7 +1787,7 @@ var AnthropicMessagesLanguageModel = class {
1772
1787
  toolChoice,
1773
1788
  providerOptions
1774
1789
  }) {
1775
- var _a, _b, _c, _d;
1790
+ var _a, _b, _c, _d, _e;
1776
1791
  const warnings = [];
1777
1792
  if (frequencyPenalty != null) {
1778
1793
  warnings.push({
@@ -1822,27 +1837,33 @@ var AnthropicMessagesLanguageModel = class {
1822
1837
  });
1823
1838
  }
1824
1839
  }
1825
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
1826
- type: "function",
1827
- name: "json",
1828
- description: "Respond with a JSON object.",
1829
- inputSchema: responseFormat.schema
1830
- } : void 0;
1831
1840
  const anthropicOptions = await parseProviderOptions2({
1832
1841
  provider: "anthropic",
1833
1842
  providerOptions,
1834
1843
  schema: anthropicProviderOptions
1835
1844
  });
1845
+ const {
1846
+ maxOutputTokens: maxOutputTokensForModel,
1847
+ supportsStructuredOutput,
1848
+ isKnownModel
1849
+ } = getModelCapabilities(this.modelId);
1850
+ const structureOutputMode = (_a = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _a : "jsonTool";
1851
+ const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
1852
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
1853
+ type: "function",
1854
+ name: "json",
1855
+ description: "Respond with a JSON object.",
1856
+ inputSchema: responseFormat.schema
1857
+ } : void 0;
1836
1858
  const cacheControlValidator = new CacheControlValidator();
1837
1859
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1838
1860
  prompt,
1839
- sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1861
+ sendReasoning: (_b = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _b : true,
1840
1862
  warnings,
1841
1863
  cacheControlValidator
1842
1864
  });
1843
- const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1844
- const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1845
- const { maxOutputTokens: maxOutputTokensForModel, knownModel } = getMaxOutputTokensForModel(this.modelId);
1865
+ const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
1866
+ const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
1846
1867
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1847
1868
  const baseArgs = {
1848
1869
  // model id:
@@ -1860,11 +1881,18 @@ var AnthropicMessagesLanguageModel = class {
1860
1881
  ...(anthropicOptions == null ? void 0 : anthropicOptions.effort) && {
1861
1882
  output_config: { effort: anthropicOptions.effort }
1862
1883
  },
1884
+ // structured output:
1885
+ ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
1886
+ output_format: {
1887
+ type: "json_schema",
1888
+ schema: responseFormat.schema
1889
+ }
1890
+ },
1863
1891
  // container with agent skills:
1864
1892
  ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
1865
1893
  container: {
1866
1894
  id: anthropicOptions.container.id,
1867
- skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
1895
+ skills: (_e = anthropicOptions.container.skills) == null ? void 0 : _e.map((skill) => ({
1868
1896
  type: skill.type,
1869
1897
  skill_id: skill.skillId,
1870
1898
  version: skill.version
@@ -1907,7 +1935,7 @@ var AnthropicMessagesLanguageModel = class {
1907
1935
  }
1908
1936
  baseArgs.max_tokens = maxTokens + thinkingBudget;
1909
1937
  }
1910
- if (knownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
1938
+ if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
1911
1939
  if (maxOutputTokens != null) {
1912
1940
  warnings.push({
1913
1941
  type: "unsupported-setting",
@@ -1933,6 +1961,9 @@ var AnthropicMessagesLanguageModel = class {
1933
1961
  if (anthropicOptions == null ? void 0 : anthropicOptions.effort) {
1934
1962
  betas.add("effort-2025-11-24");
1935
1963
  }
1964
+ if (useStructuredOutput) {
1965
+ betas.add("structured-outputs-2025-11-13");
1966
+ }
1936
1967
  const {
1937
1968
  tools: anthropicTools2,
1938
1969
  toolChoice: anthropicToolChoice,
@@ -1959,7 +1990,7 @@ var AnthropicMessagesLanguageModel = class {
1959
1990
  tool_choice: anthropicToolChoice
1960
1991
  },
1961
1992
  warnings: [...warnings, ...toolWarnings, ...cacheWarnings],
1962
- betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas]),
1993
+ betas: /* @__PURE__ */ new Set([...betas, ...toolsBetas, ...userSuppliedBetas]),
1963
1994
  usesJsonResponseTool: jsonResponseTool != null
1964
1995
  };
1965
1996
  }
@@ -1969,8 +2000,20 @@ var AnthropicMessagesLanguageModel = class {
1969
2000
  }) {
1970
2001
  return combineHeaders(
1971
2002
  await resolve(this.config.headers),
1972
- betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {},
1973
- headers
2003
+ headers,
2004
+ betas.size > 0 ? { "anthropic-beta": Array.from(betas).join(",") } : {}
2005
+ );
2006
+ }
2007
+ async getBetasFromHeaders(requestHeaders) {
2008
+ var _a, _b;
2009
+ const configHeaders = await resolve(this.config.headers);
2010
+ const configBetaHeader = (_a = configHeaders["anthropic-beta"]) != null ? _a : "";
2011
+ const requestBetaHeader = (_b = requestHeaders == null ? void 0 : requestHeaders["anthropic-beta"]) != null ? _b : "";
2012
+ return new Set(
2013
+ [
2014
+ ...configBetaHeader.toLowerCase().split(","),
2015
+ ...requestBetaHeader.toLowerCase().split(",")
2016
+ ].map((beta) => beta.trim()).filter((beta) => beta !== "")
1974
2017
  );
1975
2018
  }
1976
2019
  buildRequestUrl(isStreaming) {
@@ -2006,7 +2049,10 @@ var AnthropicMessagesLanguageModel = class {
2006
2049
  }
2007
2050
  async doGenerate(options) {
2008
2051
  var _a, _b, _c, _d, _e, _f, _g, _h;
2009
- const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2052
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2053
+ ...options,
2054
+ userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2055
+ });
2010
2056
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2011
2057
  const {
2012
2058
  responseHeaders,
@@ -2271,7 +2317,10 @@ var AnthropicMessagesLanguageModel = class {
2271
2317
  };
2272
2318
  }
2273
2319
  async doStream(options) {
2274
- const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs(options);
2320
+ const { args, warnings, betas, usesJsonResponseTool } = await this.getArgs({
2321
+ ...options,
2322
+ userSuppliedBetas: await this.getBetasFromHeaders(options.headers)
2323
+ });
2275
2324
  const citationDocuments = this.extractCitationDocuments(options.prompt);
2276
2325
  const body = { ...args, stream: true };
2277
2326
  const { responseHeaders, value: response } = await postJsonToApi({
@@ -2735,27 +2784,49 @@ var AnthropicMessagesLanguageModel = class {
2735
2784
  };
2736
2785
  }
2737
2786
  };
2738
- function getMaxOutputTokensForModel(modelId) {
2787
+ function getModelCapabilities(modelId) {
2739
2788
  if (modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5")) {
2740
2789
  return {
2741
2790
  maxOutputTokens: 64e3,
2742
- knownModel: true
2791
+ supportsStructuredOutput: true,
2792
+ isKnownModel: true
2743
2793
  };
2744
2794
  } else if (modelId.includes("claude-opus-4-1")) {
2745
2795
  return {
2746
2796
  maxOutputTokens: 32e3,
2747
- knownModel: true
2797
+ supportsStructuredOutput: true,
2798
+ isKnownModel: true
2748
2799
  };
2749
2800
  } else if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2750
- return { maxOutputTokens: 64e3, knownModel: true };
2801
+ return {
2802
+ maxOutputTokens: 64e3,
2803
+ supportsStructuredOutput: false,
2804
+ isKnownModel: true
2805
+ };
2751
2806
  } else if (modelId.includes("claude-opus-4-")) {
2752
- return { maxOutputTokens: 32e3, knownModel: true };
2807
+ return {
2808
+ maxOutputTokens: 32e3,
2809
+ supportsStructuredOutput: false,
2810
+ isKnownModel: true
2811
+ };
2753
2812
  } else if (modelId.includes("claude-3-5-haiku")) {
2754
- return { maxOutputTokens: 8192, knownModel: true };
2813
+ return {
2814
+ maxOutputTokens: 8192,
2815
+ supportsStructuredOutput: false,
2816
+ isKnownModel: true
2817
+ };
2755
2818
  } else if (modelId.includes("claude-3-haiku")) {
2756
- return { maxOutputTokens: 4096, knownModel: true };
2819
+ return {
2820
+ maxOutputTokens: 4096,
2821
+ supportsStructuredOutput: false,
2822
+ isKnownModel: true
2823
+ };
2757
2824
  } else {
2758
- return { maxOutputTokens: 4096, knownModel: false };
2825
+ return {
2826
+ maxOutputTokens: 4096,
2827
+ supportsStructuredOutput: false,
2828
+ isKnownModel: false
2829
+ };
2759
2830
  }
2760
2831
  }
2761
2832