When using z.ai endpoints with GLM 4.7 or GLM 5 models, the LLM sometimes returns garbled outputs—random tokens that are unusable. The same endpoints and LLM work correctly in other harnesses like CloudCode CLI, suggesting the issue is specific to OpenCode's handling of these models.
After investigation, the issue appears to be related to the thinking parameter configuration in transform.ts:
// packages/opencode/src/provider/transform.ts:746-751
if (["zai", "zhipuai"].includes(input.model.providerID) && input.model.api.npm === "@ai-sdk/openai-compatible") {
result["thinking"] = {
type: "enabled",
clear_thinking: false,
}
}-
clear_thinking: falsemay cause instability - This setting tells the API not to clear thinking content, but may interfere with the model's token generation, causing garbled output. -
Inconsistent
interleavedcapability configuration - Onlyglm-4.7has"interleaved": { "field": "reasoning_content" }configured in the model definitions, while other reasoning-capable GLM models (glm-4.5-flash, glm-4.5, glm-4.7-flash, glm-4.5-air, glm-4.6, glm-4.6v) are missing this capability. This means reasoning content in assistant messages won't be properly serialized for multi-turn conversations. -
Parameter format differs from similar providers - Other providers use simpler formats:
- alibaba-cn:
{ enable_thinking: true } - baseten:
{ chat_template_args: { enable_thinking: true } }
- alibaba-cn:
- Configure z.ai provider with GLM 4.7 or GLM 5 model
- Start a conversation with the model
- After several turns (especially with reasoning content), observe garbled/random tokens in the output
GLM models should produce coherent output consistent with behavior in other clients (CloudCode CLI, etc.)
-
Simplify the thinking parameter - Try removing
clear_thinking: false:result["thinking"] = { type: "enabled", }
-
Add
interleavedcapability to all reasoning GLM models - Update model definitions to include:"interleaved": { "field": "reasoning_content" }
-
Consider testing without the thinking parameter - Temporarily disable to confirm this is the cause
- OpenCode version: current dev branch
- Affected models: GLM 4.7, GLM 5 (and potentially other z.ai reasoning models)
- Provider: z.ai / zhipuai
packages/opencode/src/provider/transform.ts(thinking parameter configuration)packages/opencode/test/tool/fixtures/models-api.json(model capabilities)
Investigation also covered:
- The zhipuai Python SDK (v2.1.5) which shows the API accepts a
thinkingparameter but doesn't define a specific schema - The AI SDK's openai-compatible provider which correctly handles
reasoning_contentin streaming responses - The flow of
providerOptionsthrough the AI SDK which correctly passes thethinkingparameter to the API