fix: map model_context_window_exceeded to length to prevent unknown loop#18814
Closed
heimoshuiyu wants to merge 2 commits intoanomalyco:devfrom
Closed
fix: map model_context_window_exceeded to length to prevent unknown loop#18814heimoshuiyu wants to merge 2 commits intoanomalyco:devfrom
heimoshuiyu wants to merge 2 commits intoanomalyco:devfrom
Conversation
Contributor
|
Thanks for updating your PR! It now meets our contributing guidelines. 👍 |
Author
|
oh no just realized this is a problem of z.ai or |
3 tasks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Issue for this PR
Closes #18813
Related: #15778
Type of change
What does this PR do?
Issue #18813 reports that some providers can return a non-standard finish reason:
finish_reason: "model_context_window_exceeded"From the report, z.ai returns this when context is too long; Anthropic appears to have introduced the same value (based on internet search, not directly verified via Anthropic API in this PR).
OpenCode session flow treats
finish: "unknown"as unfinished. That can keep the loop running and repeatedly send requests.This PR adds a guard in session processing for all models using
@ai-sdk/openai-compatible:finishReason === "unknown"then OpenCode rewrites that finish reason to
length.This prevents the unknown-loop behavior described in #18813 and routes the result into normal length/context-limit handling.
(Additionally, the existing Copilot-specific mapper already handles
model_context_window_exceededdirectly.)Reference implementation:
How did you verify your code works?
bun test test/provider/copilot/copilot-chat-model.test.tsbun test test/session/llm.test.tsScreenshots / recordings
N/A (non-UI change). See screenshots and payload examples in #18813.
Checklist