Skip to content

fix(provider): map model_context_window_exceeded to length finish reason#18918

Closed
kevinWangSheng wants to merge 1 commit intoanomalyco:devfrom
kevinWangSheng:fix/context-window-exceeded-finish-reason
Closed

fix(provider): map model_context_window_exceeded to length finish reason#18918
kevinWangSheng wants to merge 1 commit intoanomalyco:devfrom
kevinWangSheng:fix/context-window-exceeded-finish-reason

Conversation

@kevinWangSheng
Copy link
Contributor

Issue for this PR

Closes #18813

Type of change

  • Bug fix

What does this PR do?

Some providers (Anthropic via z.ai, zhipuai) return finish_reason: "model_context_window_exceeded" when the prompt exceeds the model's context window. This finish reason was not mapped in the two finish-reason mapping functions, causing it to fall through to "unknown".

When the session loop sees "unknown", it retries indefinitely with exponential backoff instead of triggering compaction — burning tokens and money in an infinite loop.

This PR maps "model_context_window_exceeded" to "length" in both:

  1. map-openai-compatible-finish-reason.ts (Chat API path)
  2. map-openai-responses-finish-reason.ts (Responses API path)

This routes it into the existing context overflow / compaction logic, matching the semantics of "length" (prompt too long).

How did you verify your code works?

Screenshots / recordings

N/A — finish reason mapping change

Checklist

  • I have tested my changes locally
  • I have not included unrelated changes in this PR

Some providers (Anthropic via z.ai, zhipuai) return
finish_reason: "model_context_window_exceeded" when the prompt exceeds
the model's context window. This was falling through to "unknown",
causing the session loop to retry indefinitely instead of triggering
compaction.

Map it to "length" so it flows into the existing context overflow /
compaction logic.

Fixes anomalyco#18813
@rekram1-node
Copy link
Collaborator

Closing because PR is not applicable, u did not read the readme for the files u touched.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

z.ai/zhipuai finish_reason: "model_context_window_exceeded" is treated as unknown, causing loop instead of stopping

2 participants