@ai-sdk/amazon-bedrock 5.0.20 → 5.0.22

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,21 @@
1
1
  # @ai-sdk/amazon-bedrock
2
2
 
3
+ ## 5.0.22
4
+
5
+ ### Patch Changes
6
+
7
+ - 43fc411: Return Bedrock Converse request bodies from language model generation and streaming calls.
8
+
9
+ ## 5.0.21
10
+
11
+ ### Patch Changes
12
+
13
+ - b0e9d24: Avoid unreliable synthetic response tools when structured output is combined with tools on Bedrock Claude Opus 4.7 and 4.8.
14
+ - Updated dependencies [31c7be8]
15
+ - @ai-sdk/provider-utils@5.0.10
16
+ - @ai-sdk/anthropic@4.0.15
17
+ - @ai-sdk/openai@4.0.14
18
+
3
19
  ## 5.0.20
4
20
 
5
21
  ### Patch Changes
@@ -24,7 +24,7 @@ import {
24
24
  import { AwsV4Signer } from "aws4fetch";
25
25
 
26
26
  // src/version.ts
27
- var VERSION = true ? "5.0.20" : "0.0.0-test";
27
+ var VERSION = true ? "5.0.22" : "0.0.0-test";
28
28
 
29
29
  // src/amazon-bedrock-sigv4-fetch.ts
30
30
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {
package/dist/index.js CHANGED
@@ -13,6 +13,7 @@ import {
13
13
  combineHeaders,
14
14
  createJsonErrorResponseHandler,
15
15
  createJsonResponseHandler,
16
+ injectJsonInstructionIntoMessages,
16
17
  isCustomReasoning,
17
18
  mapReasoningToProviderBudget,
18
19
  mapReasoningToProviderEffort,
@@ -929,8 +930,10 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
929
930
  });
930
931
  const isThinkingEnabled = ((_c = amazonBedrockOptions.reasoningConfig) == null ? void 0 : _c.type) === "enabled" || ((_d = amazonBedrockOptions.reasoningConfig) == null ? void 0 : _d.type) === "adaptive";
931
932
  const { supportsStructuredOutput: modelSupportsStructuredOutput } = getModelCapabilities(this.modelId);
932
- const useNativeStructuredOutput = isAnthropicModel && (modelSupportsStructuredOutput || isThinkingEnabled) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
933
- const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useNativeStructuredOutput ? {
933
+ 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");
934
+ const useNativeStructuredOutput = isAnthropicModel && !modelRejectsNativeStructuredOutput && (modelSupportsStructuredOutput || isThinkingEnabled) && (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null;
935
+ 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;
936
+ const jsonResponseTool = (responseFormat == null ? void 0 : responseFormat.type) === "json" && responseFormat.schema != null && !useNativeStructuredOutput && !useJsonInstructionForStructuredOutput ? {
934
937
  type: "function",
935
938
  name: "json",
936
939
  description: "Respond with a JSON object.",
@@ -1094,6 +1097,13 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1094
1097
  });
1095
1098
  }
1096
1099
  }
1100
+ if (useJsonInstructionForStructuredOutput) {
1101
+ filteredPrompt = injectJsonInstructionIntoMessages({
1102
+ messages: filteredPrompt,
1103
+ schema: responseFormat.schema,
1104
+ 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."
1105
+ });
1106
+ }
1097
1107
  const isMistral = isMistralModel(this.modelId);
1098
1108
  const { system, messages } = await convertToAmazonBedrockChatMessages(
1099
1109
  filteredPrompt,
@@ -1126,6 +1136,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1126
1136
  ...toolConfig.tools !== void 0 && toolConfig.tools.length > 0 ? { toolConfig } : {}
1127
1137
  },
1128
1138
  warnings,
1139
+ usesJsonInstruction: useJsonInstructionForStructuredOutput,
1129
1140
  usesJsonResponseTool: jsonResponseTool != null,
1130
1141
  betas
1131
1142
  };
@@ -1139,10 +1150,11 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1139
1150
  );
1140
1151
  }
1141
1152
  async doGenerate(options) {
1142
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n;
1153
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o;
1143
1154
  const {
1144
1155
  command: args,
1145
1156
  warnings,
1157
+ usesJsonInstruction,
1146
1158
  usesJsonResponseTool
1147
1159
  } = await this.getArgs(options);
1148
1160
  const url = `${this.getUrl(this.modelId)}/converse`;
@@ -1165,9 +1177,13 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1165
1177
  });
1166
1178
  const content = [];
1167
1179
  let isJsonResponseFromTool = false;
1180
+ const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
1168
1181
  for (const part of response.output.message.content) {
1169
1182
  if (part.text != null) {
1170
- content.push({ type: "text", text: part.text });
1183
+ content.push({
1184
+ type: "text",
1185
+ text: (_a = jsonObjectTextExtractor == null ? void 0 : jsonObjectTextExtractor.process(part.text)) != null ? _a : part.text
1186
+ });
1171
1187
  }
1172
1188
  if (part.reasoningContent) {
1173
1189
  if ("reasoningText" in part.reasoningContent) {
@@ -1187,7 +1203,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1187
1203
  content.push(reasoning);
1188
1204
  } else if ("redactedReasoning" in part.reasoningContent) {
1189
1205
  const redactedPayload = {
1190
- redactedData: (_a = part.reasoningContent.redactedReasoning.data) != null ? _a : ""
1206
+ redactedData: (_b = part.reasoningContent.redactedReasoning.data) != null ? _b : ""
1191
1207
  };
1192
1208
  content.push({
1193
1209
  type: "reasoning",
@@ -1209,17 +1225,17 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1209
1225
  });
1210
1226
  } else {
1211
1227
  const isMistral = isMistralModel(this.modelId);
1212
- const rawToolCallId = (_c = (_b = part.toolUse) == null ? void 0 : _b.toolUseId) != null ? _c : this.config.generateId();
1228
+ const rawToolCallId = (_d = (_c = part.toolUse) == null ? void 0 : _c.toolUseId) != null ? _d : this.config.generateId();
1213
1229
  content.push({
1214
1230
  type: "tool-call",
1215
1231
  toolCallId: normalizeToolCallId(rawToolCallId, isMistral),
1216
- toolName: (_e = (_d = part.toolUse) == null ? void 0 : _d.name) != null ? _e : `tool-${this.config.generateId()}`,
1217
- input: JSON.stringify((_g = (_f = part.toolUse) == null ? void 0 : _f.input) != null ? _g : {})
1232
+ toolName: (_f = (_e = part.toolUse) == null ? void 0 : _e.name) != null ? _f : `tool-${this.config.generateId()}`,
1233
+ input: JSON.stringify((_h = (_g = part.toolUse) == null ? void 0 : _g.input) != null ? _h : {})
1218
1234
  });
1219
1235
  }
1220
1236
  }
1221
1237
  }
1222
- const stopSequence = (_j = (_i = (_h = response.additionalModelResponseFields) == null ? void 0 : _h.delta) == null ? void 0 : _i.stop_sequence) != null ? _j : null;
1238
+ const stopSequence = (_k = (_j = (_i = response.additionalModelResponseFields) == null ? void 0 : _i.delta) == null ? void 0 : _j.stop_sequence) != null ? _k : null;
1223
1239
  const providerMetadataPayload = response.trace || response.usage || response.performanceConfig || response.serviceTier || isJsonResponseFromTool || stopSequence ? {
1224
1240
  ...response.trace && typeof response.trace === "object" ? { trace: response.trace } : {},
1225
1241
  ...response.performanceConfig && {
@@ -1228,7 +1244,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1228
1244
  ...response.serviceTier && {
1229
1245
  serviceTier: response.serviceTier
1230
1246
  },
1231
- ...(((_k = response.usage) == null ? void 0 : _k.cacheWriteInputTokens) != null || ((_l = response.usage) == null ? void 0 : _l.cacheDetails) != null) && {
1247
+ ...(((_l = response.usage) == null ? void 0 : _l.cacheWriteInputTokens) != null || ((_m = response.usage) == null ? void 0 : _m.cacheDetails) != null) && {
1232
1248
  usage: {
1233
1249
  ...response.usage.cacheWriteInputTokens != null && {
1234
1250
  cacheWriteInputTokens: response.usage.cacheWriteInputTokens
@@ -1252,16 +1268,17 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1252
1268
  response.stopReason,
1253
1269
  isJsonResponseFromTool
1254
1270
  ),
1255
- raw: (_m = response.stopReason) != null ? _m : void 0
1271
+ raw: (_n = response.stopReason) != null ? _n : void 0
1256
1272
  },
1257
1273
  usage: convertAmazonBedrockUsage(response.usage),
1258
1274
  response: {
1259
- id: (_n = responseHeaders == null ? void 0 : responseHeaders["x-amzn-requestid"]) != null ? _n : void 0,
1275
+ id: (_o = responseHeaders == null ? void 0 : responseHeaders["x-amzn-requestid"]) != null ? _o : void 0,
1260
1276
  timestamp: (responseHeaders == null ? void 0 : responseHeaders["date"]) != null ? new Date(responseHeaders["date"]) : void 0,
1261
1277
  modelId: this.modelId,
1262
1278
  headers: responseHeaders
1263
1279
  },
1264
1280
  warnings,
1281
+ request: { body: args },
1265
1282
  ...providerMetadata && { providerMetadata }
1266
1283
  };
1267
1284
  }
@@ -1269,6 +1286,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1269
1286
  const {
1270
1287
  command: args,
1271
1288
  warnings,
1289
+ usesJsonInstruction,
1272
1290
  usesJsonResponseTool
1273
1291
  } = await this.getArgs(options);
1274
1292
  const modelId = this.modelId;
@@ -1296,6 +1314,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1296
1314
  let providerMetadata = void 0;
1297
1315
  let isJsonResponseFromTool = false;
1298
1316
  let stopSequence = null;
1317
+ const jsonObjectTextExtractor = usesJsonInstruction ? new JsonObjectTextExtractor() : void 0;
1299
1318
  const contentBlocks = {};
1300
1319
  return {
1301
1320
  stream: response.pipeThrough(
@@ -1311,7 +1330,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1311
1330
  });
1312
1331
  },
1313
1332
  transform(chunk, controller) {
1314
- var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p;
1333
+ var _a, _b, _c, _d, _e, _f, _g, _h, _i, _j, _k, _l, _m, _n, _o, _p, _q;
1315
1334
  function enqueueError(amazonBedrockError) {
1316
1335
  finishReason = { unified: "error", raw: void 0 };
1317
1336
  controller.enqueue({ type: "error", error: amazonBedrockError });
@@ -1401,13 +1420,18 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1401
1420
  id: String(blockIndex)
1402
1421
  });
1403
1422
  }
1404
- controller.enqueue({
1405
- type: "text-delta",
1406
- id: String(blockIndex),
1407
- delta: value.contentBlockDelta.delta.text
1408
- });
1423
+ const textDelta = (_m = jsonObjectTextExtractor == null ? void 0 : jsonObjectTextExtractor.process(
1424
+ value.contentBlockDelta.delta.text
1425
+ )) != null ? _m : value.contentBlockDelta.delta.text;
1426
+ if (textDelta.length > 0) {
1427
+ controller.enqueue({
1428
+ type: "text-delta",
1429
+ id: String(blockIndex),
1430
+ delta: textDelta
1431
+ });
1432
+ }
1409
1433
  }
1410
- if (((_m = value.contentBlockStop) == null ? void 0 : _m.contentBlockIndex) != null) {
1434
+ if (((_n = value.contentBlockStop) == null ? void 0 : _n.contentBlockIndex) != null) {
1411
1435
  const blockIndex = value.contentBlockStop.contentBlockIndex;
1412
1436
  const contentBlock = contentBlocks[blockIndex];
1413
1437
  if (contentBlock != null) {
@@ -1453,7 +1477,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1453
1477
  delete contentBlocks[blockIndex];
1454
1478
  }
1455
1479
  }
1456
- if (((_n = value.contentBlockDelta) == null ? void 0 : _n.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1480
+ if (((_o = value.contentBlockDelta) == null ? void 0 : _o.delta) && "reasoningContent" in value.contentBlockDelta.delta && value.contentBlockDelta.delta.reasoningContent) {
1457
1481
  const blockIndex = value.contentBlockDelta.contentBlockIndex || 0;
1458
1482
  const reasoningContent = value.contentBlockDelta.delta.reasoningContent;
1459
1483
  if ("text" in reasoningContent && reasoningContent.text) {
@@ -1516,7 +1540,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1516
1540
  }
1517
1541
  }
1518
1542
  const contentBlockStart = value.contentBlockStart;
1519
- if (((_o = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _o.toolUse) != null) {
1543
+ if (((_p = contentBlockStart == null ? void 0 : contentBlockStart.start) == null ? void 0 : _p.toolUse) != null) {
1520
1544
  const toolUse = contentBlockStart.start.toolUse;
1521
1545
  const blockIndex = contentBlockStart.contentBlockIndex;
1522
1546
  const isJsonResponseTool = usesJsonResponseTool && toolUse.name === "json";
@@ -1544,7 +1568,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1544
1568
  const blockIndex = contentBlockDelta.contentBlockIndex;
1545
1569
  const contentBlock = contentBlocks[blockIndex];
1546
1570
  if ((contentBlock == null ? void 0 : contentBlock.type) === "tool-call") {
1547
- const delta = (_p = contentBlockDelta.delta.toolUse.input) != null ? _p : "";
1571
+ const delta = (_q = contentBlockDelta.delta.toolUse.input) != null ? _q : "";
1548
1572
  if (!contentBlock.isJsonResponseTool) {
1549
1573
  controller.enqueue({
1550
1574
  type: "tool-input-delta",
@@ -1581,7 +1605,7 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1581
1605
  }
1582
1606
  })
1583
1607
  ),
1584
- // TODO request?
1608
+ request: { body: args },
1585
1609
  response: { headers: responseHeaders }
1586
1610
  };
1587
1611
  }
@@ -1590,6 +1614,57 @@ var AmazonBedrockChatLanguageModel = class _AmazonBedrockChatLanguageModel {
1590
1614
  return `${this.config.baseUrl()}/model/${encodedModelId}`;
1591
1615
  }
1592
1616
  };
1617
+ var JsonObjectTextExtractor = class {
1618
+ constructor() {
1619
+ this.started = false;
1620
+ this.completed = false;
1621
+ this.depth = 0;
1622
+ this.inString = false;
1623
+ this.escaped = false;
1624
+ }
1625
+ process(text) {
1626
+ let result = "";
1627
+ for (const character of text) {
1628
+ if (this.completed) {
1629
+ break;
1630
+ }
1631
+ if (!this.started) {
1632
+ if (character !== "{") {
1633
+ continue;
1634
+ }
1635
+ this.started = true;
1636
+ this.depth = 1;
1637
+ result += character;
1638
+ continue;
1639
+ }
1640
+ result += character;
1641
+ if (this.escaped) {
1642
+ this.escaped = false;
1643
+ continue;
1644
+ }
1645
+ if (character === "\\" && this.inString) {
1646
+ this.escaped = true;
1647
+ continue;
1648
+ }
1649
+ if (character === '"') {
1650
+ this.inString = !this.inString;
1651
+ continue;
1652
+ }
1653
+ if (this.inString) {
1654
+ continue;
1655
+ }
1656
+ if (character === "{") {
1657
+ this.depth++;
1658
+ } else if (character === "}") {
1659
+ this.depth--;
1660
+ if (this.depth === 0) {
1661
+ this.completed = true;
1662
+ }
1663
+ }
1664
+ }
1665
+ return result;
1666
+ }
1667
+ };
1593
1668
  var AmazonBedrockStopReasonSchema = z4.union([
1594
1669
  z4.enum(BEDROCK_STOP_REASONS),
1595
1670
  z4.string()
@@ -2269,7 +2344,7 @@ import {
2269
2344
  import { AwsV4Signer } from "aws4fetch";
2270
2345
 
2271
2346
  // src/version.ts
2272
- var VERSION = true ? "5.0.20" : "0.0.0-test";
2347
+ var VERSION = true ? "5.0.22" : "0.0.0-test";
2273
2348
 
2274
2349
  // src/amazon-bedrock-sigv4-fetch.ts
2275
2350
  function createSigV4FetchFunction(getCredentials, fetch, service = "bedrock") {