@ai-sdk/amazon-bedrock 4.0.133 → 4.0.135

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
@@ -13,6 +13,7 @@ import {
13
13
  combineHeaders,
14
14
  createJsonErrorResponseHandler,
15
15
  createJsonResponseHandler,
16
+ injectJsonInstructionIntoMessages,
16
17
  parseProviderOptions as parseProviderOptions2,
17
18
  postJsonToApi,
18
19
  resolve
@@ -865,8 +866,11 @@ var BedrockChatLanguageModel = class {
865
866
  }
866
867
  const isAnthropicModel = this.modelId.includes("anthropic");
867
868
  const isThinkingEnabled = ((_b = bedrockOptions.reasoningConfig) == null ? void 0 : _b.type) === "enabled" || ((_c = bedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "adaptive";
868
- const useNativeStructuredOutput = isAnthropicModel && isThinkingEnabled && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
869
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useNativeStructuredOutput ? {
869
+ const modelRejectsNativeStructuredOutput = this.modelId.includes("claude-opus-4-7") || this.modelId.includes("claude-opus-4-8") || this.modelId.includes("claude-fable-5") || this.modelId.includes("claude-sonnet-5");
870
+ const modelSupportsStructuredOutput = bedrockChatModelSupportsStructuredOutput(this.modelId);
871
+ const useNativeStructuredOutput = isAnthropicModel && !modelRejectsNativeStructuredOutput && (modelSupportsStructuredOutput || isThinkingEnabled) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
872
+ const useJsonInstructionForStructuredOutput = isAnthropicModel && (this.modelId.includes("claude-opus-4-7") || this.modelId.includes("claude-opus-4-8")) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && tools != null && tools.length > 0;
873
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useNativeStructuredOutput && !useJsonInstructionForStructuredOutput ? {
870
874
  type: "function",
871
875
  name: "json",
872
876
  description: "Respond with a JSON object.",
@@ -1031,6 +1035,13 @@ var BedrockChatLanguageModel = class {
1031
1035
  });
1032
1036
  }
1033
1037
  }
1038
+ if (useJsonInstructionForStructuredOutput) {
1039
+ filteredPrompt = injectJsonInstructionIntoMessages({
1040
+ messages: filteredPrompt,
1041
+ schema: responseFormat.schema,
1042
+ schemaSuffix: "You MUST answer with only a JSON object that matches the JSON schema above. Do not wrap it in markdown fences or include any other text."
1043
+ });
1044
+ }
1034
1045
  const isMistral = isMistralModel(this.modelId);
1035
1046
  const { system, messages } = await convertToBedrockChatMessages(
1036
1047
  filteredPrompt,
@@ -1063,6 +1074,7 @@ var BedrockChatLanguageModel = class {
1063
1074
  ...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
1064
1075
  },
1065
1076
  warnings,
1077
+ usesJsonInstruction: useJsonInstructionForStructuredOutput,
1066
1078
  usesJsonResponseTool: jsonResponseTool != null,
1067
1079
  betas
1068
1080
  };
@@ -1073,10 +1085,11 @@ var BedrockChatLanguageModel = class {
1073
1085
  return combineHeaders(await resolve(this.config.headers), headers);
1074
1086
  }
1075
1087
  async doGenerate(options) {
1076
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1088
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1077
1089
  const {
1078
1090
  command: args,
1079
1091
  warnings,
1092
+ usesJsonInstruction,
1080
1093
  usesJsonResponseTool
1081
1094
  } = await this.getArgs(options);
1082
1095
  const url = `${this.getUrl(this.modelId)}/converse`;
@@ -1099,9 +1112,13 @@ var BedrockChatLanguageModel = class {
1099
1112
  });
1100
1113
  const content = [];
1101
1114
  let isJsonResponseFromTool = false;
1115
+ const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
1102
1116
  for (const part of response.output.message.content) {
1103
1117
  if (part.text != null) {
1104
- content.push({ type: "text", text: part.text });
1118
+ content.push({
1119
+ type: "text",
1120
+ text: (_a = jsonObjectTextExtractor == null ? void 0 : jsonObjectTextExtractor.process(part.text)) != null ? _a : part.text
1121
+ });
1105
1122
  }
1106
1123
  if (part.reasoningContent) {
1107
1124
  if ("reasoningText" in part.reasoningContent) {
@@ -1123,7 +1140,7 @@ var BedrockChatLanguageModel = class {
1123
1140
  text: "",
1124
1141
  providerMetadata: {
1125
1142
  bedrock: {
1126
- redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
1143
+ redactedData: (_b = part.reasoningContent.redactedReasoning.data) != null ? _b : ""
1127
1144
  }
1128
1145
  }
1129
1146
  });
@@ -1139,17 +1156,17 @@ var BedrockChatLanguageModel = class {
1139
1156
  });
1140
1157
  } else {
1141
1158
  const isMistral = isMistralModel(this.modelId);
1142
- const rawToolCallId = (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId();
1159
+ const rawToolCallId = (_d = (_c = part.toolUse) == null ? void 0 : _c.toolUseId) != null ? _d : this.config.generateId();
1143
1160
  content.push({
1144
1161
  type: "tool-call",
1145
1162
  toolCallId: normalizeToolCallId(rawToolCallId, isMistral),
1146
- toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
1147
- input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : {})
1163
+ toolName: (_f = (_e = part.toolUse) == null ? void 0 : _e.name) != null ? _f : `tool-${this.config.generateId()}`,
1164
+ input: JSON.stringify((_h = (_g = part.toolUse) == null ? void 0 : _g.input) != null ? _h : {})
1148
1165
  });
1149
1166
  }
1150
1167
  }
1151
1168
  }
1152
- const stopSequence = (_j = (_i = (_h = response.additionalModelResponseFields) == null ? void 0 : _h.delta) == null ? void 0 : _i.stop_sequence) != null ? _j : null;
1169
+ const stopSequence = (_k = (_j = (_i = response.additionalModelResponseFields) == null ? void 0 : _i.delta) == null ? void 0 : _j.stop_sequence) != null ? _k : null;
1153
1170
  const providerMetadata = response.trace || response.usage || response.performanceConfig || response.serviceTier || isJsonResponseFromTool || stopSequence ? {
1154
1171
  bedrock: {
1155
1172
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
@@ -1159,7 +1176,7 @@ var BedrockChatLanguageModel = class {
1159
1176
  ...response.serviceTier && {
1160
1177
  serviceTier: response.serviceTier
1161
1178
  },
1162
- ...(((_k = response.usage) == null ? void 0 : _k.cacheWriteInputTokens) != null || ((_l = response.usage) == null ? void 0 : _l.cacheDetails) != null) && {
1179
+ ...(((_l = response.usage) == null ? void 0 : _l.cacheWriteInputTokens) != null || ((_m = response.usage) == null ? void 0 : _m.cacheDetails) != null) && {
1163
1180
  usage: {
1164
1181
  ...response.usage.cacheWriteInputTokens != null && {
1165
1182
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
@@ -1180,11 +1197,11 @@ var BedrockChatLanguageModel = class {
1180
1197
  response.stopReason,
1181
1198
  isJsonResponseFromTool
1182
1199
  ),
1183
- raw: (_m = response.stopReason) != null ? _m : void 0
1200
+ raw: (_n = response.stopReason) != null ? _n : void 0
1184
1201
  },
1185
1202
  usage: convertBedrockUsage(response.usage),
1186
1203
  response: {
1187
- id: (_n = responseHeaders == null ? void 0 : responseHeaders["x-amzn-requestid"]) != null ? _n : void 0,
1204
+ id: (_o = responseHeaders == null ? void 0 : responseHeaders["x-amzn-requestid"]) != null ? _o : void 0,
1188
1205
  timestamp: (responseHeaders == null ? void 0 : responseHeaders["date"]) != null ? new Date(responseHeaders["date"]) : void 0,
1189
1206
  modelId: this.modelId,
1190
1207
  headers: responseHeaders
@@ -1197,6 +1214,7 @@ var BedrockChatLanguageModel = class {
1197
1214
  const {
1198
1215
  command: args,
1199
1216
  warnings,
1217
+ usesJsonInstruction,
1200
1218
  usesJsonResponseTool
1201
1219
  } = await this.getArgs(options);
1202
1220
  const modelId = this.modelId;
@@ -1222,6 +1240,7 @@ var BedrockChatLanguageModel = class {
1222
1240
  let providerMetadata = void 0;
1223
1241
  let isJsonResponseFromTool = false;
1224
1242
  let stopSequence = null;
1243
+ const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
1225
1244
  const contentBlocks = {};
1226
1245
  return {
1227
1246
  stream: response.pipeThrough(
@@ -1237,7 +1256,7 @@ var BedrockChatLanguageModel = class {
1237
1256
  });
1238
1257
  },
1239
1258
  transform(chunk, controller) {
1240
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1259
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1241
1260
  function enqueueError(bedrockError) {
1242
1261
  finishReason = { unified: "error", raw: void 0 };
1243
1262
  controller.enqueue({ type: "error", error: bedrockError });
@@ -1325,13 +1344,18 @@ var BedrockChatLanguageModel = class {
1325
1344
  id: String(blockIndex)
1326
1345
  });
1327
1346
  }
1328
- controller.enqueue({
1329
- type: "text-delta",
1330
- id: String(blockIndex),
1331
- delta: value.contentBlockDelta.delta.text
1332
- });
1347
+ const textDelta = (_m = jsonObjectTextExtractor == null ? void 0 : jsonObjectTextExtractor.process(
1348
+ value.contentBlockDelta.delta.text
1349
+ )) != null ? _m : value.contentBlockDelta.delta.text;
1350
+ if (textDelta.length > 0) {
1351
+ controller.enqueue({
1352
+ type: "text-delta",
1353
+ id: String(blockIndex),
1354
+ delta: textDelta
1355
+ });
1356
+ }
1333
1357
  }
1334
- if (((_m = value.contentBlockStop) == null ? void 0 : _m.contentBlockIndex) != null) {
1358
+ if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1335
1359
  const blockIndex = value.contentBlockStop.contentBlockIndex;
1336
1360
  const contentBlock = contentBlocks[blockIndex];
1337
1361
  if (contentBlock != null) {
@@ -1377,7 +1401,7 @@ var BedrockChatLanguageModel = class {
1377
1401
  delete contentBlocks[blockIndex];
1378
1402
  }
1379
1403
  }
1380
- if (((_n = value.contentBlockDelta) == null ? void 0 : _n.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1404
+ if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1381
1405
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1382
1406
  const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
1383
1407
  if ("text" in reasoningContent && reasoningContent.text) {
@@ -1432,7 +1456,7 @@ var BedrockChatLanguageModel = class {
1432
1456
  }
1433
1457
  }
1434
1458
  const contentBlockStart = value.contentBlockStart;
1435
- if (((_o = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _o.toolUse) != null) {
1459
+ if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1436
1460
  const toolUse = contentBlockStart.start.toolUse;
1437
1461
  const blockIndex = contentBlockStart.contentBlockIndex;
1438
1462
  const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
@@ -1460,7 +1484,7 @@ var BedrockChatLanguageModel = class {
1460
1484
  const blockIndex = contentBlockDelta.contentBlockIndex;
1461
1485
  const contentBlock = contentBlocks[blockIndex];
1462
1486
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1463
- const delta = (_p = contentBlockDelta.delta.toolUse.input) != null ? _p : "";
1487
+ const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1464
1488
  if (!contentBlock.isJsonResponseTool) {
1465
1489
  controller.enqueue({
1466
1490
  type: "tool-input-delta",
@@ -1511,6 +1535,60 @@ var BedrockChatLanguageModel = class {
1511
1535
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
1512
1536
  }
1513
1537
  };
1538
+ function bedrockChatModelSupportsStructuredOutput(modelId) {
1539
+ return modelId.includes("claude-opus-4-8") || modelId.includes("claude-opus-4-7") || modelId.includes("claude-fable-5") || modelId.includes("claude-sonnet-5") || modelId.includes("claude-sonnet-4-6") || modelId.includes("claude-opus-4-6") || modelId.includes("claude-sonnet-4-5") || modelId.includes("claude-opus-4-5") || modelId.includes("claude-haiku-4-5") || modelId.includes("claude-opus-4-1");
1540
+ }
1541
+ var JsonObjectTextExtractor = class {
1542
+ constructor() {
1543
+ this.started = false;
1544
+ this.completed = false;
1545
+ this.depth = 0;
1546
+ this.inString = false;
1547
+ this.escaped = false;
1548
+ }
1549
+ process(text) {
1550
+ let result = "";
1551
+ for (const character of text) {
1552
+ if (this.completed) {
1553
+ break;
1554
+ }
1555
+ if (!this.started) {
1556
+ if (character !== "{") {
1557
+ continue;
1558
+ }
1559
+ this.started = true;
1560
+ this.depth = 1;
1561
+ result += character;
1562
+ continue;
1563
+ }
1564
+ result += character;
1565
+ if (this.escaped) {
1566
+ this.escaped = false;
1567
+ continue;
1568
+ }
1569
+ if (character === "\\" && this.inString) {
1570
+ this.escaped = true;
1571
+ continue;
1572
+ }
1573
+ if (character === '"') {
1574
+ this.inString = !this.inString;
1575
+ continue;
1576
+ }
1577
+ if (this.inString) {
1578
+ continue;
1579
+ }
1580
+ if (character === "{") {
1581
+ this.depth++;
1582
+ } else if (character === "}") {
1583
+ this.depth--;
1584
+ if (this.depth === 0) {
1585
+ this.completed = true;
1586
+ }
1587
+ }
1588
+ }
1589
+ return result;
1590
+ }
1591
+ };
1514
1592
  var BedrockStopReasonSchema = z4.union([
1515
1593
  z4.enum(BEDROCK_STOP_REASONS),
1516
1594
  z4.string()
@@ -2044,7 +2122,7 @@ import {
2044
2122
  import { AwsV4Signer } from "aws4fetch";
2045
2123
 
2046
2124
  // src/version.ts
2047
- var VERSION = true ? "4.0.133" : "0.0.0-test";
2125
+ var VERSION = true ? "4.0.135" : "0.0.0-test";
2048
2126
 
2049
2127
  // src/bedrock-sigv4-fetch.ts
2050
2128
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {