Skip to content

mapStopReason throws on unknown Gemini FinishReason (e.g. MALFORMED_RESPONSE) #2028

Closed

Description

@BackToCimaCoppi

What happened?

Bug

mapStopReason() in packages/ai/src/providers/google-shared.ts uses an exhaustive switch over FinishReason. When the Gemini API returns a finish reason not in the @google/genai SDK's enum (e.g. MALFORMED_RESPONSE), the value reaches the default branch and throws:

Unhandled stop reason: MALFORMED_RESPONSE

This crashes the active agent run. In OpenClaw's Telegram integration, it makes the bot unresponsive until the session history is manually cleared and the gateway restarted.

Root cause

google-shared.ts L~280:

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

The FinishReason enum in @google/genai doesn't include MALFORMED_RESPONSE, but the Gemini API returns it. The exhaustive check assumes all cases are covered -- they aren't when the API evolves faster than the SDK.

Proposed fix

Replace the throw with a graceful fallback:

default: {
    console.warn(`[pi-ai] Unknown finish reason: ${reason}, treating as error`);
    return "error" as StopReason;
}

This is consistent with mapStopReasonString() directly below, which already returns "error" for unknown reasons.

I've validated this fix in production (OpenClaw + Gemini 3 Flash Preview on GCE). Happy to submit a PR once approved.

Steps to reproduce

  1. Use OpenClaw v2026.3.8 with google/gemini-3-flash-preview via AI Studio
  2. Have a cron systemEvent inject a reminder into an active conversation
  3. The model responds with finishReason: "MALFORMED_RESPONSE"
  4. Agent run fails with Unhandled stop reason: MALFORMED_RESPONSE
  5. All subsequent messages in the same session also fail (corrupted conversation history)

Expected behavior

Graceful error handling when Gemini API returns an unknown finish reason. The agent should log a warning and treat it as a generic error instead of throwing and crashing the runtime.

Version

pi-ai @ latest (source check on packages/ai/src/providers/google-shared.ts)

Activity

  1. badlogic commented on Mar 10, 2026

    @badlogic
    Collaborator

    that exception being thrown does not corrupt session. the exception is caught in the provider code, then an assistant message is returned with stopReason "error", which behaves like any other errornous provider response. pi coding agent sessions are not corrupted by this. if openclaw sessions get corrupted, then that's an openclaw problem.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingpossibly-openclaw-clankerUser has activity on openclaw/openclaw

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions