Skip to content

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

Closed as not planned

Description

@heimoshuiyu

Description

Some providers appear to return a non-standard finish reason:

  • finish_reason: "model_context_window_exceeded"

Based on internet search, this was introduced on Anthropic side, and z.ai seems to follow the same behavior when context is too long.
In OpenCode, this finish reason is currently treated as unknown (instead of length), which can cause the session loop to continue rather than stop/compact, resulting in repeated/infinite loops sending request.

This is one of the repeated assistant message exported from session:

Image
{
  "message": {
    "role": "assistant",
    "time": {
      "created": 1774204420869,
      "completed": 1774204423742
    },
    "parentID": "msg_d16d2a7f7001GyKBIDcFE3dGOY",
    "modelID": "glm-5-turbo",
    "providerID": "zhipuai-coding-plan",
    "mode": "normal",
    "agent": "normal",
    "path": {
      "cwd": "/home/hmsy/workspace",
      "root": "/home/hmsy/workspace"
    },
    "cost": 0,
    "tokens": {
      "input": 0,
      "output": 0,
      "reasoning": 0,
      "cache": {
        "read": 0,
        "write": 0
      }
    },
    "finish": "unknown",
    "id": "msg_d16d30305001Nyw0BNeLsTjxCe",
    "sessionID": "ses_2e9440126ffeOGeX4DKrphB4tT"
  },
  "parts": [
    {
      "type": "step-start",
      "snapshot": "51df3df3638f2d50ef5530301f1f3889bcf12c29",
      "id": "prt_d16d30e2c001TYpfLIMd5SSeof",
      "sessionID": "ses_2e9440126ffeOGeX4DKrphB4tT",
      "messageID": "msg_d16d30305001Nyw0BNeLsTjxCe"
    },
    {
      "type": "step-finish",
      "reason": "unknown",
      "snapshot": "51df3df3638f2d50ef5530301f1f3889bcf12c29",
      "cost": 0,
      "tokens": {
        "input": 0,
        "output": 0,
        "reasoning": 0,
        "cache": {
          "read": 0,
          "write": 0
        }
      },
      "id": "prt_d16d30e2d001V047uR4bXcaluN",
      "sessionID": "ses_2e9440126ffeOGeX4DKrphB4tT",
      "messageID": "msg_d16d30305001Nyw0BNeLsTjxCe"
    }
  ]
}

This is the request and response of z.ai

Image

References:

OpenClaw already handled this case: openclaw/openclaw#35934

Link OpenCode issue: #15778

Expected behavior:

Treat model_context_window_exceeded as length (or equivalent overflow handling).
Stop current generation and trigger normal overflow/compaction flow, instead of continuing with unknown.

Plugins

no

OpenCode version

v1.3.0

Steps to reproduce

No response

Screenshot and/or share link

z.ai model context limit reached

Operating System

Linux

Terminal

Alacritty

Activity

  1. github-actions commented on Mar 23, 2026

    @github-actions
    Contributor

    This issue might be a duplicate of existing issues. Please check:

  2. added
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on Mar 23, 2026
  3. heimoshuiyu commented on Mar 23, 2026

    @heimoshuiyu
    ContributorAuthor

    I think this is a problem of vercel ai sdk or z.ai not follow the standard value.

  4. changed the title [-]Anthropic/z.ai finish_reason: "model_context_window_exceeded" is treated as unknown, causing loop instead of stopping[/-] [+]z.ai/zhipuai finish_reason: "model_context_window_exceeded" is treated as unknown, causing loop instead of stopping[/+] on Mar 24, 2026
  5. heimoshuiyu commented on Mar 24, 2026

    @heimoshuiyu
    ContributorAuthor

    The z.ai development team stated that model_context_window_exceeded means that the validation exceeded the allowed length before the model start processing, resulting in an error. length means that the output was incomplete. They believe their approach is correct.

  6. added a commit that references this issue on Mar 24, 2026
    cf73381
  7. okuyam2y commented on Mar 26, 2026

    @okuyam2y

    Fix PR: #19342 — removes "unknown" from the non-terminal finish reason list so the prompt loop exits correctly.

  8. removed
    bugSomething isn't working
    coreAnything pertaining to core functionality of the application (opencode server stuff)
    on May 3, 2026
  9. github-actions commented on Jul 3, 2026

    @github-actions
    Contributor

    To stay organized issues are automatically closed after 60 days of no activity. If the issue is still relevant please open a new one.

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions