@ai-sdk/anthropic 2.0.47 → 2.0.48

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.
package/dist/index.mjs CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  } from "@ai-sdk/provider-utils";
12
12
 
13
13
  // src/version.ts
14
- var VERSION = true ? "2.0.47" : "0.0.0-test";
14
+ var VERSION = true ? "2.0.48" : "0.0.0-test";
15
15
 
16
16
  // src/anthropic-messages-language-model.ts
17
17
  import {
@@ -546,6 +546,20 @@ var anthropicFilePartProviderOptions = z3.object({
546
546
  });
547
547
  var anthropicProviderOptions = z3.object({
548
548
  sendReasoning: z3.boolean().optional(),
549
+ /**
550
+ * Determines how structured outputs are generated.
551
+ *
552
+ * - `outputFormat`: Use the `output_format` parameter to specify the structured output format.
553
+ * - `jsonTool`: Use a special 'json' tool to specify the structured output format (default).
554
+ * - `auto`: Use 'outputFormat' when supported, otherwise use 'jsonTool'.
555
+ */
556
+ structuredOutputMode: z3.enum(["outputFormat", "jsonTool", "auto"]).optional(),
557
+ /**
558
+ * Configuration for enabling Claude's extended thinking.
559
+ *
560
+ * When enabled, responses include thinking content blocks showing Claude's thinking process before the final answer.
561
+ * Requires a minimum budget of 1,024 tokens and counts towards the `max_tokens` limit.
562
+ */
549
563
  thinking: z3.object({
550
564
  type: z3.union([z3.literal("enabled"), z3.literal("disabled")]),
551
565
  budgetTokens: z3.number().optional()
@@ -1787,7 +1801,7 @@ var AnthropicMessagesLanguageModel = class {
1787
1801
  toolChoice,
1788
1802
  providerOptions
1789
1803
  }) {
1790
- var _a, _b, _c, _d;
1804
+ var _a, _b, _c, _d, _e;
1791
1805
  const warnings = [];
1792
1806
  if (frequencyPenalty != null) {
1793
1807
  warnings.push({
@@ -1837,27 +1851,33 @@ var AnthropicMessagesLanguageModel = class {
1837
1851
  });
1838
1852
  }
1839
1853
  }
1840
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
1841
- type: "function",
1842
- name: "json",
1843
- description: "Respond with a JSON object.",
1844
- inputSchema: responseFormat.schema
1845
- } : void 0;
1846
1854
  const anthropicOptions = await parseProviderOptions2({
1847
1855
  provider: "anthropic",
1848
1856
  providerOptions,
1849
1857
  schema: anthropicProviderOptions
1850
1858
  });
1859
+ const {
1860
+ maxOutputTokens: maxOutputTokensForModel,
1861
+ supportsStructuredOutput,
1862
+ isKnownModel
1863
+ } = getModelCapabilities(this.modelId);
1864
+ const structureOutputMode = (_a = anthropicOptions == null ? void 0 : anthropicOptions.structuredOutputMode) != null ? _a : "jsonTool";
1865
+ const useStructuredOutput = structureOutputMode === "outputFormat" || structureOutputMode === "auto" && supportsStructuredOutput;
1866
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useStructuredOutput ? {
1867
+ type: "function",
1868
+ name: "json",
1869
+ description: "Respond with a JSON object.",
1870
+ inputSchema: responseFormat.schema
1871
+ } : void 0;
1851
1872
  const cacheControlValidator = new CacheControlValidator();
1852
1873
  const { prompt: messagesPrompt, betas } = await convertToAnthropicMessagesPrompt({
1853
1874
  prompt,
1854
- sendReasoning: (_a = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _a : true,
1875
+ sendReasoning: (_b = anthropicOptions == null ? void 0 : anthropicOptions.sendReasoning) != null ? _b : true,
1855
1876
  warnings,
1856
1877
  cacheControlValidator
1857
1878
  });
1858
- const isThinking = ((_b = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _b.type) === "enabled";
1859
- const thinkingBudget = (_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.budgetTokens;
1860
- const { maxOutputTokens: maxOutputTokensForModel, knownModel } = getMaxOutputTokensForModel(this.modelId);
1879
+ const isThinking = ((_c = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _c.type) === "enabled";
1880
+ const thinkingBudget = (_d = anthropicOptions == null ? void 0 : anthropicOptions.thinking) == null ? void 0 : _d.budgetTokens;
1861
1881
  const maxTokens = maxOutputTokens != null ? maxOutputTokens : maxOutputTokensForModel;
1862
1882
  const baseArgs = {
1863
1883
  // model id:
@@ -1875,11 +1895,18 @@ var AnthropicMessagesLanguageModel = class {
1875
1895
  ...(anthropicOptions == null ? void 0 : anthropicOptions.effort) && {
1876
1896
  output_config: { effort: anthropicOptions.effort }
1877
1897
  },
1898
+ // structured output:
1899
+ ...useStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && {
1900
+ output_format: {
1901
+ type: "json_schema",
1902
+ schema: responseFormat.schema
1903
+ }
1904
+ },
1878
1905
  // container with agent skills:
1879
1906
  ...(anthropicOptions == null ? void 0 : anthropicOptions.container) && {
1880
1907
  container: {
1881
1908
  id: anthropicOptions.container.id,
1882
- skills: (_d = anthropicOptions.container.skills) == null ? void 0 : _d.map((skill) => ({
1909
+ skills: (_e = anthropicOptions.container.skills) == null ? void 0 : _e.map((skill) => ({
1883
1910
  type: skill.type,
1884
1911
  skill_id: skill.skillId,
1885
1912
  version: skill.version
@@ -1922,7 +1949,7 @@ var AnthropicMessagesLanguageModel = class {
1922
1949
  }
1923
1950
  baseArgs.max_tokens = maxTokens + thinkingBudget;
1924
1951
  }
1925
- if (knownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
1952
+ if (isKnownModel && baseArgs.max_tokens > maxOutputTokensForModel) {
1926
1953
  if (maxOutputTokens != null) {
1927
1954
  warnings.push({
1928
1955
  type: "unsupported-setting",
@@ -1948,6 +1975,9 @@ var AnthropicMessagesLanguageModel = class {
1948
1975
  if (anthropicOptions == null ? void 0 : anthropicOptions.effort) {
1949
1976
  betas.add("effort-2025-11-24");
1950
1977
  }
1978
+ if (useStructuredOutput) {
1979
+ betas.add("structured-outputs-2025-11-13");
1980
+ }
1951
1981
  const {
1952
1982
  tools: anthropicTools2,
1953
1983
  toolChoice: anthropicToolChoice,
@@ -2750,27 +2780,49 @@ var AnthropicMessagesLanguageModel = class {
2750
2780
  };
2751
2781
  }
2752
2782
  };
2753
- function getMaxOutputTokensForModel(modelId) {
2783
+ function getModelCapabilities(modelId) {
2754
2784
  if (modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5")) {
2755
2785
  return {
2756
2786
  maxOutputTokens: 64e3,
2757
- knownModel: true
2787
+ supportsStructuredOutput: true,
2788
+ isKnownModel: true
2758
2789
  };
2759
2790
  } else if (modelId.includes("claude-opus-4-1")) {
2760
2791
  return {
2761
2792
  maxOutputTokens: 32e3,
2762
- knownModel: true
2793
+ supportsStructuredOutput: true,
2794
+ isKnownModel: true
2763
2795
  };
2764
2796
  } else if (modelId.includes("claude-sonnet-4-") || modelId.includes("claude-3-7-sonnet") || modelId.includes("claude-haiku-4-5")) {
2765
- return { maxOutputTokens: 64e3, knownModel: true };
2797
+ return {
2798
+ maxOutputTokens: 64e3,
2799
+ supportsStructuredOutput: false,
2800
+ isKnownModel: true
2801
+ };
2766
2802
  } else if (modelId.includes("claude-opus-4-")) {
2767
- return { maxOutputTokens: 32e3, knownModel: true };
2803
+ return {
2804
+ maxOutputTokens: 32e3,
2805
+ supportsStructuredOutput: false,
2806
+ isKnownModel: true
2807
+ };
2768
2808
  } else if (modelId.includes("claude-3-5-haiku")) {
2769
- return { maxOutputTokens: 8192, knownModel: true };
2809
+ return {
2810
+ maxOutputTokens: 8192,
2811
+ supportsStructuredOutput: false,
2812
+ isKnownModel: true
2813
+ };
2770
2814
  } else if (modelId.includes("claude-3-haiku")) {
2771
- return { maxOutputTokens: 4096, knownModel: true };
2815
+ return {
2816
+ maxOutputTokens: 4096,
2817
+ supportsStructuredOutput: false,
2818
+ isKnownModel: true
2819
+ };
2772
2820
  } else {
2773
- return { maxOutputTokens: 4096, knownModel: false };
2821
+ return {
2822
+ maxOutputTokens: 4096,
2823
+ supportsStructuredOutput: false,
2824
+ isKnownModel: false
2825
+ };
2774
2826
  }
2775
2827
  }
2776
2828