@ai-sdk/anthropic 4.0.38 → 4.0.39

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/anthropic",
3
- "version": "4.0.38",
3
+ "version": "4.0.39",
4
4
  "type": "module",
5
5
  "license": "Apache-2.0",
6
6
  "sideEffects": false,
@@ -189,6 +189,7 @@ export interface AnthropicServerToolUseContent {
189
189
  // advisor:
190
190
  | 'advisor';
191
191
  input: unknown;
192
+ caller?: AnthropicToolCallCaller;
192
193
  cache_control: AnthropicCacheControl | undefined;
193
194
  }
194
195
 
@@ -250,6 +251,7 @@ export interface AnthropicWebSearchToolResultContent {
250
251
  type: 'web_search_tool_result_error';
251
252
  error_code: string;
252
253
  };
254
+ caller?: AnthropicToolCallCaller;
253
255
  cache_control: AnthropicCacheControl | undefined;
254
256
  }
255
257
 
@@ -398,6 +400,7 @@ export interface AnthropicWebFetchToolResultContent {
398
400
  type: 'web_fetch_tool_result_error';
399
401
  error_code: string;
400
402
  };
403
+ caller?: AnthropicToolCallCaller;
401
404
  cache_control: AnthropicCacheControl | undefined;
402
405
  }
403
406
  export interface AnthropicMcpToolUseContent {
@@ -638,6 +641,20 @@ const anthropicStopDetailsSchema = z.object({
638
641
 
639
642
  export type AnthropicStopDetails = z.infer<typeof anthropicStopDetailsSchema>;
640
643
 
644
+ const anthropicToolCallCallerSchema = z.union([
645
+ z.object({
646
+ type: z.literal('code_execution_20250825'),
647
+ tool_id: z.string(),
648
+ }),
649
+ z.object({
650
+ type: z.literal('code_execution_20260120'),
651
+ tool_id: z.string(),
652
+ }),
653
+ z.object({
654
+ type: z.literal('direct'),
655
+ }),
656
+ ]);
657
+
641
658
  // limited version of the schema, focussed on what is needed for the implementation
642
659
  // this approach limits breakages when the API changes and increases efficiency
643
660
  export const anthropicResponseSchema = lazySchema(() =>
@@ -700,38 +717,14 @@ export const anthropicResponseSchema = lazySchema(() =>
700
717
  name: z.string(),
701
718
  input: z.unknown(),
702
719
  // Programmatic tool calling: caller info when triggered from code execution
703
- caller: z
704
- .union([
705
- z.object({
706
- type: z.literal('code_execution_20250825'),
707
- tool_id: z.string(),
708
- }),
709
- z.object({
710
- type: z.literal('code_execution_20260120'),
711
- tool_id: z.string(),
712
- }),
713
- z.object({
714
- type: z.literal('direct'),
715
- }),
716
- ])
717
- .optional(),
720
+ caller: anthropicToolCallCallerSchema.optional(),
718
721
  }),
719
722
  z.object({
720
723
  type: z.literal('server_tool_use'),
721
724
  id: z.string(),
722
725
  name: z.string(),
723
726
  input: z.record(z.string(), z.unknown()).nullish(),
724
- caller: z
725
- .union([
726
- z.object({
727
- type: z.literal('code_execution_20260120'),
728
- tool_id: z.string(),
729
- }),
730
- z.object({
731
- type: z.literal('direct'),
732
- }),
733
- ])
734
- .optional(),
727
+ caller: anthropicToolCallCallerSchema.optional(),
735
728
  }),
736
729
  z.object({
737
730
  type: z.literal('mcp_tool_use'),
@@ -754,6 +747,7 @@ export const anthropicResponseSchema = lazySchema(() =>
754
747
  z.object({
755
748
  type: z.literal('web_fetch_tool_result'),
756
749
  tool_use_id: z.string(),
750
+ caller: anthropicToolCallCallerSchema.optional(),
757
751
  content: z.union([
758
752
  z.object({
759
753
  type: z.literal('web_fetch_result'),
@@ -786,6 +780,7 @@ export const anthropicResponseSchema = lazySchema(() =>
786
780
  z.object({
787
781
  type: z.literal('web_search_tool_result'),
788
782
  tool_use_id: z.string(),
783
+ caller: anthropicToolCallCallerSchema.optional(),
789
784
  content: z.union([
790
785
  z.array(
791
786
  z.object({
@@ -1044,21 +1039,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1044
1039
  id: z.string(),
1045
1040
  name: z.string(),
1046
1041
  input: z.unknown(),
1047
- caller: z
1048
- .union([
1049
- z.object({
1050
- type: z.literal('code_execution_20250825'),
1051
- tool_id: z.string(),
1052
- }),
1053
- z.object({
1054
- type: z.literal('code_execution_20260120'),
1055
- tool_id: z.string(),
1056
- }),
1057
- z.object({
1058
- type: z.literal('direct'),
1059
- }),
1060
- ])
1061
- .optional(),
1042
+ caller: anthropicToolCallCallerSchema.optional(),
1062
1043
  }),
1063
1044
  ]),
1064
1045
  )
@@ -1091,21 +1072,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1091
1072
  // Programmatic tool calling: input may be present directly for deferred tool calls
1092
1073
  input: z.record(z.string(), z.unknown()).optional(),
1093
1074
  // Programmatic tool calling: caller info when triggered from code execution
1094
- caller: z
1095
- .union([
1096
- z.object({
1097
- type: z.literal('code_execution_20250825'),
1098
- tool_id: z.string(),
1099
- }),
1100
- z.object({
1101
- type: z.literal('code_execution_20260120'),
1102
- tool_id: z.string(),
1103
- }),
1104
- z.object({
1105
- type: z.literal('direct'),
1106
- }),
1107
- ])
1108
- .optional(),
1075
+ caller: anthropicToolCallCallerSchema.optional(),
1109
1076
  }),
1110
1077
  z.object({
1111
1078
  type: z.literal('redacted_thinking'),
@@ -1120,17 +1087,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1120
1087
  id: z.string(),
1121
1088
  name: z.string(),
1122
1089
  input: z.record(z.string(), z.unknown()).nullish(),
1123
- caller: z
1124
- .union([
1125
- z.object({
1126
- type: z.literal('code_execution_20260120'),
1127
- tool_id: z.string(),
1128
- }),
1129
- z.object({
1130
- type: z.literal('direct'),
1131
- }),
1132
- ])
1133
- .optional(),
1090
+ caller: anthropicToolCallCallerSchema.optional(),
1134
1091
  }),
1135
1092
  z.object({
1136
1093
  type: z.literal('mcp_tool_use'),
@@ -1153,6 +1110,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1153
1110
  z.object({
1154
1111
  type: z.literal('web_fetch_tool_result'),
1155
1112
  tool_use_id: z.string(),
1113
+ caller: anthropicToolCallCallerSchema.optional(),
1156
1114
  content: z.union([
1157
1115
  z.object({
1158
1116
  type: z.literal('web_fetch_result'),
@@ -1185,6 +1143,7 @@ export const anthropicChunkSchema = lazySchema(() =>
1185
1143
  z.object({
1186
1144
  type: z.literal('web_search_tool_result'),
1187
1145
  tool_use_id: z.string(),
1146
+ caller: anthropicToolCallCallerSchema.optional(),
1188
1147
  content: z.union([
1189
1148
  z.array(
1190
1149
  z.object({
@@ -51,6 +51,7 @@ import {
51
51
  type AnthropicResponseContextManagement,
52
52
  type AnthropicStopDetails,
53
53
  type AnthropicTool,
54
+ type AnthropicToolCallCaller,
54
55
  type Citation,
55
56
  } from './anthropic-api';
56
57
  import {
@@ -127,6 +128,24 @@ function createCitationSource(
127
128
  };
128
129
  }
129
130
 
131
+ function getAnthropicCallerInfo(caller: AnthropicToolCallCaller | undefined) {
132
+ return caller == null
133
+ ? undefined
134
+ : {
135
+ type: caller.type,
136
+ toolId: 'tool_id' in caller ? caller.tool_id : undefined,
137
+ };
138
+ }
139
+
140
+ function getAnthropicCallerMetadata(
141
+ caller: AnthropicToolCallCaller | undefined,
142
+ ) {
143
+ const callerInfo = getAnthropicCallerInfo(caller);
144
+ return callerInfo == null
145
+ ? {}
146
+ : { providerMetadata: { anthropic: { caller: callerInfo } } };
147
+ }
148
+
130
149
  export type AnthropicLanguageModelConfig = {
131
150
  provider: string;
132
151
  baseURL: string;
@@ -1061,26 +1080,12 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1061
1080
  text: JSON.stringify(part.input),
1062
1081
  });
1063
1082
  } else {
1064
- const caller = part.caller;
1065
- const callerInfo = caller
1066
- ? {
1067
- type: caller.type,
1068
- toolId: 'tool_id' in caller ? caller.tool_id : undefined,
1069
- }
1070
- : undefined;
1071
-
1072
1083
  content.push({
1073
1084
  type: 'tool-call',
1074
1085
  toolCallId: part.id,
1075
1086
  toolName: part.name,
1076
1087
  input: JSON.stringify(part.input),
1077
- ...(callerInfo && {
1078
- providerMetadata: {
1079
- anthropic: {
1080
- caller: callerInfo,
1081
- },
1082
- },
1083
- }),
1088
+ ...getAnthropicCallerMetadata(part.caller),
1084
1089
  });
1085
1090
  }
1086
1091
 
@@ -1109,6 +1114,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1109
1114
  providerToolName === 'code_execution'
1110
1115
  ? { dynamic: true }
1111
1116
  : {}),
1117
+ ...getAnthropicCallerMetadata(part.caller),
1112
1118
  });
1113
1119
  } else if (
1114
1120
  part.name === 'web_search' ||
@@ -1138,6 +1144,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1138
1144
  ...(markCodeExecutionDynamic && part.name === 'code_execution'
1139
1145
  ? { dynamic: true }
1140
1146
  : {}),
1147
+ ...getAnthropicCallerMetadata(part.caller),
1141
1148
  });
1142
1149
  } else if (
1143
1150
  part.name === 'tool_search_tool_regex' ||
@@ -1150,6 +1157,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1150
1157
  toolName: toolNameMapping.toCustomToolName(part.name),
1151
1158
  input: JSON.stringify(part.input),
1152
1159
  providerExecuted: true,
1160
+ ...getAnthropicCallerMetadata(part.caller),
1153
1161
  });
1154
1162
  } else if (part.name === 'advisor') {
1155
1163
  content.push({
@@ -1158,6 +1166,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1158
1166
  toolName: toolNameMapping.toCustomToolName('advisor'),
1159
1167
  input: JSON.stringify(part.input),
1160
1168
  providerExecuted: true,
1169
+ ...getAnthropicCallerMetadata(part.caller),
1161
1170
  });
1162
1171
  }
1163
1172
 
@@ -1218,6 +1227,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1218
1227
  },
1219
1228
  },
1220
1229
  },
1230
+ ...getAnthropicCallerMetadata(part.caller),
1221
1231
  });
1222
1232
  } else if (part.content.type === 'web_fetch_tool_result_error') {
1223
1233
  content.push({
@@ -1229,6 +1239,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1229
1239
  type: 'web_fetch_tool_result_error',
1230
1240
  errorCode: part.content.error_code,
1231
1241
  },
1242
+ ...getAnthropicCallerMetadata(part.caller),
1232
1243
  });
1233
1244
  }
1234
1245
  break;
@@ -1246,6 +1257,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1246
1257
  encryptedContent: result.encrypted_content,
1247
1258
  type: result.type,
1248
1259
  })),
1260
+ ...getAnthropicCallerMetadata(part.caller),
1249
1261
  });
1250
1262
 
1251
1263
  for (const result of part.content) {
@@ -1272,6 +1284,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1272
1284
  type: 'web_search_tool_result_error',
1273
1285
  errorCode: part.content.error_code,
1274
1286
  },
1287
+ ...getAnthropicCallerMetadata(part.caller),
1275
1288
  });
1276
1289
  }
1277
1290
  break;
@@ -1738,15 +1751,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1738
1751
  id: String(value.index),
1739
1752
  });
1740
1753
  } else {
1741
- // Extract caller info for type-safe access
1742
- const caller = part.caller;
1743
- const callerInfo = caller
1744
- ? {
1745
- type: caller.type,
1746
- toolId:
1747
- 'tool_id' in caller ? caller.tool_id : undefined,
1748
- }
1749
- : undefined;
1754
+ const callerInfo = getAnthropicCallerInfo(part.caller);
1750
1755
 
1751
1756
  // Programmatic tool calling: for deferred tool calls from code_execution,
1752
1757
  // input may be present directly in content_block_start.
@@ -1776,6 +1781,8 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1776
1781
  }
1777
1782
 
1778
1783
  case 'server_tool_use': {
1784
+ const callerInfo = getAnthropicCallerInfo(part.caller);
1785
+
1779
1786
  if (
1780
1787
  [
1781
1788
  'web_fetch',
@@ -1832,6 +1839,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1832
1839
  firstDelta: finalInput.length === 0,
1833
1840
  providerToolName,
1834
1841
  providerToolInputType,
1842
+ ...(callerInfo && { caller: callerInfo }),
1835
1843
  };
1836
1844
 
1837
1845
  controller.enqueue({
@@ -1865,6 +1873,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1865
1873
  providerExecuted: true,
1866
1874
  firstDelta: true,
1867
1875
  providerToolName: part.name,
1876
+ ...(callerInfo && { caller: callerInfo }),
1868
1877
  };
1869
1878
 
1870
1879
  controller.enqueue({
@@ -1885,6 +1894,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1885
1894
  providerExecuted: true,
1886
1895
  firstDelta: true,
1887
1896
  providerToolName: part.name,
1897
+ ...(callerInfo && { caller: callerInfo }),
1888
1898
  };
1889
1899
 
1890
1900
  controller.enqueue({
@@ -1923,6 +1933,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1923
1933
  },
1924
1934
  },
1925
1935
  },
1936
+ ...getAnthropicCallerMetadata(part.caller),
1926
1937
  });
1927
1938
  } else if (
1928
1939
  part.content.type === 'web_fetch_tool_result_error'
@@ -1936,6 +1947,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1936
1947
  type: 'web_fetch_tool_result_error',
1937
1948
  errorCode: part.content.error_code,
1938
1949
  },
1950
+ ...getAnthropicCallerMetadata(part.caller),
1939
1951
  });
1940
1952
  }
1941
1953
 
@@ -1955,6 +1967,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1955
1967
  encryptedContent: result.encrypted_content,
1956
1968
  type: result.type,
1957
1969
  })),
1970
+ ...getAnthropicCallerMetadata(part.caller),
1958
1971
  });
1959
1972
 
1960
1973
  for (const result of part.content) {
@@ -1981,6 +1994,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
1981
1994
  type: 'web_search_tool_result_error',
1982
1995
  errorCode: part.content.error_code,
1983
1996
  },
1997
+ ...getAnthropicCallerMetadata(part.caller),
1984
1998
  });
1985
1999
  }
1986
2000
  return;
@@ -2490,14 +2504,7 @@ export class AnthropicLanguageModel implements LanguageModelV4 {
2490
2504
  ) {
2491
2505
  const part = value.message.content[contentIndex];
2492
2506
  if (part.type === 'tool_use') {
2493
- const caller = part.caller;
2494
- const callerInfo = caller
2495
- ? {
2496
- type: caller.type,
2497
- toolId:
2498
- 'tool_id' in caller ? caller.tool_id : undefined,
2499
- }
2500
- : undefined;
2507
+ const callerInfo = getAnthropicCallerInfo(part.caller);
2501
2508
 
2502
2509
  controller.enqueue({
2503
2510
  type: 'tool-input-start',
@@ -26,6 +26,7 @@ import {
26
26
  type AnthropicSystemMessage,
27
27
  type AnthropicTextContent,
28
28
  type AnthropicToolChangeContent,
29
+ type AnthropicToolCallCaller,
29
30
  type AnthropicToolResultContent,
30
31
  type AnthropicUserMessage,
31
32
  type AnthropicWebFetchToolResultContent,
@@ -723,6 +724,8 @@ export async function convertToAnthropicPrompt({
723
724
  }
724
725
 
725
726
  case 'tool-call': {
727
+ const caller = getAnthropicCaller(part.providerOptions);
728
+
726
729
  if (part.providerExecuted) {
727
730
  const providerToolName = toolNameMapping.toProviderToolName(
728
731
  part.toolName,
@@ -771,6 +774,7 @@ export async function convertToAnthropicPrompt({
771
774
  id: part.toolCallId,
772
775
  name: codeExecutionType, // map back to subtool name
773
776
  input: inputWithoutType,
777
+ ...(caller && { caller }),
774
778
  cache_control: cacheControl,
775
779
  });
776
780
  } else if (
@@ -791,6 +795,7 @@ export async function convertToAnthropicPrompt({
791
795
  id: part.toolCallId,
792
796
  name: 'code_execution',
793
797
  input: inputWithoutType,
798
+ ...(caller && { caller }),
794
799
  cache_control: cacheControl,
795
800
  });
796
801
  } else {
@@ -804,6 +809,7 @@ export async function convertToAnthropicPrompt({
804
809
  id: part.toolCallId,
805
810
  name: providerToolName,
806
811
  input: part.input,
812
+ ...(caller && { caller }),
807
813
  cache_control: cacheControl,
808
814
  });
809
815
  } else if (
@@ -815,6 +821,7 @@ export async function convertToAnthropicPrompt({
815
821
  id: part.toolCallId,
816
822
  name: providerToolName,
817
823
  input: part.input,
824
+ ...(caller && { caller }),
818
825
  cache_control: cacheControl,
819
826
  });
820
827
  } else if (providerToolName === 'advisor') {
@@ -824,6 +831,7 @@ export async function convertToAnthropicPrompt({
824
831
  id: part.toolCallId,
825
832
  name: 'advisor',
826
833
  input: {},
834
+ ...(caller && { caller }),
827
835
  cache_control: cacheControl,
828
836
  });
829
837
  } else {
@@ -837,26 +845,6 @@ export async function convertToAnthropicPrompt({
837
845
  break;
838
846
  }
839
847
 
840
- // Extract caller info from provider options for programmatic tool calling
841
- const callerOptions = part.providerOptions?.anthropic as
842
- | { caller?: { type: string; toolId?: string } }
843
- | undefined;
844
- const caller = callerOptions?.caller
845
- ? (callerOptions.caller.type === 'code_execution_20250825' ||
846
- callerOptions.caller.type ===
847
- 'code_execution_20260120') &&
848
- callerOptions.caller.toolId
849
- ? {
850
- type: callerOptions.caller.type as
851
- | 'code_execution_20250825'
852
- | 'code_execution_20260120',
853
- tool_id: callerOptions.caller.toolId,
854
- }
855
- : callerOptions.caller.type === 'direct'
856
- ? { type: 'direct' as const }
857
- : undefined
858
- : undefined;
859
-
860
848
  anthropicContent.push({
861
849
  type: 'tool_use',
862
850
  id: part.toolCallId,
@@ -872,6 +860,7 @@ export async function convertToAnthropicPrompt({
872
860
  const providerToolName = toolNameMapping.toProviderToolName(
873
861
  part.toolName,
874
862
  );
863
+ const caller = getAnthropicCaller(part.providerOptions);
875
864
 
876
865
  if (mcpToolUseIds.has(part.toolCallId)) {
877
866
  const output = part.output;
@@ -1078,6 +1067,7 @@ export async function convertToAnthropicPrompt({
1078
1067
  extractErrorValue(output.value).errorCode ??
1079
1068
  'unavailable',
1080
1069
  },
1070
+ ...(caller && { caller }),
1081
1071
  cache_control: cacheControl,
1082
1072
  });
1083
1073
 
@@ -1122,6 +1112,7 @@ export async function convertToAnthropicPrompt({
1122
1112
  >['content']['source'],
1123
1113
  },
1124
1114
  },
1115
+ ...(caller && { caller }),
1125
1116
  cache_control: cacheControl,
1126
1117
  });
1127
1118
 
@@ -1141,6 +1132,7 @@ export async function convertToAnthropicPrompt({
1141
1132
  extractErrorValue(output.value).errorCode ??
1142
1133
  'unavailable',
1143
1134
  },
1135
+ ...(caller && { caller }),
1144
1136
  cache_control: cacheControl,
1145
1137
  });
1146
1138
 
@@ -1174,6 +1166,7 @@ export async function convertToAnthropicPrompt({
1174
1166
  encrypted_content: result.encryptedContent,
1175
1167
  type: result.type,
1176
1168
  })),
1169
+ ...(caller && { caller }),
1177
1170
  cache_control: cacheControl,
1178
1171
  });
1179
1172
 
@@ -1406,6 +1399,29 @@ function moveToolUseBlocksToEnd(
1406
1399
  return result;
1407
1400
  }
1408
1401
 
1402
+ function getAnthropicCaller(
1403
+ providerOptions: SharedV4ProviderMetadata | undefined,
1404
+ ): AnthropicToolCallCaller | undefined {
1405
+ const caller = (
1406
+ providerOptions?.anthropic as
1407
+ | { caller?: { type: string; toolId?: string } }
1408
+ | undefined
1409
+ )?.caller;
1410
+
1411
+ if (
1412
+ (caller?.type === 'code_execution_20250825' ||
1413
+ caller?.type === 'code_execution_20260120') &&
1414
+ caller.toolId
1415
+ ) {
1416
+ return {
1417
+ type: caller.type,
1418
+ tool_id: caller.toolId,
1419
+ };
1420
+ }
1421
+
1422
+ return caller?.type === 'direct' ? { type: 'direct' } : undefined;
1423
+ }
1424
+
1409
1425
  // wrap invalid tool call input because Anthropic requires it to be an object
1410
1426
  function toAnthropicToolInput(input: unknown): JSONObject {
1411
1427
  return typeof input === 'object' && input !== null && !Array.isArray(input)