Skip to content

pi-ai: Unhandled stop reason from Z.AI GLM-5 (openai-completions provider) #16252

@AmbitiousRealism2025

Description

@AmbitiousRealism2025

Bug

The mapStopReason function in @mariozechner/pi-ai (providers/openai-completions.js) throws on unrecognized finish_reason values:

default: {
    const _exhaustive = reason;
    throw new Error(`Unhandled stop reason: ${_exhaustive}`);
}

Z.AI's GLM-5 model (OpenAI-compatible API at api.z.ai) returns a non-standard finish_reason value not in the handled set (stop, length, tool_calls, function_call, content_filter). This causes the function to throw even though the model response content streamed successfully.

Impact

Every GLM-5 completion triggers an Unhandled stop reason: abort error visible to the end user. The content still arrives (streams before finish_reason), but the thrown error propagates and gets displayed.

Suggested Fix

Map unknown stop reasons to "stop" instead of throwing:

default: {
    return "stop";
}

Non-standard providers using the OpenAI-compatible API should degrade gracefully.

Workaround

Manually patch the installed file:

sudo sed -i.bak 's/throw new Error(`Unhandled stop reason: ${_exhaustive}`);/return "stop";/' node_modules/@mariozechner/pi-ai/dist/providers/openai-completions.js

Environment

  • @mariozechner/pi-ai v0.52.10
  • Provider: Z.AI GLM-5 (openai-completions API mode)
  • OpenClaw v2026.2.13

Note: badlogic/pi-mono has issues disabled, filing here instead.

Metadata

Metadata

Assignees

No one assigned

    Labels

    staleMarked as stale due to inactivity

    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