@ai-sdk/amazon-bedrock 4.0.163 → 4.0.166

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
@@ -239,7 +239,8 @@ import {
239
239
  async function prepareTools({
240
240
  tools,
241
241
  toolChoice,
242
- modelId
242
+ modelId,
243
+ disableParallelToolUse
243
244
  }) {
244
245
  var _a;
245
246
  const toolWarnings = [];
@@ -285,6 +286,7 @@ async function prepareTools({
285
286
  } = await prepareAnthropicTools({
286
287
  tools: ProviderTools,
287
288
  toolChoice,
289
+ disableParallelToolUse,
288
290
  supportsStructuredOutput: false,
289
291
  supportsStrictTools: false
290
292
  });
@@ -340,8 +342,17 @@ async function prepareTools({
340
342
  }
341
343
  });
342
344
  }
345
+ if (isAnthropicModel && !usingAnthropicTools && disableParallelToolUse && bedrockTools.length > 0 && (toolChoice == null ? void 0 : toolChoice.type) !== "none") {
346
+ additionalTools = {
347
+ tool_choice: (toolChoice == null ? void 0 : toolChoice.type) === "required" ? { type: "any", disable_parallel_tool_use: true } : (toolChoice == null ? void 0 : toolChoice.type) === "tool" ? {
348
+ type: "tool",
349
+ name: toolChoice.toolName,
350
+ disable_parallel_tool_use: true
351
+ } : { type: "auto", disable_parallel_tool_use: true }
352
+ };
353
+ }
343
354
  let bedrockToolChoice = void 0;
344
- if (!usingAnthropicTools && bedrockTools.length > 0 && toolChoice) {
355
+ if (!usingAnthropicTools && (additionalTools == null ? void 0 : additionalTools.tool_choice) == null && bedrockTools.length > 0 && toolChoice) {
345
356
  const type = toolChoice.type;
346
357
  switch (type) {
347
358
  case "auto":
@@ -692,7 +703,12 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
692
703
  }
693
704
  pushCachePoint(bedrockContent, providerOptions);
694
705
  }
695
- messages.push({ role: "user", content: bedrockContent });
706
+ const previousMessage = messages.at(-1);
707
+ if ((previousMessage == null ? void 0 : previousMessage.role) === "user") {
708
+ previousMessage.content.push(...bedrockContent);
709
+ } else {
710
+ messages.push({ role: "user", content: bedrockContent });
711
+ }
696
712
  break;
697
713
  }
698
714
  case "assistant": {
@@ -701,15 +717,56 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
701
717
  const message = block.messages[j];
702
718
  const isLastMessage = j === block.messages.length - 1;
703
719
  const { content } = message;
704
- const hasReasoningBlocks = content.some(
705
- (part) => part.type === "reasoning"
720
+ const convertedReasoningContent = await Promise.all(
721
+ content.map(async (part) => {
722
+ if (part.type !== "reasoning") {
723
+ return void 0;
724
+ }
725
+ const metadata = await parseProviderOptions({
726
+ provider: "bedrock",
727
+ providerOptions: part.providerOptions,
728
+ schema: bedrockReasoningMetadataSchema
729
+ });
730
+ if ((metadata == null ? void 0 : metadata.signature) != null) {
731
+ return {
732
+ reasoningContent: {
733
+ reasoningText: {
734
+ // do not trim reasoning text when a signature is present:
735
+ // the signature validates the exact original bytes
736
+ text: part.text,
737
+ signature: metadata.signature
738
+ }
739
+ }
740
+ };
741
+ }
742
+ if ((metadata == null ? void 0 : metadata.redactedContent) != null) {
743
+ return {
744
+ reasoningContent: {
745
+ redactedContent: metadata.redactedContent
746
+ }
747
+ };
748
+ }
749
+ if ((metadata == null ? void 0 : metadata.redactedData) != null) {
750
+ return {
751
+ reasoningContent: {
752
+ redactedReasoning: {
753
+ data: metadata.redactedData
754
+ }
755
+ }
756
+ };
757
+ }
758
+ return void 0;
759
+ })
760
+ );
761
+ const hasReplayableReasoningBlocks = convertedReasoningContent.some(
762
+ (part) => part != null
706
763
  );
707
764
  for (let k = 0; k < content.length; k++) {
708
765
  const part = content[k];
709
766
  const isLastContentPart = k === content.length - 1;
710
767
  switch (part.type) {
711
768
  case "text": {
712
- if (!part.text.trim() && !hasReasoningBlocks) {
769
+ if (!part.text.trim() && !hasReplayableReasoningBlocks) {
713
770
  break;
714
771
  }
715
772
  bedrockContent.push({
@@ -728,34 +785,9 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
728
785
  break;
729
786
  }
730
787
  case "reasoning": {
731
- const reasoningMetadata = await parseProviderOptions({
732
- provider: "bedrock",
733
- providerOptions: part.providerOptions,
734
- schema: bedrockReasoningMetadataSchema
735
- });
736
- if ((reasoningMetadata == null ? void 0 : reasoningMetadata.signature) != null) {
737
- bedrockContent.push({
738
- reasoningContent: {
739
- reasoningText: {
740
- text: part.text,
741
- signature: reasoningMetadata.signature
742
- }
743
- }
744
- });
745
- } else if ((reasoningMetadata == null ? void 0 : reasoningMetadata.redactedContent) != null) {
746
- bedrockContent.push({
747
- reasoningContent: {
748
- redactedContent: reasoningMetadata.redactedContent
749
- }
750
- });
751
- } else if ((reasoningMetadata == null ? void 0 : reasoningMetadata.redactedData) != null) {
752
- bedrockContent.push({
753
- reasoningContent: {
754
- redactedReasoning: {
755
- data: reasoningMetadata.redactedData
756
- }
757
- }
758
- });
788
+ const convertedPart = convertedReasoningContent[k];
789
+ if (convertedPart != null) {
790
+ bedrockContent.push(convertedPart);
759
791
  }
760
792
  break;
761
793
  }
@@ -774,7 +806,9 @@ async function convertToBedrockChatMessages(prompt, isMistral = false) {
774
806
  }
775
807
  pushCachePoint(bedrockContent, message.providerOptions);
776
808
  }
777
- messages.push({ role: "assistant", content: bedrockContent });
809
+ if (bedrockContent.some((block2) => !("cachePoint" in block2))) {
810
+ messages.push({ role: "assistant", content: bedrockContent });
811
+ }
778
812
  break;
779
813
  }
780
814
  default: {
@@ -883,6 +917,9 @@ function mapBedrockFinishReason(finishReason, isJsonResponseFromTool) {
883
917
  }
884
918
 
885
919
  // src/bedrock-chat-language-model.ts
920
+ var anthropicProviderOptions = z4.object({
921
+ disableParallelToolUse: z4.boolean().optional()
922
+ });
886
923
  var BedrockChatLanguageModel = class {
887
924
  constructor(modelId, config) {
888
925
  this.modelId = modelId;
@@ -908,12 +945,17 @@ var BedrockChatLanguageModel = class {
908
945
  toolChoice,
909
946
  providerOptions
910
947
  }) {
911
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
948
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
912
949
  const bedrockOptions = (_a = await parseProviderOptions2({
913
950
  provider: "bedrock",
914
951
  providerOptions,
915
952
  schema: amazonBedrockLanguageModelOptions
916
953
  })) != null ? _a : {};
954
+ const anthropicOptions = await parseProviderOptions2({
955
+ provider: "anthropic",
956
+ providerOptions,
957
+ schema: anthropicProviderOptions
958
+ });
917
959
  const warnings = [];
918
960
  if (frequencyPenalty != null) {
919
961
  warnings.push({
@@ -955,11 +997,11 @@ var BedrockChatLanguageModel = class {
955
997
  details: "Only text and json response formats are supported."
956
998
  });
957
999
  }
958
- const isAnthropicModel = this.modelId.includes("anthropic");
959
- const openAIModelId = (_b = /^(?:[^.]+\.)?(openai\..+)$/.exec(this.modelId)) == null ? void 0 : _b[1];
1000
+ const isAnthropicModel = this.modelId.includes("anthropic") || this.modelId.includes(":application-inference-profile/") && ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.budgetTokens) != null;
1001
+ const openAIModelId = (_c = /^(?:[^.]+\.)?(openai\..+)$/.exec(this.modelId)) == null ? void 0 : _c[1];
960
1002
  const isOpenAIModel = openAIModelId != null;
961
- const isOpenAIGptOssModel = (_c = openAIModelId == null ? void 0 : openAIModelId.startsWith("openai.gpt-oss-")) != null ? _c : false;
962
- const isThinkingEnabled = ((_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.type) === "enabled" || ((_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type) === "adaptive";
1003
+ const isOpenAIGptOssModel = (_d = openAIModelId == null ? void 0 : openAIModelId.startsWith("openai.gpt-oss-")) != null ? _d : false;
1004
+ const isThinkingEnabled = ((_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type) === "enabled" || ((_f = bedrockOptions.reasoningConfig) == null ? void 0 : _f.type) === "adaptive";
963
1005
  const { supportsStructuredOutput: modelSupportsStructuredOutput } = getModelCapabilities(this.modelId);
964
1006
  const useNativeStructuredOutput = isAnthropicModel && supportsNativeStructuredOutput(this.modelId) && (modelSupportsStructuredOutput || isThinkingEnabled) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
965
1007
  const useJsonInstructionForStructuredOutput = isAnthropicModel && !supportsStrictTools(this.modelId) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && tools != null && tools.length > 0;
@@ -972,7 +1014,8 @@ var BedrockChatLanguageModel = class {
972
1014
  const { toolConfig, additionalTools, toolWarnings, betas } = await prepareTools({
973
1015
  tools: jsonResponseTool ? [...tools != null ? tools : [], jsonResponseTool] : tools,
974
1016
  toolChoice: jsonResponseTool != null ? { type: "required" } : toolChoice,
975
- modelId: this.modelId
1017
+ modelId: this.modelId,
1018
+ disableParallelToolUse: anthropicOptions == null ? void 0 : anthropicOptions.disableParallelToolUse
976
1019
  });
977
1020
  warnings.push(...toolWarnings);
978
1021
  if (additionalTools) {
@@ -982,16 +1025,16 @@ var BedrockChatLanguageModel = class {
982
1025
  };
983
1026
  }
984
1027
  if (betas.size > 0 || bedrockOptions.anthropicBeta) {
985
- const existingBetas = (_f = bedrockOptions.anthropicBeta) != null ? _f : [];
1028
+ const existingBetas = (_g = bedrockOptions.anthropicBeta) != null ? _g : [];
986
1029
  const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
987
1030
  bedrockOptions.additionalModelRequestFields = {
988
1031
  ...bedrockOptions.additionalModelRequestFields,
989
1032
  anthropic_beta: mergedBetas
990
1033
  };
991
1034
  }
992
- const thinkingType = (_g = bedrockOptions.reasoningConfig) == null ? void 0 : _g.type;
993
- const thinkingBudget = thinkingType === "enabled" ? (_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.budgetTokens : void 0;
994
- const thinkingDisplay = thinkingType === "adaptive" ? (_i = bedrockOptions.reasoningConfig) == null ? void 0 : _i.display : void 0;
1035
+ const thinkingType = (_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.type;
1036
+ const thinkingBudget = thinkingType === "enabled" ? (_i = bedrockOptions.reasoningConfig) == null ? void 0 : _i.budgetTokens : void 0;
1037
+ const thinkingDisplay = thinkingType === "adaptive" ? (_j = bedrockOptions.reasoningConfig) == null ? void 0 : _j.display : void 0;
995
1038
  const isAnthropicThinkingEnabled = isAnthropicModel && isThinkingEnabled;
996
1039
  const inferenceConfig = {
997
1040
  ...maxOutputTokens != null && { maxTokens: maxOutputTokens },
@@ -1024,7 +1067,7 @@ var BedrockChatLanguageModel = class {
1024
1067
  };
1025
1068
  }
1026
1069
  } else if (!isAnthropicModel) {
1027
- if (((_j = bedrockOptions.reasoningConfig) == null ? void 0 : _j.budgetTokens) != null) {
1070
+ if (((_k = bedrockOptions.reasoningConfig) == null ? void 0 : _k.budgetTokens) != null) {
1028
1071
  warnings.push({
1029
1072
  type: "unsupported",
1030
1073
  feature: "budgetTokens",
@@ -1039,13 +1082,13 @@ var BedrockChatLanguageModel = class {
1039
1082
  });
1040
1083
  }
1041
1084
  }
1042
- const maxReasoningEffort = (_k = bedrockOptions.reasoningConfig) == null ? void 0 : _k.maxReasoningEffort;
1085
+ const maxReasoningEffort = (_l = bedrockOptions.reasoningConfig) == null ? void 0 : _l.maxReasoningEffort;
1043
1086
  if (maxReasoningEffort != null) {
1044
1087
  if (isAnthropicModel) {
1045
1088
  bedrockOptions.additionalModelRequestFields = {
1046
1089
  ...bedrockOptions.additionalModelRequestFields,
1047
1090
  output_config: {
1048
- ...(_l = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _l.output_config,
1091
+ ...(_m = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _m.output_config,
1049
1092
  effort: maxReasoningEffort
1050
1093
  }
1051
1094
  };
@@ -1056,7 +1099,7 @@ var BedrockChatLanguageModel = class {
1056
1099
  } : {
1057
1100
  ...bedrockOptions.additionalModelRequestFields,
1058
1101
  reasoning: {
1059
- ...(_m = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _m.reasoning,
1102
+ ...(_n = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _n.reasoning,
1060
1103
  effort: maxReasoningEffort
1061
1104
  }
1062
1105
  };
@@ -1075,7 +1118,7 @@ var BedrockChatLanguageModel = class {
1075
1118
  bedrockOptions.additionalModelRequestFields = {
1076
1119
  ...bedrockOptions.additionalModelRequestFields,
1077
1120
  output_config: {
1078
- ...(_n = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _n.output_config,
1121
+ ...(_o = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _o.output_config,
1079
1122
  format: {
1080
1123
  type: "json_schema",
1081
1124
  schema: sanitizeJsonSchema(responseFormat.schema)
@@ -1107,7 +1150,7 @@ var BedrockChatLanguageModel = class {
1107
1150
  details: "topK is not supported when thinking is enabled"
1108
1151
  });
1109
1152
  }
1110
- const hasAnyTools = ((_p = (_o = toolConfig.tools) == null ? void 0 : _o.length) != null ? _p : 0) > 0 || additionalTools;
1153
+ const hasAnyTools = ((_q = (_p = toolConfig.tools) == null ? void 0 : _p.length) != null ? _q : 0) > 0 || additionalTools;
1111
1154
  let filteredPrompt = prompt;
1112
1155
  if (!hasAnyTools) {
1113
1156
  const hasToolContent = prompt.some(
@@ -1790,6 +1833,9 @@ var BedrockStreamSchema = z4.object({
1790
1833
  contentBlockIndex: z4.number(),
1791
1834
  delta: z4.union([
1792
1835
  z4.object({ text: z4.string() }),
1836
+ z4.object({
1837
+ citation: z4.record(z4.string(), z4.unknown())
1838
+ }),
1793
1839
  z4.object({ toolUse: z4.object({ input: z4.string() }) }),
1794
1840
  z4.object({
1795
1841
  reasoningContent: z4.object({ text: z4.string() })
@@ -1919,7 +1965,11 @@ var BedrockEmbeddingModel = class {
1919
1965
  this.supportsParallelCalls = true;
1920
1966
  }
1921
1967
  get maxEmbeddingsPerCall() {
1922
- return isCohereEmbeddingModel(this.modelId) ? 96 : 1;
1968
+ return this.modelFamily === "cohere" ? 96 : 1;
1969
+ }
1970
+ get modelFamily() {
1971
+ var _a;
1972
+ return (_a = this.config.modelFamily) != null ? _a : detectEmbeddingModelFamily(this.modelId);
1923
1973
  }
1924
1974
  getUrl(modelId) {
1925
1975
  const encodedModelId = encodeURIComponent(modelId);
@@ -1945,9 +1995,8 @@ var BedrockEmbeddingModel = class {
1945
1995
  providerOptions,
1946
1996
  schema: amazonBedrockEmbeddingModelOptionsSchema
1947
1997
  })) != null ? _a : {};
1948
- const isNovaModel = isNovaEmbeddingModel(this.modelId);
1949
- const isCohereModel = isCohereEmbeddingModel(this.modelId);
1950
- const args = isNovaModel ? {
1998
+ const modelFamily = this.modelFamily;
1999
+ const args = modelFamily === "nova" ? {
1951
2000
  taskType: "SINGLE_EMBEDDING",
1952
2001
  singleEmbeddingParams: {
1953
2002
  embeddingPurpose: (_b = bedrockOptions.embeddingPurpose) != null ? _b : "GENERIC_INDEX",
@@ -1957,7 +2006,7 @@ var BedrockEmbeddingModel = class {
1957
2006
  value: values[0]
1958
2007
  }
1959
2008
  }
1960
- } : isCohereModel ? {
2009
+ } : modelFamily === "cohere" ? {
1961
2010
  // Cohere embedding models on Bedrock require `input_type`.
1962
2011
  // Without it, the service attempts other schema branches and rejects the request.
1963
2012
  input_type: (_e = bedrockOptions.inputType) != null ? _e : "search_query",
@@ -2014,7 +2063,10 @@ function isCohereEmbeddingModel(modelId) {
2014
2063
  return modelId.includes("cohere.embed-");
2015
2064
  }
2016
2065
  function isNovaEmbeddingModel(modelId) {
2017
- return modelId.startsWith("amazon.nova-") && modelId.includes("embed");
2066
+ return modelId.includes("amazon.nova-") && modelId.includes("embed");
2067
+ }
2068
+ function detectEmbeddingModelFamily(modelId) {
2069
+ return isNovaEmbeddingModel(modelId) ? "nova" : isCohereEmbeddingModel(modelId) ? "cohere" : "titan";
2018
2070
  }
2019
2071
  var BedrockEmbeddingResponseSchema = z6.union([
2020
2072
  // Titan-style response
@@ -2268,7 +2320,7 @@ import {
2268
2320
  import { AwsV4Signer } from "aws4fetch";
2269
2321
 
2270
2322
  // src/version.ts
2271
- var VERSION = true ? "4.0.163" : "0.0.0-test";
2323
+ var VERSION = true ? "4.0.166" : "0.0.0-test";
2272
2324
 
2273
2325
  // src/bedrock-sigv4-fetch.ts
2274
2326
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
@@ -2586,10 +2638,11 @@ Original error: ${errorMessage}`
2586
2638
  }
2587
2639
  return createChatModel(modelId);
2588
2640
  };
2589
- const createEmbeddingModel = (modelId) => new BedrockEmbeddingModel(modelId, {
2641
+ const createEmbeddingModel = (modelId, settings = {}) => new BedrockEmbeddingModel(modelId, {
2590
2642
  baseUrl: getBedrockRuntimeBaseUrl,
2591
2643
  headers: getHeaders,
2592
- fetch: fetchFunction
2644
+ fetch: fetchFunction,
2645
+ modelFamily: settings.modelFamily
2593
2646
  });
2594
2647
  const createImageModel = (modelId) => new BedrockImageModel(modelId, {
2595
2648
  baseUrl: getBedrockRuntimeBaseUrl,