diff --git a/packages/ai/src/provider-error.ts b/packages/ai/src/provider-error.ts index 5f6574846e8..35b9cff7425 100644 --- a/packages/ai/src/provider-error.ts +++ b/packages/ai/src/provider-error.ts @@ -21,6 +21,8 @@ const patterns = [ /exceeds the context window/i, /exceeds (?:the )?(?:model'?s )?maximum context length(?: of [\d,]+ tokens?|\s*\([\d,]+\))/i, /input token count.*exceeds the maximum/i, + // Amazon Nova on Bedrock reports this as a mid-stream validationException. + /number of input tokens exceeds maximum length/i, /tokens in request more than max tokens allowed/i, /maximum prompt length is \d+/i, /reduce the length of the messages/i, diff --git a/packages/ai/test/provider-error.test.ts b/packages/ai/test/provider-error.test.ts index 52599f8ae3f..98a94c1b143 100644 --- a/packages/ai/test/provider-error.test.ts +++ b/packages/ai/test/provider-error.test.ts @@ -366,6 +366,23 @@ describe("provider error rawBody classification", () => { expect(reason._tag === "InvalidRequest" ? reason.classification : reason._tag).toBe("context-overflow") }) + test("classifies Amazon Nova input token rejections as context overflow", () => { + const message = + "The model returned the following errors: Input Tokens Exceeded: Number of input tokens exceeds maximum length. Please update the input to try again." + const reason = classifyProviderFailure({ + message, + rawBody: JSON.stringify({ + headers: { + ":exception-type": { type: "string", value: "validationException" }, + ":content-type": { type: "string", value: "application/json" }, + ":message-type": { type: "string", value: "exception" }, + }, + body: JSON.stringify({ message }), + }), + }) + expect(reason._tag === "InvalidRequest" ? reason.classification : reason._tag).toBe("context-overflow") + }) + test("classifies Google invalid API keys as authentication failures", () => { const rawBody = JSON.stringify({ error: {