From 5ebe97bfcc519453ffaae304e97c8b09f56afd94 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Thu, 12 Nov 2026 13:08:15 -0600 Subject: [PATCH 1/3] Send prefix_mismatch_behavior to Bedrock instead of the deprecated alias @ai-sdk/amazon-bedrock renames thinking.block_binding.prefix_mismatch_behavior to mismatch_behavior on the InvokeModel route (vercel/ai#20728). AWS now documents mismatch_behavior as a deprecated alias, and some regions ignore it, so a changed prefix before replayed thinking fails with "The block is bound to a different conversation" despite drop_block. The SDK renames before it signs, so the Anthropic-route fetch now renames the field back and re-signs with the SDK's own SigV4 scope. Bodies without the field pass through untouched, so this is a no-op once the SDK stops renaming (vercel/ai#24919) and can then be removed. --- .../src/model-clients/BedrockClient.ts | 3 +- .../bedrock-block-binding-wire.test.ts | 120 ++++++++++++++++++ .../bedrock-block-binding-fetch.ts | 91 +++++++++++++ 3 files changed, 213 insertions(+), 1 deletion(-) create mode 100644 packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts create mode 100644 packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts diff --git a/packages/ai-provider-bridge/src/model-clients/BedrockClient.ts b/packages/ai-provider-bridge/src/model-clients/BedrockClient.ts index 8059ff9..48bad68 100644 --- a/packages/ai-provider-bridge/src/model-clients/BedrockClient.ts +++ b/packages/ai-provider-bridge/src/model-clients/BedrockClient.ts @@ -37,6 +37,7 @@ import { createAbortControllerFromToken, stepLoggingOptions, } from "./ai-sdk-helpers"; +import { withPrefixMismatchBehavior } from "./bedrock-block-binding-fetch"; import type { ModelClient, ModelClientChatParams } from "./ModelClient"; import { prepareExplicitOpenAIRequest } from "./openai-prompt-caching"; import { mergeOpenAIWebSearchTool } from "./provider-tools"; @@ -355,7 +356,7 @@ export class BedrockClient implements ModelClient { headers, credentialProvider, ...explicitKeyAuth, - ...(loggedFetch && { fetch: loggedFetch }), + fetch: withPrefixMismatchBehavior(credentialProvider, loggedFetch), })(modelId); } diff --git a/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts b/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts new file mode 100644 index 0000000..1830352 --- /dev/null +++ b/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts @@ -0,0 +1,120 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (C) 2026 Posit Software, PBC. All rights reserved. + *--------------------------------------------------------------------------------------------*/ + +/** + * Bedrock InvokeModel wire for thinking block binding. `@ai-sdk/amazon-bedrock` + * renames the field to the deprecated `mismatch_behavior`, which some regions + * ignore, so the client must send `prefix_mismatch_behavior` and a SigV4 + * signature that covers the body actually sent. + */ + +import { AwsV4Signer } from "aws4fetch"; +import { afterEach, beforeEach, describe, expect, it, vi } from "vitest"; + +import { + createRawFetchCapture, + type RawFetchCapture, +} from "../../../tests/helpers/raw-fetch-capture"; +import type { CancellationToken } from "../../types"; +import { THINKING_BINDING_BETA } from "../../utils"; +import { BedrockClient } from "../BedrockClient"; + +vi.mock("../../providers/bedrock-transport", async (importOriginal) => ({ + ...(await importOriginal()), + resolveBedrockTransport: async () => ({ + useFipsEndpoint: false, + mantleEnabled: true, + runtimeBaseUrl: "https://bedrock-runtime.us-east-1.amazonaws.com", + }), + resolveConfiguredBedrockEndpoints: async () => ({ runtime: undefined, listing: undefined }), +})); + +const cancellationToken: CancellationToken = { + isCancellationRequested: false, + onCancellationRequested: () => ({ dispose() {} }), +}; + +const keys = { + accessKeyId: "AKIDTEST", + secretAccessKey: "secret-test", + sessionToken: "token-test", +}; + +let fetchCapture: RawFetchCapture; + +beforeEach(() => { + fetchCapture = createRawFetchCapture( + async () => + new Response("", { + status: 200, + headers: { "content-type": "application/vnd.amazon.eventstream" }, + }), + ); + vi.stubGlobal("fetch", fetchCapture.mock); +}); + +afterEach(() => { + vi.unstubAllGlobals(); +}); + +/** + * Send one request through the stubbed fetch. The mocked stream is empty, and + * BedrockClient leaves AI SDK's default error logging on, so expect its one + * NoOutputGeneratedError log. + */ +async function sendRequest(): Promise { + const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + try { + const stream = await new BedrockClient({ region: "us-east-1", ...keys }).chat({ + model: "us.anthropic.claude-opus-5-5", + messages: [{ role: "user", content: "hello" }], + maxOutputTokens: 1024, + thinkingEffort: "high", + thinkingBlockBinding: "drop_block", + cancellationToken, + }); + for await (const _part of stream) { + // Drain the (empty) mocked event stream. + } + } catch { + // Only the request matters. + } + expect(consoleError).toHaveBeenCalledTimes(1); + expect(consoleError).toHaveBeenCalledWith( + expect.objectContaining({ name: "AI_NoOutputGeneratedError" }), + ); + } finally { + consoleError.mockRestore(); + } +} + +describe("Bedrock thinking block binding wire", () => { + it("sends prefix_mismatch_behavior with the beta", async () => { + await sendRequest(); + const [, init] = fetchCapture.single(); + const body = JSON.parse(String(init?.body)); + expect(body.thinking.block_binding).toEqual({ prefix_mismatch_behavior: "drop_block" }); + expect(body.anthropic_beta).toContain(THINKING_BINDING_BETA); + }); + + it("signs the body it sends", async () => { + await sendRequest(); + const [input, init] = fetchCapture.single(); + const sent = new Headers(init?.headers); + const unsigned = new Headers(sent); + unsigned.delete("authorization"); + const expected = await new AwsV4Signer({ + url: String(input), + method: "POST", + headers: unsigned, + body: String(init?.body), + region: "us-east-1", + service: "bedrock", + datetime: sent.get("x-amz-date") ?? undefined, + ...keys, + }).sign(); + expect(sent.get("authorization")).toBe(expected.headers.get("authorization")); + }); +}); diff --git a/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts b/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts new file mode 100644 index 0000000..fec8f3d --- /dev/null +++ b/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts @@ -0,0 +1,91 @@ +/*--------------------------------------------------------------------------------------------- + * Copyright (C) 2026 Posit Software, PBC. All rights reserved. + *--------------------------------------------------------------------------------------------*/ + +import { AwsV4Signer } from "aws4fetch"; + +import type { ResolvedAwsCredentials } from "../aws-credentials"; + +/** Region and service from a SigV4 `Authorization` header's credential scope. */ +const SIGV4_SCOPE = /^AWS4-HMAC-SHA256 Credential=[^/]+\/\d{8}\/([^/]+)\/([^/]+)\/aws4_request/; + +/** Headers the SDK's signature added; dropped so the new signature replaces them. */ +const SIGNATURE_HEADERS = [ + "authorization", + "x-amz-date", + "x-amz-security-token", + "x-amz-content-sha256", +]; + +/** + * Fetch for Bedrock's Anthropic (InvokeModel) route that sends + * `thinking.block_binding.prefix_mismatch_behavior`. + * + * `@ai-sdk/amazon-bedrock` renames the field to `mismatch_behavior` + * (vercel/ai#20728). AWS documents that name as a deprecated alias and some + * regions ignore it, so a prefix change fails with the 400 the binding exists + * to prevent. The SDK renames before it signs, so this renames the field back + * and re-signs with the scope the SDK chose. Bodies without the field pass + * through untouched, so this becomes a no-op once the SDK stops renaming + * (vercel/ai#24919) and can then be removed. + */ +export function withPrefixMismatchBehavior( + credentialProvider: () => Promise, + fetchFunction: typeof globalThis.fetch | undefined, +): typeof globalThis.fetch { + return async (input, init) => { + const send = fetchFunction ?? globalThis.fetch; + const body = typeof init?.body === "string" ? renameMismatchBehavior(init.body) : undefined; + if (init === undefined || body === undefined) { + return send(input, init); + } + const headers = new Headers(init.headers); + const scope = SIGV4_SCOPE.exec(headers.get("authorization") ?? ""); + if (!scope) { + // Bearer-token auth doesn't sign the body. + return send(input, { ...init, body }); + } + for (const name of SIGNATURE_HEADERS) { + headers.delete(name); + } + const credentials = await credentialProvider(); + const signed = await new AwsV4Signer({ + url: typeof input === "string" ? input : input instanceof URL ? input.href : input.url, + method: "POST", + headers, + body, + region: scope[1], + service: scope[2], + accessKeyId: credentials.accessKeyId, + secretAccessKey: credentials.secretAccessKey, + sessionToken: credentials.sessionToken, + }).sign(); + return send(input, { ...init, body, headers: signed.headers }); + }; +} + +/** The body with `mismatch_behavior` renamed, or `undefined` when there is nothing to rename. */ +function renameMismatchBehavior(body: string): string | undefined { + if (!body.includes('"mismatch_behavior"')) { + return undefined; + } + const parsed: unknown = JSON.parse(body); + if (!isRecord(parsed) || !isRecord(parsed.thinking) || !isRecord(parsed.thinking.block_binding)) { + return undefined; + } + const { mismatch_behavior: behavior, ...rest } = parsed.thinking.block_binding; + if (behavior === undefined || "prefix_mismatch_behavior" in rest) { + return undefined; + } + return JSON.stringify({ + ...parsed, + thinking: { + ...parsed.thinking, + block_binding: { ...rest, prefix_mismatch_behavior: behavior }, + }, + }); +} + +function isRecord(value: unknown): value is Record { + return typeof value === "object" && value !== null && !Array.isArray(value); +} From 51e3d56af391b477a65b95f9aa288b6137196fb1 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Thu, 12 Nov 2026 14:47:40 -0600 Subject: [PATCH 2/3] Leave non-ASCII headers unsigned when re-signing Bedrock InvokeModel bodies Fetch sends header values as Latin-1 while SigV4 hashes UTF-8, so signing a non-ASCII custom header produced a signature the server rejects. Match the SDK and exclude those headers from the signature while still sending them. Extend the wire tests to cover bearer auth and a gateway signed for a non-default region. --- .../bedrock-block-binding-wire.test.ts | 80 +++++++++++++++---- .../bedrock-block-binding-fetch.ts | 13 +++ 2 files changed, 77 insertions(+), 16 deletions(-) diff --git a/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts b/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts index 1830352..aff9829 100644 --- a/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts +++ b/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-block-binding-wire.test.ts @@ -18,7 +18,7 @@ import { } from "../../../tests/helpers/raw-fetch-capture"; import type { CancellationToken } from "../../types"; import { THINKING_BINDING_BETA } from "../../utils"; -import { BedrockClient } from "../BedrockClient"; +import { BedrockClient, type BedrockClientConfig } from "../BedrockClient"; vi.mock("../../providers/bedrock-transport", async (importOriginal) => ({ ...(await importOriginal()), @@ -41,6 +41,8 @@ const keys = { sessionToken: "token-test", }; +const GATEWAY = "https://connect.example.com/__gateway__/bedrock/g1"; + let fetchCapture: RawFetchCapture; beforeEach(() => { @@ -56,6 +58,7 @@ beforeEach(() => { afterEach(() => { vi.unstubAllGlobals(); + delete process.env.AWS_BEARER_TOKEN_BEDROCK; }); /** @@ -63,12 +66,16 @@ afterEach(() => { * BedrockClient leaves AI SDK's default error logging on, so expect its one * NoOutputGeneratedError log. */ -async function sendRequest(): Promise { +async function sendRequest( + config: BedrockClientConfig = { region: "us-east-1", ...keys }, + baseUrl?: string, +): Promise { const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); try { try { - const stream = await new BedrockClient({ region: "us-east-1", ...keys }).chat({ + const stream = await new BedrockClient(config).chat({ model: "us.anthropic.claude-opus-5-5", + baseUrl, messages: [{ role: "user", content: "hello" }], maxOutputTokens: 1024, thinkingEffort: "high", @@ -99,22 +106,63 @@ describe("Bedrock thinking block binding wire", () => { expect(body.anthropic_beta).toContain(THINKING_BINDING_BETA); }); - it("signs the body it sends", async () => { - await sendRequest(); + it("signs the body it sends with the SDK's scope", async () => { + await sendRequest({ region: "eu-west-1", ...keys }, GATEWAY); const [input, init] = fetchCapture.single(); + expect(String(input).startsWith(GATEWAY)).toBe(true); const sent = new Headers(init?.headers); - const unsigned = new Headers(sent); - unsigned.delete("authorization"); - const expected = await new AwsV4Signer({ - url: String(input), - method: "POST", - headers: unsigned, - body: String(init?.body), + expect(sent.get("authorization")).toContain("/eu-west-1/bedrock/aws4_request"); + expect(sent.get("authorization")).toBe( + await expectedAuthorization(String(input), init, "eu-west-1"), + ); + }); + + it("leaves a non-ASCII custom header unsigned but sends it", async () => { + await sendRequest({ region: "us-east-1", - service: "bedrock", - datetime: sent.get("x-amz-date") ?? undefined, ...keys, - }).sign(); - expect(sent.get("authorization")).toBe(expected.headers.get("authorization")); + customHeaders: { "x-title": "Example · App" }, + }); + const [input, init] = fetchCapture.single(); + const sent = new Headers(init?.headers); + expect(sent.get("x-title")).toBe("Example · App"); + expect(sent.get("authorization")).not.toContain("x-title"); + expect(sent.get("authorization")).toBe( + await expectedAuthorization(String(input), init, "us-east-1", ["x-title"]), + ); + }); + + it("sends prefix_mismatch_behavior with bearer auth", async () => { + process.env.AWS_BEARER_TOKEN_BEDROCK = "tok"; + await sendRequest({ region: "us-east-1" }); + const [, init] = fetchCapture.single(); + expect(new Headers(init?.headers).get("authorization")).toBe("Bearer tok"); + const body = JSON.parse(String(init?.body)); + expect(body.thinking.block_binding).toEqual({ prefix_mismatch_behavior: "drop_block" }); }); }); + +/** The Authorization a correct SigV4 signature of the sent request would carry. */ +async function expectedAuthorization( + url: string, + init: RequestInit | undefined, + region: string, + unsignedHeaders: string[] = [], +): Promise { + const sent = new Headers(init?.headers); + const signedInput = new Headers(sent); + for (const name of ["authorization", ...unsignedHeaders]) { + signedInput.delete(name); + } + const expected = await new AwsV4Signer({ + url, + method: "POST", + headers: signedInput, + body: String(init?.body), + region, + service: "bedrock", + datetime: sent.get("x-amz-date") ?? undefined, + ...keys, + }).sign(); + return expected.headers.get("authorization"); +} diff --git a/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts b/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts index fec8f3d..2e3c759 100644 --- a/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts +++ b/packages/ai-provider-bridge/src/model-clients/bedrock-block-binding-fetch.ts @@ -48,6 +48,12 @@ export function withPrefixMismatchBehavior( for (const name of SIGNATURE_HEADERS) { headers.delete(name); } + // Fetch sends header values as Latin-1 but SigV4 hashes them as UTF-8, so + // a non-ASCII value can't be signed. Leave it unsigned, as the SDK does. + const unsignable = [...headers].filter(([, value]) => !isAscii(value)); + for (const [name] of unsignable) { + headers.delete(name); + } const credentials = await credentialProvider(); const signed = await new AwsV4Signer({ url: typeof input === "string" ? input : input instanceof URL ? input.href : input.url, @@ -60,6 +66,9 @@ export function withPrefixMismatchBehavior( secretAccessKey: credentials.secretAccessKey, sessionToken: credentials.sessionToken, }).sign(); + for (const [name, value] of unsignable) { + signed.headers.set(name, value); + } return send(input, { ...init, body, headers: signed.headers }); }; } @@ -86,6 +95,10 @@ function renameMismatchBehavior(body: string): string | undefined { }); } +function isAscii(value: string): boolean { + return !/[\u0080-\uffff]/.test(value); +} + function isRecord(value: unknown): value is Record { return typeof value === "object" && value !== null && !Array.isArray(value); } From 56fb6f3c569fb77e32258594ec81c71b005b3dc2 Mon Sep 17 00:00:00 2001 From: Winston Chang Date: Thu, 12 Nov 2026 14:48:45 -0600 Subject: [PATCH 3/3] Capture the expected APICallError log in Bedrock auth wire tests The mocked 400 makes AI SDK's default error logging print to stderr. Capture it in send() and assert the single expected error instead. --- .../bedrock-runtime-auth-wire.test.ts | 30 ++++++++++++++----- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-runtime-auth-wire.test.ts b/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-runtime-auth-wire.test.ts index 695153e..6df8144 100644 --- a/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-runtime-auth-wire.test.ts +++ b/packages/ai-provider-bridge/src/model-clients/__tests__/bedrock-runtime-auth-wire.test.ts @@ -46,17 +46,31 @@ async function consumeIgnoringNetworkFailure( } } +/** + * Send one request through the stubbed fetch. The mocked 400 fails the call, + * and BedrockClient leaves AI SDK's default error logging on, so expect its + * one APICallError log. + */ async function send(client: BedrockClient, model: string, baseUrl?: string) { const capture = createRawFetchCapture(async () => new Response("{}", { status: 400 })); vi.stubGlobal("fetch", capture.mock); - await consumeIgnoringNetworkFailure( - client.chat({ - model, - baseUrl, - messages: [{ role: "user", content: "Hello" }], - cancellationToken, - }), - ); + const consoleError = vi.spyOn(console, "error").mockImplementation(() => {}); + try { + await consumeIgnoringNetworkFailure( + client.chat({ + model, + baseUrl, + messages: [{ role: "user", content: "Hello" }], + cancellationToken, + }), + ); + expect(consoleError).toHaveBeenCalledTimes(1); + expect(consoleError).toHaveBeenCalledWith( + expect.objectContaining({ name: "AI_APICallError", statusCode: 400 }), + ); + } finally { + consoleError.mockRestore(); + } const [input, init] = capture.call(0); return { url: String(input instanceof Request ? input.url : input),