fix(ai): detect Z.AI CN endpoint context overflow errors

The CN endpoint returns code 1261 with "Prompt exceeds max length"
instead of "Prompt too long", which isContextOverflow() did not match.

fixes #10208
This commit is contained in:
David Brailovsky 2026-09-30 10:51:10 +02:00
parent 3e94512383
commit 3dd803d7e7
3 changed files with 12 additions and 1 deletions

View file

@ -6,6 +6,10 @@
- Added the lightweight `@earendil-works/pi-ai/models` entry point for model collections and provider construction without loading TypeBox, built-in catalogs, or provider SDKs.
### Fixed
- Fixed context overflow detection for Z.AI CN endpoint `Prompt exceeds max length` errors ([#10208](https://github.com/earendil-works/pi/issues/10208))
## [0.99.1] - 2026-09-29
### Added

View file

@ -27,7 +27,7 @@ import type { AssistantMessage } from "../types.ts";
* - DS4: "Prompt has X tokens, but the configured context size is Y tokens"
* - Cerebras: "400/413 status code (no body)"
* - Mistral: "Prompt contains X tokens ... too large for model with Y maximum context length"
* - z.ai: `{"code":"1261","message":"Prompt too long"}` or silent overflow via usage.input > contextWindow
* - z.ai: `{"code":"1261","message":"Prompt too long"}`, `{"code":"1261","message":"Prompt exceeds max length"}` (CN endpoint), or silent overflow via usage.input > contextWindow
* - Xiaomi MiMo: Truncates input to fill contextWindow exactly, then returns finish_reason "length"
* with output=0 (no room left to generate). Detected via stopReason "length" + zero output +
* input filling the context window.
@ -36,6 +36,7 @@ import type { AssistantMessage } from "../types.ts";
*/
const OVERFLOW_PATTERNS = [
/prompt (?:is )?too long/i, // Anthropic and z.ai token overflow
/prompt exceeds max length/i, // z.ai CN endpoint token overflow
/request_too_large/i, // Anthropic request byte-size overflow (HTTP 413)
/input is too long for requested model/i, // Amazon Bedrock
/exceeds the context window/i, // OpenAI (Completions & Responses API)

View file

@ -41,6 +41,12 @@ describe("isContextOverflow", () => {
expect(isContextOverflow(message, 1048576)).toBe(true);
});
it("detects z.ai CN endpoint prompt-exceeds-max-length errors", () => {
// Regression for #10208.
const message = createErrorMessage('400 {"code":"1261","message":"Prompt exceeds max length"}', "zai");
expect(isContextOverflow(message, 1048576)).toBe(true);
});
it("detects Together AI context length errors", () => {
const message = createErrorMessage(
"400 The input (516368 tokens) is longer than the model's context length (262144 tokens).",