You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
mapStopReason throws on unknown Gemini FinishReason (e.g. MALFORMED_RESPONSE) #2028
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.
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"asStopReason;}
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
Use OpenClaw v2026.3.8 with google/gemini-3-flash-preview via AI Studio
Have a cron systemEvent inject a reminder into an active conversation
The model responds with finishReason: "MALFORMED_RESPONSE"
Agent run fails with Unhandled stop reason: MALFORMED_RESPONSE
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)
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.
Activity
badlogic commentedon Mar 10, 2026
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.