Skip to content

[Bug]: Uppercase finish_reason from non-OpenAI providers causes "Unhandled stop reason" error` #15311

@Pter61

Description

@Pter61

Summary

When using OpenAI-compatible proxy endpoints that route to Google Gemini models, the finish_reason field is returned as uppercase ("STOP", "MAX_TOKENS") instead of the OpenAI-standard lowercase ("stop", "max_tokens"). OpenClaw treats these as errors due to case-sensitive matching.

Steps to reproduce

  1. Configure a Gemini model (e.g., gemini-3-pro-preview) via an OpenAI-compatible proxy endpoint
  2. Send any chat completion request
  3. Observe the response is rejected with Unhandled stop reason: STOP

Expected behavior

OpenClaw should normalize finish_reason to lowercase before matching, so "STOP" is treated the same as "stop".

Actual behavior

The response content is valid, but OpenClaw rejects it:

                                                                                                                                                                                                                                                                               
Unhandled stop reason: STOP                                                                                                                                                                                                                                                    
                                                                                                                                                                                                                                                                               

Raw API response from the proxy:

{"choices":[{"message":{"role":"assistant","content":"Hi!"},"index":0,"finish_reason":"STOP"}]}                                                                                                                                                                              

Environment

  • Clawdbot version: 2026.2.13
  • OS: macOS Darwin 24.3.0 (arm64)
  • Install method: pnpm

Logs or screenshots

Affected values observed:

  • STOP → should map to stop
  • MAX_TOKENS → should map to max_tokens
  • Potentially other Google-native values: SAFETY, RECITATION, etc.

Suggested fix:

  const reason = rawFinishReason?.toLowerCase?.() ?? rawFinishReason;                                                                                                                                                                                                          

Models affected: gemini-3-pro-preview, gemini-3-flash-preview, gemini-2.5-pro-06-17, gemini-2.5-flash-06-17

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingstaleMarked 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