Skip to content

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

@heimoshuiyu

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

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingcoreAnything pertaining to core functionality of the application (opencode server stuff)

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions