@ai-sdk/amazon-bedrock 4.0.134 → 4.0.136

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,16 +1197,17 @@ 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
1191
1208
  },
1192
1209
  warnings,
1210
+ request: { body: args },
1193
1211
  ...providerMetadata && { providerMetadata }
1194
1212
  };
1195
1213
  }
@@ -1197,6 +1215,7 @@ var BedrockChatLanguageModel = class {
1197
1215
  const {
1198
1216
  command: args,
1199
1217
  warnings,
1218
+ usesJsonInstruction,
1200
1219
  usesJsonResponseTool
1201
1220
  } = await this.getArgs(options);
1202
1221
  const modelId = this.modelId;
@@ -1222,6 +1241,7 @@ var BedrockChatLanguageModel = class {
1222
1241
  let providerMetadata = void 0;
1223
1242
  let isJsonResponseFromTool = false;
1224
1243
  let stopSequence = null;
1244
+ const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
1225
1245
  const contentBlocks = {};
1226
1246
  return {
1227
1247
  stream: response.pipeThrough(
@@ -1237,7 +1257,7 @@ var BedrockChatLanguageModel = class {
1237
1257
  });
1238
1258
  },
1239
1259
  transform(chunk, controller) {
1240
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1260
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1241
1261
  function enqueueError(bedrockError) {
1242
1262
  finishReason = { unified: "error", raw: void 0 };
1243
1263
  controller.enqueue({ type: "error", error: bedrockError });
@@ -1325,13 +1345,18 @@ var BedrockChatLanguageModel = class {
1325
1345
  id: String(blockIndex)
1326
1346
  });
1327
1347
  }
1328
- controller.enqueue({
1329
- type: "text-delta",
1330
- id: String(blockIndex),
1331
- delta: value.contentBlockDelta.delta.text
1332
- });
1348
+ const textDelta = (_m = jsonObjectTextExtractor == null ? void 0 : jsonObjectTextExtractor.process(
1349
+ value.contentBlockDelta.delta.text
1350
+ )) != null ? _m : value.contentBlockDelta.delta.text;
1351
+ if (textDelta.length > 0) {
1352
+ controller.enqueue({
1353
+ type: "text-delta",
1354
+ id: String(blockIndex),
1355
+ delta: textDelta
1356
+ });
1357
+ }
1333
1358
  }
1334
- if (((_m = value.contentBlockStop) == null ? void 0 : _m.contentBlockIndex) != null) {
1359
+ if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1335
1360
  const blockIndex = value.contentBlockStop.contentBlockIndex;
1336
1361
  const contentBlock = contentBlocks[blockIndex];
1337
1362
  if (contentBlock != null) {
@@ -1377,7 +1402,7 @@ var BedrockChatLanguageModel = class {
1377
1402
  delete contentBlocks[blockIndex];
1378
1403
  }
1379
1404
  }
1380
- if (((_n = value.contentBlockDelta) == null ? void 0 : _n.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1405
+ if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1381
1406
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1382
1407
  const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
1383
1408
  if ("text" in reasoningContent && reasoningContent.text) {
@@ -1432,7 +1457,7 @@ var BedrockChatLanguageModel = class {
1432
1457
  }
1433
1458
  }
1434
1459
  const contentBlockStart = value.contentBlockStart;
1435
- if (((_o = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _o.toolUse) != null) {
1460
+ if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1436
1461
  const toolUse = contentBlockStart.start.toolUse;
1437
1462
  const blockIndex = contentBlockStart.contentBlockIndex;
1438
1463
  const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
@@ -1460,7 +1485,7 @@ var BedrockChatLanguageModel = class {
1460
1485
  const blockIndex = contentBlockDelta.contentBlockIndex;
1461
1486
  const contentBlock = contentBlocks[blockIndex];
1462
1487
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1463
- const delta = (_p = contentBlockDelta.delta.toolUse.input) != null ? _p : "";
1488
+ const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1464
1489
  if (!contentBlock.isJsonResponseTool) {
1465
1490
  controller.enqueue({
1466
1491
  type: "tool-input-delta",
@@ -1502,7 +1527,7 @@ var BedrockChatLanguageModel = class {
1502
1527
  }
1503
1528
  })
1504
1529
  ),
1505
- // TODO request?
1530
+ request: { body: args },
1506
1531
  response: { headers: responseHeaders }
1507
1532
  };
1508
1533
  }
@@ -1511,6 +1536,60 @@ var BedrockChatLanguageModel = class {
1511
1536
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
1512
1537
  }
1513
1538
  };
1539
+ function bedrockChatModelSupportsStructuredOutput(modelId) {
1540
+ 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");
1541
+ }
1542
+ var JsonObjectTextExtractor = class {
1543
+ constructor() {
1544
+ this.started = false;
1545
+ this.completed = false;
1546
+ this.depth = 0;
1547
+ this.inString = false;
1548
+ this.escaped = false;
1549
+ }
1550
+ process(text) {
1551
+ let result = "";
1552
+ for (const character of text) {
1553
+ if (this.completed) {
1554
+ break;
1555
+ }
1556
+ if (!this.started) {
1557
+ if (character !== "{") {
1558
+ continue;
1559
+ }
1560
+ this.started = true;
1561
+ this.depth = 1;
1562
+ result += character;
1563
+ continue;
1564
+ }
1565
+ result += character;
1566
+ if (this.escaped) {
1567
+ this.escaped = false;
1568
+ continue;
1569
+ }
1570
+ if (character === "\\" && this.inString) {
1571
+ this.escaped = true;
1572
+ continue;
1573
+ }
1574
+ if (character === '"') {
1575
+ this.inString = !this.inString;
1576
+ continue;
1577
+ }
1578
+ if (this.inString) {
1579
+ continue;
1580
+ }
1581
+ if (character === "{") {
1582
+ this.depth++;
1583
+ } else if (character === "}") {
1584
+ this.depth--;
1585
+ if (this.depth === 0) {
1586
+ this.completed = true;
1587
+ }
1588
+ }
1589
+ }
1590
+ return result;
1591
+ }
1592
+ };
1514
1593
  var BedrockStopReasonSchema = z4.union([
1515
1594
  z4.enum(BEDROCK_STOP_REASONS),
1516
1595
  z4.string()
@@ -2044,7 +2123,7 @@ import {
2044
2123
  import { AwsV4Signer } from "aws4fetch";
2045
2124
 
2046
2125
  // src/version.ts
2047
- var VERSION = true ? "4.0.134" : "0.0.0-test";
2126
+ var VERSION = true ? "4.0.136" : "0.0.0-test";
2048
2127
 
2049
2128
  // src/bedrock-sigv4-fetch.ts
2050
2129
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {