@ai-sdk/amazon-bedrock 4.0.84 → 4.0.86

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ai-sdk/amazon-bedrock",
3
- "version": "4.0.84",
3
+ "version": "4.0.86",
4
4
  "license": "Apache-2.0",
5
5
  "sideEffects": false,
6
6
  "main": "./dist/index.js",
@@ -47,8 +47,8 @@
47
47
  "tsup": "^8.3.0",
48
48
  "typescript": "5.8.3",
49
49
  "zod": "3.25.76",
50
- "@vercel/ai-tsconfig": "0.0.0",
51
- "@ai-sdk/test-server": "1.0.3"
50
+ "@ai-sdk/test-server": "1.0.3",
51
+ "@vercel/ai-tsconfig": "0.0.0"
52
52
  },
53
53
  "peerDependencies": {
54
54
  "zod": "^3.25.76 || ^4.1.8"
@@ -3,14 +3,40 @@ import {
3
3
  FetchFunction,
4
4
  safeParseJSON,
5
5
  } from '@ai-sdk/provider-utils';
6
+ import { z } from 'zod/v4';
6
7
  import { createBedrockEventStreamDecoder } from '../bedrock-event-stream-decoder';
7
8
 
9
+ const bedrockErrorSchema = z.looseObject({
10
+ message: z.string().optional(),
11
+ });
12
+
8
13
  export function createBedrockAnthropicFetch(
9
14
  baseFetch: FetchFunction,
10
15
  ): FetchFunction {
11
16
  return async (url, options) => {
12
17
  const response = await baseFetch(url, options);
13
18
 
19
+ // Transform Bedrock error responses into Anthropic error format
20
+ // so that anthropicFailedResponseHandler can extract the message.
21
+ if (!response.ok) {
22
+ const text = await response.text();
23
+ const parsed = await safeParseJSON({ text, schema: bedrockErrorSchema });
24
+
25
+ const message =
26
+ parsed.success && parsed.value.message ? parsed.value.message : text;
27
+
28
+ const anthropicError = JSON.stringify({
29
+ type: 'error',
30
+ error: { type: 'error', message },
31
+ });
32
+
33
+ return new Response(anthropicError, {
34
+ status: response.status,
35
+ statusText: response.statusText,
36
+ headers: response.headers,
37
+ });
38
+ }
39
+
14
40
  const contentType = response.headers.get('content-type');
15
41
  if (
16
42
  contentType?.includes('application/vnd.amazon.eventstream') &&
@@ -287,33 +287,41 @@ export async function convertToBedrockChatMessages(
287
287
  schema: bedrockReasoningMetadataSchema,
288
288
  });
289
289
 
290
- if (reasoningMetadata != null) {
291
- if (reasoningMetadata.signature != null) {
292
- bedrockContent.push({
293
- reasoningContent: {
294
- reasoningText: {
295
- // trim the last text part if it's the last message in the block
296
- // because Bedrock does not allow trailing whitespace
297
- // in pre-filled assistant responses
298
- text: trimIfLast(
299
- isLastBlock,
300
- isLastMessage,
301
- isLastContentPart,
302
- part.text,
303
- ),
304
- signature: reasoningMetadata.signature,
305
- },
290
+ if (reasoningMetadata?.signature != null) {
291
+ // do not trim reasoning text when a signature is present:
292
+ // the signature validates the exact original bytes
293
+ bedrockContent.push({
294
+ reasoningContent: {
295
+ reasoningText: {
296
+ text: part.text,
297
+ signature: reasoningMetadata.signature,
306
298
  },
307
- });
308
- } else if (reasoningMetadata.redactedData != null) {
309
- bedrockContent.push({
310
- reasoningContent: {
311
- redactedReasoning: {
312
- data: reasoningMetadata.redactedData,
313
- },
299
+ },
300
+ });
301
+ } else if (reasoningMetadata?.redactedData != null) {
302
+ bedrockContent.push({
303
+ reasoningContent: {
304
+ redactedReasoning: {
305
+ data: reasoningMetadata.redactedData,
314
306
  },
315
- });
316
- }
307
+ },
308
+ });
309
+ } else {
310
+ // trim the last text part if it's the last message in the block
311
+ // because Bedrock does not allow trailing whitespace
312
+ // in pre-filled assistant responses
313
+ bedrockContent.push({
314
+ reasoningContent: {
315
+ reasoningText: {
316
+ text: trimIfLast(
317
+ isLastBlock,
318
+ isLastMessage,
319
+ isLastContentPart,
320
+ part.text,
321
+ ),
322
+ },
323
+ },
324
+ });
317
325
  }
318
326
 
319
327
  break;