fix(embedded): classify model_context_window_exceeded as context overflow error

This commit is contained in:
Kaiyi
2026-03-05 18:26:13 +08:00
committed by Josh Lehman
parent 246410947d
commit 39c1f96fc2
2 changed files with 18 additions and 0 deletions

View File

@@ -269,6 +269,21 @@ describe("isContextOverflowError", () => {
}
});
it("matches model_context_window_exceeded stop reason surfaced by pi-ai", () => {
// Anthropic API (and some OpenAI-compatible providers like ZhipuAI/GLM) return
// stop_reason: "model_context_window_exceeded" when the context window is hit.
// The pi-ai library surfaces this as "Unhandled stop reason: model_context_window_exceeded".
const samples = [
"Unhandled stop reason: model_context_window_exceeded",
"model_context_window_exceeded",
"context_window_exceeded",
"Unhandled stop reason: context_window_exceeded",
];
for (const sample of samples) {
expect(isContextOverflowError(sample)).toBe(true);
}
});
it("matches Chinese context overflow error messages from proxy providers", () => {
const samples = [
"上下文过长",

View File

@@ -105,6 +105,9 @@ export function isContextOverflowError(errorMessage?: string): boolean {
(lower.includes("max_tokens") && lower.includes("exceed") && lower.includes("context")) ||
(lower.includes("input length") && lower.includes("exceed") && lower.includes("context")) ||
(lower.includes("413") && lower.includes("too large")) ||
// Anthropic API and OpenAI-compatible providers (e.g. ZhipuAI/GLM) return this stop reason
// when the context window is exceeded. pi-ai surfaces it as "Unhandled stop reason: model_context_window_exceeded".
lower.includes("context_window_exceeded") ||
// Chinese proxy error messages for context overflow
errorMessage.includes("上下文过长") ||
errorMessage.includes("上下文超出") ||