Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -355,7 +356,7 @@ export class BedrockClient implements ModelClient {
headers,
credentialProvider,
...explicitKeyAuth,
...(loggedFetch && { fetch: loggedFetch }),
fetch: withPrefixMismatchBehavior(credentialProvider, loggedFetch),
})(modelId);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,168 @@
/*---------------------------------------------------------------------------------------------
* 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, type BedrockClientConfig } from "../BedrockClient";

vi.mock("../../providers/bedrock-transport", async (importOriginal) => ({
...(await importOriginal<typeof import("../../providers/bedrock-transport")>()),
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",
};

const GATEWAY = "https://connect.example.com/__gateway__/bedrock/g1";

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();
delete process.env.AWS_BEARER_TOKEN_BEDROCK;
});

/**
* 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(
config: BedrockClientConfig = { region: "us-east-1", ...keys },
baseUrl?: string,
): Promise<void> {
const consoleError = vi.spyOn(console, "error").mockImplementation(() => {});
try {
try {
const stream = await new BedrockClient(config).chat({
model: "us.anthropic.claude-opus-5-5",
baseUrl,
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 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);
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",
...keys,
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<string | null> {
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");
}
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
/*---------------------------------------------------------------------------------------------
* 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<ResolvedAwsCredentials>,
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);
}
// 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,
method: "POST",
headers,
body,
region: scope[1],
service: scope[2],
accessKeyId: credentials.accessKeyId,
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 });
};
}

/** 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 isAscii(value: string): boolean {
return !/[\u0080-\uffff]/.test(value);
}

function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === "object" && value !== null && !Array.isArray(value);
}
Loading