@ai-sdk/amazon-bedrock 3.0.0-beta.10 → 3.0.0-beta.11

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/CHANGELOG.md CHANGED
@@ -1,5 +1,11 @@
1
1
  # @ai-sdk/amazon-bedrock
2
2
 
3
+ ## 3.0.0-beta.11
4
+
5
+ ### Patch Changes
6
+
7
+ - a89add7: fix(amazon-bedrock): add structured output support for claude models
8
+
3
9
  ## 3.0.0-beta.10
4
10
 
5
11
  ### Patch Changes
package/dist/index.js CHANGED
@@ -630,13 +630,27 @@ var BedrockChatLanguageModel = class {
630
630
  setting: "topK"
631
631
  });
632
632
  }
633
- if (responseFormat != null && responseFormat.type !== "text") {
633
+ if (responseFormat != null && responseFormat.type !== "text" && responseFormat.type !== "json") {
634
634
  warnings.push({
635
635
  type: "unsupported-setting",
636
636
  setting: "responseFormat",
637
- details: "JSON response format is not supported."
637
+ details: "Only text and json response formats are supported."
638
638
  });
639
639
  }
640
+ if (tools != null && (responseFormat == null ? void 0 : responseFormat.type) === "json") {
641
+ if (tools.length > 0) {
642
+ warnings.push({
643
+ type: "other",
644
+ message: "JSON response format does not support tools. The provided tools are ignored."
645
+ });
646
+ }
647
+ }
648
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null ? {
649
+ type: "function",
650
+ name: "json",
651
+ description: "Respond with a JSON object.",
652
+ inputSchema: responseFormat.schema
653
+ } : void 0;
640
654
  const { system, messages } = await convertToBedrockChatMessages(prompt);
641
655
  const isThinking = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled";
642
656
  const thinkingBudget = (_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.budgetTokens;
@@ -677,8 +691,8 @@ var BedrockChatLanguageModel = class {
677
691
  });
678
692
  }
679
693
  const { toolConfig, toolWarnings } = prepareTools({
680
- tools,
681
- toolChoice,
694
+ tools: jsonResponseTool != null ? [jsonResponseTool] : tools != null ? tools : [],
695
+ toolChoice: jsonResponseTool != null ? { type: "tool", toolName: jsonResponseTool.name } : toolChoice,
682
696
  prompt
683
697
  });
684
698
  const { reasoningConfig: _, ...filteredBedrockOptions } = (providerOptions == null ? void 0 : providerOptions.bedrock) || {};
@@ -693,12 +707,17 @@ var BedrockChatLanguageModel = class {
693
707
  ...filteredBedrockOptions,
694
708
  ...toolConfig.tools !== void 0 ? { toolConfig } : {}
695
709
  },
696
- warnings: [...warnings, ...toolWarnings]
710
+ warnings: [...warnings, ...toolWarnings],
711
+ usesJsonResponseTool: jsonResponseTool != null
697
712
  };
698
713
  }
699
714
  async doGenerate(options) {
700
715
  var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
701
- const { command: args, warnings } = await this.getArgs(options);
716
+ const {
717
+ command: args,
718
+ warnings,
719
+ usesJsonResponseTool
720
+ } = await this.getArgs(options);
702
721
  const url = `${this.getUrl(this.modelId)}/converse`;
703
722
  const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
704
723
  url,
@@ -723,7 +742,9 @@ var BedrockChatLanguageModel = class {
723
742
  const content = [];
724
743
  for (const part of response.output.message.content) {
725
744
  if (part.text) {
726
- content.push({ type: "text", text: part.text });
745
+ if (!usesJsonResponseTool) {
746
+ content.push({ type: "text", text: part.text });
747
+ }
727
748
  }
728
749
  if (part.reasoningContent) {
729
750
  if ("reasoningText" in part.reasoningContent) {
@@ -752,22 +773,29 @@ var BedrockChatLanguageModel = class {
752
773
  }
753
774
  }
754
775
  if (part.toolUse) {
755
- content.push({
756
- type: "tool-call",
757
- toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
758
- toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
759
- input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
760
- });
776
+ content.push(
777
+ // when a json response tool is used, the tool call becomes the text:
778
+ usesJsonResponseTool ? {
779
+ type: "text",
780
+ text: JSON.stringify(part.toolUse.input)
781
+ } : {
782
+ type: "tool-call",
783
+ toolCallId: (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId(),
784
+ toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
785
+ input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : "")
786
+ }
787
+ );
761
788
  }
762
789
  }
763
- const providerMetadata = response.trace || response.usage ? {
790
+ const providerMetadata = response.trace || response.usage || usesJsonResponseTool ? {
764
791
  bedrock: {
765
792
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
766
793
  ...((_h = response.usage) == null ? void 0 : _h.cacheWriteInputTokens) != null && {
767
794
  usage: {
768
795
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
769
796
  }
770
- }
797
+ },
798
+ ...usesJsonResponseTool && { isJsonResponseFromTool: true }
771
799
  }
772
800
  } : void 0;
773
801
  return {
@@ -790,7 +818,11 @@ var BedrockChatLanguageModel = class {
790
818
  };
791
819
  }
792
820
  async doStream(options) {
793
- const { command: args, warnings } = await this.getArgs(options);
821
+ const {
822
+ command: args,
823
+ warnings,
824
+ usesJsonResponseTool
825
+ } = await this.getArgs(options);
794
826
  const url = `${this.getUrl(this.modelId)}/converse-stream`;
795
827
  const { value: response, responseHeaders } = await (0, import_provider_utils3.postJsonToApi)({
796
828
  url,
@@ -869,11 +901,14 @@ var BedrockChatLanguageModel = class {
869
901
  const trace = value.metadata.trace ? {
870
902
  trace: value.metadata.trace
871
903
  } : void 0;
872
- if (cacheUsage || trace) {
904
+ if (cacheUsage || trace || usesJsonResponseTool) {
873
905
  providerMetadata = {
874
906
  bedrock: {
875
907
  ...cacheUsage,
876
- ...trace
908
+ ...trace,
909
+ ...usesJsonResponseTool && {
910
+ isJsonResponseFromTool: true
911
+ }
877
912
  }
878
913
  };
879
914
  }
@@ -890,16 +925,20 @@ var BedrockChatLanguageModel = class {
890
925
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
891
926
  if (contentBlocks[blockIndex] == null) {
892
927
  contentBlocks[blockIndex] = { type: "text" };
928
+ if (!usesJsonResponseTool) {
929
+ controller.enqueue({
930
+ type: "text-start",
931
+ id: String(blockIndex)
932
+ });
933
+ }
934
+ }
935
+ if (!usesJsonResponseTool) {
893
936
  controller.enqueue({
894
- type: "text-start",
895
- id: String(blockIndex)
937
+ type: "text-delta",
938
+ id: String(blockIndex),
939
+ delta: value.contentBlockDelta.delta.text
896
940
  });
897
941
  }
898
- controller.enqueue({
899
- type: "text-delta",
900
- id: String(blockIndex),
901
- delta: value.contentBlockDelta.delta.text
902
- });
903
942
  }
904
943
  if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
905
944
  const blockIndex = value.contentBlockStop.contentBlockIndex;
@@ -911,21 +950,39 @@ var BedrockChatLanguageModel = class {
911
950
  id: String(blockIndex)
912
951
  });
913
952
  } else if (contentBlock.type === "text") {
914
- controller.enqueue({
915
- type: "text-end",
916
- id: String(blockIndex)
917
- });
953
+ if (!usesJsonResponseTool) {
954
+ controller.enqueue({
955
+ type: "text-end",
956
+ id: String(blockIndex)
957
+ });
958
+ }
918
959
  } else if (contentBlock.type === "tool-call") {
919
- controller.enqueue({
920
- type: "tool-input-end",
921
- id: contentBlock.toolCallId
922
- });
923
- controller.enqueue({
924
- type: "tool-call",
925
- toolCallId: contentBlock.toolCallId,
926
- toolName: contentBlock.toolName,
927
- input: contentBlock.jsonText
928
- });
960
+ if (usesJsonResponseTool) {
961
+ controller.enqueue({
962
+ type: "text-start",
963
+ id: String(blockIndex)
964
+ });
965
+ controller.enqueue({
966
+ type: "text-delta",
967
+ id: String(blockIndex),
968
+ delta: contentBlock.jsonText
969
+ });
970
+ controller.enqueue({
971
+ type: "text-end",
972
+ id: String(blockIndex)
973
+ });
974
+ } else {
975
+ controller.enqueue({
976
+ type: "tool-input-end",
977
+ id: contentBlock.toolCallId
978
+ });
979
+ controller.enqueue({
980
+ type: "tool-call",
981
+ toolCallId: contentBlock.toolCallId,
982
+ toolName: contentBlock.toolName,
983
+ input: contentBlock.jsonText
984
+ });
985
+ }
929
986
  }
930
987
  delete contentBlocks[blockIndex];
931
988
  }
@@ -980,11 +1037,13 @@ var BedrockChatLanguageModel = class {
980
1037
  toolName: toolUse.name,
981
1038
  jsonText: ""
982
1039
  };
983
- controller.enqueue({
984
- type: "tool-input-start",
985
- id: toolUse.toolUseId,
986
- toolName: toolUse.name
987
- });
1040
+ if (!usesJsonResponseTool) {
1041
+ controller.enqueue({
1042
+ type: "tool-input-start",
1043
+ id: toolUse.toolUseId,
1044
+ toolName: toolUse.name
1045
+ });
1046
+ }
988
1047
  }
989
1048
  const contentBlockDelta = value.contentBlockDelta;
990
1049
  if ((contentBlockDelta == null ? void 0 : contentBlockDelta.delta) && "toolUse" in contentBlockDelta.delta && contentBlockDelta.delta.toolUse) {
@@ -992,11 +1051,13 @@ var BedrockChatLanguageModel = class {
992
1051
  const contentBlock = contentBlocks[blockIndex];
993
1052
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
994
1053
  const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
995
- controller.enqueue({
996
- type: "tool-input-delta",
997
- id: contentBlock.toolCallId,
998
- delta
999
- });
1054
+ if (!usesJsonResponseTool) {
1055
+ controller.enqueue({
1056
+ type: "tool-input-delta",
1057
+ id: contentBlock.toolCallId,
1058
+ delta
1059
+ });
1060
+ }
1000
1061
  contentBlock.jsonText += delta;
1001
1062
  }
1002
1063
  }