Skip to content

🪢 Open source agent evals & observability: Trace, evaluate, and improve LLM applications with one open platform.

fix(evals): support Sonnet 5 on Bedrock (#15375)

Hassieb Pakzad8dbacf4db78b
diff --git a/packages/shared/package.json b/packages/shared/package.json
index 5bada69bbb..b9018604ce 100644
--- a/packages/shared/package.json
+++ b/packages/shared/package.json
@@ -94,7 +94,7 @@
"seed": "dotenv -e ../../.env -- tsx scripts/seeder/seed-postgres.ts"
},
"dependencies": {
- "@ai-sdk/amazon-bedrock": "^5.0.8",
+ "@ai-sdk/amazon-bedrock": "^5.0.24",
"@ai-sdk/anthropic": "^4.0.5",
"@ai-sdk/azure": "^4.0.5",
"@ai-sdk/google": "^4.0.6",
diff --git a/packages/shared/src/server/llm/ai-sdk/requestShape.test.ts b/packages/shared/src/server/llm/ai-sdk/requestShape.test.ts
index 5f599bdf95..9663529072 100644
--- a/packages/shared/src/server/llm/ai-sdk/requestShape.test.ts
+++ b/packages/shared/src/server/llm/ai-sdk/requestShape.test.ts
@@ -54,17 +54,22 @@ type CapturedRequest = {
* provider response, so the assertion target is the exact wire format the AI
* SDK provider constructs (URL, auth header, JSON body).
*/
-function createCaptureFetch(response: unknown) {
+function createCaptureFetch(
+ response: unknown | ((request: CapturedRequest) => unknown),
+) {
const calls: CapturedRequest[] = [];
const fetchImpl: typeof fetch = async (input, init) => {
const request = new Request(input, init);
- calls.push({
+ const capturedRequest = {
url: request.url,
method: request.method,
headers: request.headers,
body: JSON.parse(await request.text()) as Record<string, unknown>,
- });
- return new Response(JSON.stringify(response), {
+ };
+ calls.push(capturedRequest);
+ const responseBody =
+ typeof response === "function" ? response(capturedRequest) : response;
+ return new Response(JSON.stringify(responseBody), {
status: 200,
headers: { "content-type": "application/json" },
});
@@ -536,17 +541,23 @@ describe("AI SDK request shapes", () => {
// The Bedrock builder deliberately uses no custom fetch (VPC endpoints),
// so capture at the global fetch the AI SDK falls back to.
- async function runBedrockCompletion() {
+ async function runBedrockCompletion(params?: {
+ model?: string;
+ output?: ReturnType<typeof createLLMOutput>;
+ response?: unknown | ((request: CapturedRequest) => unknown);
+ }) {
const modelParams: ModelParams = {
provider: "bedrock",
adapter: LLMAdapter.Bedrock,
- model: "anthropic.claude-3-5-sonnet-20241022-v2:0",
+ model: params?.model ?? "anthropic.claude-3-5-sonnet-20241022-v2:0",
max_tokens: 64,
providerOptions: { top_k: 10 },
};
const llmConnectionConfig = { region: "us-east-1" };
- const { calls, fetch } = createCaptureFetch(BEDROCK_RESPONSE);
+ const { calls, fetch } = createCaptureFetch(
+ params?.response ?? BEDROCK_RESPONSE,
+ );
vi.stubGlobal("fetch", fetch);
const result = await generateLLMText({
@@ -564,15 +575,18 @@ describe("AI SDK request shapes", () => {
},
}),
timeout: 10_000,
+ output: params?.output,
});
- expect(result.text).toBe("ok");
+ if (!params?.output) {
+ expect(result.text).toBe("ok");
+ }
expect(calls).toHaveLength(1);
- return calls[0];
+ return { result, request: calls[0] };
}
it("Bedrock: converse URL from validated region, SigV4 auth, verbatim additional fields", async () => {
- const request = await runBedrockCompletion();
+ const { request } = await runBedrockCompletion();
expect(decodeURIComponent(request.url)).toBe(
"https://bedrock-runtime.us-east-1.amazonaws.com/model/anthropic.claude-3-5-sonnet-20241022-v2:0/converse",
@@ -582,6 +596,64 @@ describe("AI SDK request shapes", () => {
expect(request.body.inferenceConfig).toMatchObject({ maxTokens: 64 });
});
+ it("Bedrock: Sonnet 5 structured output falls back to the JSON tool", async () => {
+ const schema = {
+ type: "object",
+ properties: { answer: { type: "string" } },
+ required: ["answer"],
+ additionalProperties: false,
+ } as const;
+ const { result, request } = await runBedrockCompletion({
+ model: "us.anthropic.claude-sonnet-5",
+ output: createLLMOutput(schema),
+ response: (capturedRequest: CapturedRequest) =>
+ capturedRequest.body.toolConfig
+ ? {
+ output: {
+ message: {
+ role: "assistant",
+ content: [
+ {
+ toolUse: {
+ toolUseId: "tool-1",
+ name: "json",
+ input: { answer: "ok" },
+ },
+ },
+ ],
+ },
+ },
+ stopReason: "tool_use",
+ usage: { inputTokens: 1, outputTokens: 1, totalTokens: 2 },
+ metrics: { latencyMs: 1 },
+ }
+ : {
+ ...BEDROCK_RESPONSE,
+ output: {
+ message: {
+ role: "assistant",
+ content: [{ text: '{"answer":"ok"}' }],
+ },
+ },
+ },
+ });
+
+ expect(result.output).toEqual({ answer: "ok" });
+ expect(request.body.additionalModelRequestFields).toEqual({ top_k: 10 });
+ expect(request.body.toolConfig).toEqual({
+ tools: [
+ {
+ toolSpec: {
+ name: "json",
+ description: "Respond with a JSON object.",
+ inputSchema: { json: schema },
+ },
+ },
+ ],
+ toolChoice: { any: {} },
+ });
+ });
+
it("Bedrock: tenant credentials suppress server-level env auth fallbacks", async () => {
// A self-hosted operator may set these for their own purposes; tenant
// connections must never authenticate with them. Unsuppressed, the
@@ -590,7 +662,7 @@ describe("AI SDK request shapes", () => {
vi.stubEnv("AWS_BEARER_TOKEN_BEDROCK", "server-bearer-token");
vi.stubEnv("AWS_SESSION_TOKEN", "server-session-token");
- const request = await runBedrockCompletion();
+ const { request } = await runBedrockCompletion();
// SigV4 with the tenant keys — not `Bearer server-bearer-token`.
expect(request.headers.get("authorization")).toMatch(/^AWS4-HMAC-SHA256/);
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index 7489ac4af8..6e82f391e2 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -203,8 +203,8 @@ importers:
packages/shared:
dependencies:
'@ai-sdk/amazon-bedrock':
- specifier: ^5.0.8
- version: 5.0.8([email protected])
+ specifier: ^5.0.24
+ version: 5.0.24([email protected])
'@ai-sdk/anthropic':
specifier: ^4.0.5
version: 4.0.5([email protected])
@@ -1199,8 +1199,8 @@ packages:
peerDependencies:
zod: 4.3.6
- '@ai-sdk/[email protected]':
- resolution: {integrity: sha512-E4TozODmCKGS5VlCBnCR1Iffd0e0hkcbjpjzlqvxQPTJlw0hm9sJ3vltAIRU3wTKVTn0BfZiKzYylu2CAEyP6g==}
+ '@ai-sdk/[email protected]':
+ resolution: {integrity: sha512-DqepS/e0mZfpfFjbCKIG7LzsvrQYeKJT0aoQDC6WyXvR81Vi1VIJVk6IL2q6dq2zwuBNnXIQkUPFF0NTXRXGRQ==}
engines: {node: '>=22'}
peerDependencies:
zod: 4.3.6
@@ -1217,6 +1217,12 @@ packages:
peerDependencies:
zod: 4.3.6
+ '@ai-sdk/[email protected]':
+ resolution: {integrity: sha512-vyH4D6Auih5H2xvVzzh2ep5pbdWiaV7JDC+jHUE7zZJ5Kyv0TteLav4DrOgHzRuyv8ptfUSqFF6Y8//f/Ec0fQ==}
+ engines: {node: '>=22'}
+ peerDependencies:
+ zod: 4.3.6
+
'@ai-sdk/[email protected]':
resolution: {integrity: sha512-JyJYKWGYz8vfoQ1pCm48a9zgX3Gvb2/6iGHb+8pEelpqnOiUq3aDHVq+nYMf9VepqcjCuH0JSOwBrKiPg/PD9w==}
engines: {node: '>=22'}
@@ -1283,6 +1289,12 @@ packages:
peerDependencies:
zod: 4.3.6
+ '@ai-sdk/[email protected]':
+ resolution: {integrity: sha512-Yh+PsXaf9NbN7oA3oKwOuyjTiHMPD75phf3SqGbDNNKQ3Yj3oTntp/WhO3nCHIPA6gr5/1lyridQokmOpPf9oQ==}
+ engines: {node: '>=22'}
+ peerDependencies:
+ zod: 4.3.6
+
'@ai-sdk/[email protected]':
resolution: {integrity: sha512-a9cYQNAKAbkMTt6bHAvDuLv3klwtJT9m0A6x7s8EZlf1ebcxadg/1CONaYh1GA80ZRA9UQHJUS2wHoOphkQQkg==}
engines: {node: '>=22'}
@@ -1301,6 +1313,12 @@ packages:
peerDependencies:
zod: 4.3.6
+ '@ai-sdk/[email protected]':
+ resolution: {integrity: sha512-7/96wE+ZsKB35iS9ASyllrE4Ym/EolXEB7AkuJ5FI++fmS85BVTAs77890C+1Z2jwHfBKjBQSBmsliOsAh0iFQ==}
+ engines: {node: '>=22'}
+ peerDependencies:
+ zod: 4.3.6
+
'@ai-sdk/[email protected]':
resolution: {integrity: sha512-EcmdjJb7yggsZPCbS3MFBpvAUnKaPW+QvanU5GzF00XCq0bqqAmvJ3MN19ejlmOETbW8sJNiq6qam48wTcbUNw==}
engines: {node: '>=22'}
@@ -1337,6 +1355,10 @@ packages:
resolution: {integrity: sha512-6p3C/vGqVIjcptBu1DnVd/BZJ2wWmV9TUv9192vT6ZvT9KNED8EwRTqyqFpoQZKgSbMDSvBSq3dqR524Nt/Crw==}
engines: {node: '>=22'}
+ '@ai-sdk/[email protected]':
+ resolution: {integrity: sha512-e0CpNWJUY7OxAFAnCZkw+ri9QOHWwTs1tXP42782KFGCU07qt8NiXCrCVowyCB5dP2r5/Uls+g2oPd8kOJn9dw==}
+ engines: {node: '>=22'}
+
'@ai-sdk/[email protected]':
resolution: {integrity: sha512-3zcwCc8ezzFlwp3ZD15wAPjf2Au4s3vAbKsXQVyhxODHcmu0iyPO2Eua6D/vicq/AUm/BAo60r97O6HU+EI0+w==}
engines: {node: '>=18'}
@@ -4918,10 +4940,18 @@ packages:
resolution: {integrity: sha512-L+Ys6ecjk5vwPMAKHBpPKlJ3DkqwNcnfEISXBZIsVvWG/XKXfsAP8mwIYlTeLcd2ElHdesPI8OuOmJSFAPhm6A==}
engines: {node: '>=18.0.0'}
+ '@smithy/[email protected]':
+ resolution: {integrity: sha512-i0dk2t5B+CwV/dcJdUHILYkOQF5lof8f44dFCfDWToGCxjT9YQ+CgHqTAvJxzc3+zqQwm2QtVoJ5IqiNar/CnQ==}
+ engines: {node: '>=18.0.0'}
+
'@smithy/[email protected]':
resolution: {integrity: sha512-q9J7JTiXrAhB8sDp4px97uEPT7CwKH61Co78grdNQvU8QZAdiuaSRhP0tUVf2ogy36RZTrlMU1rBmDEH+cnkiA==}
engines: {node: '>=18.0.0'}
+ '@smithy/[email protected]':
+ resolution: {integrity: sha512-yG1n59zLQMa979xvXlTQ9+FpNmm4RRPR+2rYZo57wk28E27zmKZN4ST9wc2pKkyspLpDal2/84ST72KLJhbP1w==}
+ engines: {node: '>=18.0.0'}
+
'@smithy/[email protected]':
resolution: {integrity: sha512-rasV+96Obv6tEhWPKWaehEhR+MEd2/lE/rdOYcmSMh6tJiQdoZg4v21p37Y1A7DMZOFgFDUX/3eNl/J3+MJDdA==}
engines: {node: '>=18.0.0'}
@@ -5086,6 +5116,10 @@ packages:
resolution: {integrity: sha512-R8Rdn8Hy72KKcebgLiv8jQcQkXoLMOGGv5uI1/k0l+snqkOzQ1R0ChUBCxWMlBsFMekWjq0wRudIweFs7sKT5A==}
engines: {node: '>=14.0.0'}
+ '@smithy/[email protected]':
+ resolution: {integrity: sha512-4pWFv2sxrykZqaTixXhkgAsG6+k/VxgAiyZ6M0iLEmsOqcJaR0eidlTCmuKKtMJMIEw8lYwDTvEyR4NyC+V0cw==}
+ engines: {node: '>=18.0.0'}
+
'@smithy/[email protected]':
resolution: {integrity: sha512-ZS7Y5X8mU9qRSsqwOeGKw86WWtPsRxa396kX2HyhYwADRqFHJ0cb3p2uFk6QnFF3BluUUBHTZJpPA9QuEl0tlg==}
engines: {node: '>=18.0.0'}
@@ -7686,6 +7720,10 @@ packages:
resolution: {integrity: sha512-70QWGkr4snxr0OXLRWsFLeRBIRPuQOvt4s8QYjmUlmlkyTZkRqS7EDVRZtzU3TiyDbXSzaOeF0XUKy8PchzukQ==}
engines: {node: '>=18.0.0'}
+ resolution: {integrity: sha512-kJezFj9YFAMLeORyi7aCLxLbD5/qWMQnoMVlVPyHIll7lgRJCc3JVln9Vgl9nwQi0YkMnhdGTMNn7CkRRAptMg==}
+ engines: {node: '>=18.0.0'}
+
resolution: {integrity: sha512-CRT1WTyuQoD771GW56XEZFQ/ZoSfWid1alKGDYMmkt2yl8UXrVR4pspqWNEcqKvVIzg6PAltWjxcSSPrboA4iA==}
engines: {node: '>=18.0.0'}
@@ -11768,14 +11806,14 @@ snapshots:
aws4fetch: 1.0.20
zod: 4.3.6
dependencies:
- '@ai-sdk/anthropic': 4.0.5([email protected])
- '@ai-sdk/openai': 4.0.5([email protected])
- '@ai-sdk/provider': 4.0.1
- '@ai-sdk/provider-utils': 5.0.3([email protected])
- '@smithy/eventstream-codec': 4.4.5
- '@smithy/util-utf8': 4.4.5
+ '@ai-sdk/anthropic': 4.0.16([email protected])
+ '@ai-sdk/openai': 4.0.16([email protected])
+ '@ai-sdk/provider': 4.0.3
+ '@ai-sdk/provider-utils': 5.0.11([email protected])
+ '@smithy/eventstream-codec': 4.4.10
+ '@smithy/util-utf8': 4.4.10
aws4fetch: 1.0.20
zod: 4.3.6
@@ -11791,6 +11829,12 @@ snapshots:
'@ai-sdk/provider-utils': 4.0.27([email protected])
zod: 4.3.6
+ dependencies:
+ '@ai-sdk/provider': 4.0.3
+ '@ai-sdk/provider-utils': 5.0.11([email protected])
+ zod: 4.3.6
+
dependencies:
'@ai-sdk/provider': 4.0.1
@@ -11868,6 +11912,12 @@ snapshots:
'@ai-sdk/provider-utils': 4.0.27([email protected])
zod: 4.3.6
+ dependencies:
+ '@ai-sdk/provider': 4.0.3
+ '@ai-sdk/provider-utils': 5.0.11([email protected])
+ zod: 4.3.6
+
dependencies:
'@ai-sdk/provider': 4.0.1
@@ -11888,6 +11938,14 @@ snapshots:
eventsource-parser: 3.0.8
zod: 4.3.6
+ dependencies:
+ '@ai-sdk/provider': 4.0.3
+ '@standard-schema/spec': 1.1.0
+ '@workflow/serde': 4.1.0
+ eventsource-parser: 3.1.0
+ zod: 4.3.6
+
dependencies:
'@ai-sdk/provider': 4.0.1
@@ -11928,6 +11986,10 @@ snapshots:
dependencies:
json-schema: 0.4.0
+ '@ai-sdk/[email protected]':
+ dependencies:
+ json-schema: 0.4.0
+
dependencies:
'@ai-sdk/provider': 1.1.3
@@ -16012,12 +16074,22 @@ snapshots:
'@smithy/types': 4.16.1
tslib: 2.8.1
+ '@smithy/[email protected]':
+ dependencies:
+ '@smithy/types': 4.16.1
+ tslib: 2.8.1
+
'@smithy/[email protected]':
dependencies:
'@smithy/core': 3.29.3
'@smithy/types': 4.16.1
tslib: 2.8.1
+ '@smithy/[email protected]':
+ dependencies:
+ '@smithy/core': 3.29.5
+ tslib: 2.8.1
+
'@smithy/[email protected]':
dependencies:
'@smithy/core': 3.29.3
@@ -16257,6 +16329,11 @@ snapshots:
'@smithy/util-buffer-from': 2.2.0
tslib: 2.8.1
+ '@smithy/[email protected]':
+ dependencies:
+ '@smithy/core': 3.29.5
+ tslib: 2.8.1
+
'@smithy/[email protected]':
dependencies:
'@smithy/core': 3.29.3
@@ -19158,6 +19235,8 @@ snapshots:
+
dependencies:
eventsource-parser: 3.0.8