Skip to content

[Bug]: Gemini MALFORMED_RESPONSE finish reason crashes agent run (pi-ai upstream bug) #42149

Closed

Description

@BackToCimaCoppi

Bug type

Regression (worked before, now fails)

Summary

When using Gemini 3 Flash Preview, the API occasionally returns finishReason: "MALFORMED_RESPONSE". This value is not handled by @mariozechner/pi-ai's mapStopReason() function in google-shared.ts, causing it to throw Unhandled stop reason: MALFORMED_RESPONSE.

This crashes the active agent run. In Telegram DM mode, all subsequent messages in the same session also fail because the corrupted response is persisted in the session history.

Environment

  • OpenClaw: v2026.3.8
    • Model: google/gemini-3-flash-preview via AI Studio (GEMINI_API_KEY)
      • Channel: Telegram
        • OS: Ubuntu 24.04 on GCE (e2-medium)

Upstream

Filed as an issue in the pi-mono repo: earendil-works/pi#2028

Workaround

Hot-patch node_modules/@mariozechner/pi-ai/dist/providers/google-shared.js: replace the throw in the default branch of mapStopReason() with return "error".

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

The agent should gracefully handle unknown finish reasons by treating them as generic errors, instead of throwing and crashing the runtime.

Actual behavior

This crashes the active agent run. In Telegram DM mode, all subsequent messages in the same session also fail because the corrupted response is persisted in the session history.

OpenClaw version

v2026.3.8

Operating system

Ubuntu 24.04 on GCE (e2-medium)

Install method

npm global

Model

google/gemini-3-flash-preview

Provider / routing chain

openclaw -> AI Studio

Config file / key location

~/.openclaw/openclaw.json

Additional provider/model setup details

Using GEMINI_API_KEY directly with AI Studio. No proxy or routing middleware.

Logs, screenshots, and evidence

Impact and severity

Affected: All users using Gemini preview models via AI Studio
Severity: High (crashes agent, blocks all replies until manual intervention)
Frequency: Intermittent (triggered by specific API responses)
Consequence: Bot becomes unresponsive, requires manual session cleanup and gateway restart

Additional information

Upstream issue filed: earendil-works/pi#2028
Workaround: Hot-patch node_modules/@mariozechner/pi-ai/dist/providers/google-shared.js - replace throw in mapStopReason default branch with return "error"

Activity

  1. locked as resolved and limited conversation to collaborators on Mar 13, 2026
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 workingregressionBehavior that previously worked and now fails

    Type

    No type

    Fields

    Priority

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions