@ai-sdk/amazon-bedrock 3.0.107 → 3.0.109

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
@@ -8,12 +8,13 @@ import {
8
8
  } from "@ai-sdk/provider-utils";
9
9
 
10
10
  // src/version.ts
11
- var VERSION = true ? "3.0.107" : "0.0.0-test";
11
+ var VERSION = true ? "3.0.109" : "0.0.0-test";
12
12
 
13
13
  // src/bedrock-provider.ts
14
14
  import { anthropicTools as anthropicTools2 } from "@ai-sdk/anthropic/internal";
15
15
 
16
16
  // src/bedrock-chat-language-model.ts
17
+ import { sanitizeJsonSchema } from "@ai-sdk/anthropic/internal";
17
18
  import {
18
19
  combineHeaders,
19
20
  createJsonErrorResponseHandler,
@@ -372,6 +373,9 @@ async function shouldEnableCitations(providerMetadata) {
372
373
  });
373
374
  return (_b = (_a = bedrockOptions == null ? void 0 : bedrockOptions.citations) == null ? void 0 : _a.enabled) != null ? _b : false;
374
375
  }
376
+ function sanitizeToolName(toolName) {
377
+ return toolName.replace(/[^a-zA-Z0-9_-]/g, "") || "_";
378
+ }
375
379
  async function convertToBedrockChatMessages(prompt) {
376
380
  var _a;
377
381
  const blocks = groupIntoBlocks(prompt);
@@ -415,9 +419,22 @@ async function convertToBedrockChatMessages(prompt) {
415
419
  }
416
420
  case "file": {
417
421
  if (part.data instanceof URL) {
418
- throw new UnsupportedFunctionalityError2({
419
- functionality: "File URL data"
422
+ if (part.data.protocol !== "s3:" || !part.mediaType.startsWith("image/")) {
423
+ throw new UnsupportedFunctionalityError2({
424
+ functionality: "File URL data"
425
+ });
426
+ }
427
+ bedrockContent.push({
428
+ image: {
429
+ format: getBedrockImageFormat(part.mediaType),
430
+ source: {
431
+ s3Location: {
432
+ uri: part.data.toString()
433
+ }
434
+ }
435
+ }
420
436
  });
437
+ break;
421
438
  }
422
439
  if (part.mediaType.startsWith("image/")) {
423
440
  bedrockContent.push({
@@ -577,21 +594,6 @@ async function convertToBedrockChatMessages(prompt) {
577
594
  }
578
595
  });
579
596
  }
580
- } else if (part.providerOptions == null || Object.keys(part.providerOptions).every(
581
- (k2) => k2 === "bedrock" || k2 === "amazonBedrock"
582
- )) {
583
- bedrockContent.push({
584
- reasoningContent: {
585
- reasoningText: {
586
- text: trimIfLast(
587
- isLastBlock,
588
- isLastMessage,
589
- isLastContentPart,
590
- part.text
591
- )
592
- }
593
- }
594
- });
595
597
  }
596
598
  break;
597
599
  }
@@ -599,7 +601,7 @@ async function convertToBedrockChatMessages(prompt) {
599
601
  bedrockContent.push({
600
602
  toolUse: {
601
603
  toolUseId: part.toolCallId,
602
- name: part.toolName,
604
+ name: sanitizeToolName(part.toolName),
603
605
  input: part.input
604
606
  }
605
607
  });
@@ -724,7 +726,7 @@ var BedrockChatLanguageModel = class {
724
726
  this.specificationVersion = "v2";
725
727
  this.provider = "amazon-bedrock";
726
728
  this.supportedUrls = {
727
- // no supported urls for bedrock
729
+ "image/*": [/^s3:\/\//]
728
730
  };
729
731
  }
730
732
  async getArgs({
@@ -742,7 +744,7 @@ var BedrockChatLanguageModel = class {
742
744
  toolChoice,
743
745
  providerOptions
744
746
  }) {
745
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j;
747
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m;
746
748
  const bedrockOptions = (_a = await parseProviderOptions2({
747
749
  provider: "bedrock",
748
750
  providerOptions,
@@ -789,7 +791,10 @@ var BedrockChatLanguageModel = class {
789
791
  details: "Only text and json response formats are supported."
790
792
  });
791
793
  }
792
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
794
+ const isAnthropicModel = this.modelId.includes("anthropic");
795
+ const isThinkingRequested = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled" || ((_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "adaptive";
796
+ const useNativeStructuredOutput = isAnthropicModel && supportsNativeStructuredOutput(this.modelId) && isThinkingRequested && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
797
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useNativeStructuredOutput ? {
793
798
  type: "function",
794
799
  name: "json",
795
800
  description: "Respond with a JSON object.",
@@ -808,18 +813,16 @@ var BedrockChatLanguageModel = class {
808
813
  };
809
814
  }
810
815
  if (betas.size > 0 || bedrockOptions.anthropicBeta) {
811
- const existingBetas = (_b = bedrockOptions.anthropicBeta) != null ? _b : [];
816
+ const existingBetas = (_d = bedrockOptions.anthropicBeta) != null ? _d : [];
812
817
  const mergedBetas = betas.size > 0 ? [...existingBetas, ...Array.from(betas)] : existingBetas;
813
818
  bedrockOptions.additionalModelRequestFields = {
814
819
  ...bedrockOptions.additionalModelRequestFields,
815
820
  anthropic_beta: mergedBetas
816
821
  };
817
822
  }
818
- const isAnthropicModel = this.modelId.includes("anthropic");
819
- const thinkingType = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type;
820
- const isThinkingRequested = thinkingType === "enabled" || thinkingType === "adaptive";
821
- const thinkingBudget = thinkingType === "enabled" ? (_d = bedrockOptions.reasoningConfig) == null ? void 0 : _d.budgetTokens : void 0;
822
- const thinkingDisplay = thinkingType === "adaptive" ? (_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.display : void 0;
823
+ const thinkingType = (_e = bedrockOptions.reasoningConfig) == null ? void 0 : _e.type;
824
+ const thinkingBudget = thinkingType === "enabled" ? (_f = bedrockOptions.reasoningConfig) == null ? void 0 : _f.budgetTokens : void 0;
825
+ const thinkingDisplay = thinkingType === "adaptive" ? (_g = bedrockOptions.reasoningConfig) == null ? void 0 : _g.display : void 0;
823
826
  const isAnthropicThinkingEnabled = isAnthropicModel && isThinkingRequested;
824
827
  const inferenceConfig = {
825
828
  ...maxOutputTokens != null && { maxTokens: maxOutputTokens },
@@ -852,7 +855,7 @@ var BedrockChatLanguageModel = class {
852
855
  };
853
856
  }
854
857
  } else if (!isAnthropicModel) {
855
- if (((_f = bedrockOptions.reasoningConfig) == null ? void 0 : _f.budgetTokens) != null) {
858
+ if (((_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.budgetTokens) != null) {
856
859
  warnings.push({
857
860
  type: "unsupported-setting",
858
861
  setting: "budgetTokens",
@@ -867,12 +870,12 @@ var BedrockChatLanguageModel = class {
867
870
  });
868
871
  }
869
872
  }
870
- const maxReasoningEffort = (_g = bedrockOptions.reasoningConfig) == null ? void 0 : _g.maxReasoningEffort;
873
+ const maxReasoningEffort = (_i = bedrockOptions.reasoningConfig) == null ? void 0 : _i.maxReasoningEffort;
871
874
  if (maxReasoningEffort != null && !isAnthropicModel) {
872
875
  bedrockOptions.additionalModelRequestFields = {
873
876
  ...bedrockOptions.additionalModelRequestFields,
874
877
  reasoningConfig: {
875
- ...((_h = bedrockOptions.reasoningConfig) == null ? void 0 : _h.type) != null && {
878
+ ...((_j = bedrockOptions.reasoningConfig) == null ? void 0 : _j.type) != null && {
876
879
  type: bedrockOptions.reasoningConfig.type
877
880
  },
878
881
  maxReasoningEffort
@@ -886,6 +889,18 @@ var BedrockChatLanguageModel = class {
886
889
  }
887
890
  };
888
891
  }
892
+ if (useNativeStructuredOutput && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null) {
893
+ bedrockOptions.additionalModelRequestFields = {
894
+ ...bedrockOptions.additionalModelRequestFields,
895
+ output_config: {
896
+ ...(_k = bedrockOptions.additionalModelRequestFields) == null ? void 0 : _k.output_config,
897
+ format: {
898
+ type: "json_schema",
899
+ schema: sanitizeJsonSchema(responseFormat.schema)
900
+ }
901
+ }
902
+ };
903
+ }
889
904
  if (isAnthropicThinkingEnabled && inferenceConfig.temperature != null) {
890
905
  delete inferenceConfig.temperature;
891
906
  warnings.push({
@@ -910,7 +925,7 @@ var BedrockChatLanguageModel = class {
910
925
  details: "topK is not supported when thinking is enabled"
911
926
  });
912
927
  }
913
- const hasAnyTools = ((_j = (_i = toolConfig.tools) == null ? void 0 : _i.length) != null ? _j : 0) > 0 || additionalTools;
928
+ const hasAnyTools = ((_m = (_l = toolConfig.tools) == null ? void 0 : _l.length) != null ? _m : 0) > 0 || additionalTools;
914
929
  let filteredPrompt = prompt;
915
930
  if (!hasAnyTools) {
916
931
  const hasToolContent = prompt.some(
@@ -1374,6 +1389,9 @@ var BedrockChatLanguageModel = class {
1374
1389
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
1375
1390
  }
1376
1391
  };
1392
+ function supportsNativeStructuredOutput(modelId) {
1393
+ return modelId.includes("anthropic.claude-sonnet-4-5") || modelId.includes("anthropic.claude-haiku-4-5") || modelId.includes("anthropic.claude-opus-4-5") || modelId.includes("anthropic.claude-opus-4-6");
1394
+ }
1377
1395
  var BedrockStopReasonSchema = z4.union([
1378
1396
  z4.enum(BEDROCK_STOP_REASONS),
1379
1397
  z4.string()