Skip to content

fix: map model_context_window_exceeded to length to prevent unknown loop#18814

Closed
heimoshuiyu wants to merge 2 commits intoanomalyco:devfrom
heimoshuiyu:fix/model-context-window-finish-reason
Closed

fix: map model_context_window_exceeded to length to prevent unknown loop#18814
heimoshuiyu wants to merge 2 commits intoanomalyco:devfrom
heimoshuiyu:fix/model-context-window-finish-reason

Conversation

@heimoshuiyu
Copy link

@heimoshuiyu heimoshuiyu commented Mar 23, 2026

Issue for this PR

Closes #18813
Related: #15778

Type of change

  • Bug fix
  • New feature
  • Refactor / code improvement
  • Documentation

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:

  • if a step finishes with finishReason === "unknown"
  • and the step produced no text/reasoning/tool output

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_exceeded directly.)

Reference implementation:

How did you verify your code works?

Screenshots / recordings

N/A (non-UI change). See screenshots and payload examples in #18813.

Checklist

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

@github-actions github-actions bot added needs:compliance This means the issue will auto-close after 2 hours. and removed needs:compliance This means the issue will auto-close after 2 hours. labels Mar 23, 2026
@github-actions
Copy link
Contributor

Thanks for updating your PR! It now meets our contributing guidelines. 👍

@heimoshuiyu heimoshuiyu changed the title fix: map model_context_window_exceeded finish reason to length fix: map model_context_window_exceeded to length to prevent unknown loop Mar 23, 2026
@heimoshuiyu heimoshuiyu marked this pull request as draft March 23, 2026 17:21
@heimoshuiyu
Copy link
Author

oh no just realized this is a problem of z.ai or ai sdk, closing this fake pr sorry

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

Labels

None yet

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

1 participant