Skip to content

fix(llm-mapper): map Gemini STOP with function calls to tool_calls finish_reason - #5868

Open
pcbeingused333 wants to merge 1 commit into
Helicone:mainfrom
pcbeingused333:fix/google-finish-reason-tool-calls
Open

pcbeingused333 wants to merge 1 commit into
Helicone:mainfrom
pcbeingused333:fix/google-finish-reason-tool-calls

Conversation

@pcbeingused333

@pcbeingused333 pcbeingused333 commented Nov 7, 2026 •

Copy link
Copy Markdown

Ticket

No existing issue; found while reading the Google → OpenAI mapper.

Component/Service

  • Web (Frontend)
  • Jawn (Backend)
  • Worker (Proxy)
  • Bifrost (Marketing)
  • AI Gateway
  • Packages
  • Infrastructure/Docker
  • Documentation

Type of Change

  • Bug fix
  • New feature
  • Breaking change
  • Documentation update
  • Performance improvement
  • Refactoring

Deployment Notes

  • No special deployment steps required
  • Database migrations need to run
  • Environment variable changes required
  • Coordination with other teams needed

Extra Notes

Root cause: Gemini reports finishReason: "STOP" even when the turn ended with functionCall parts. mapGoogleFinishReason only mapped the legacy FUNCTION_CALL reason to tool_calls, so the Gemini → OpenAI conversion returned finish_reason: "stop" next to tool_calls, and OpenAI clients that branch on finish_reason === "tool_calls" never run the tool. The Anthropic mapper already handles this case.

Fix: mapGoogleFinishReason takes an optional hasToolCalls flag; STOP becomes tool_calls when the candidate has tool calls. Applied in both the non-streaming converter and the streaming converter (using the existing toolCallIndex). MAX_TOKENS, safety, etc. are unchanged.

Test: packages/__tests__/llm-mapper/google-finish-reason.test.ts covers non-streaming and streaming with and without tool calls, plus MAX_TOKENS with tool calls staying length. Two of its cases fail without the fix.


This PR was prepared with AI assistance (Claude Code) and reviewed before submission.


Note

Low Risk
Localized mapper fix with regression tests; no auth, data, or API surface changes beyond correct finish_reason for Gemini tool responses.

Overview
Fixes Gemini → OpenAI mapping when a turn ends with functionCall parts but Google reports finishReason: "STOP". Clients that only run tools when finish_reason === "tool_calls" were previously getting stop alongside tool_calls and skipping execution.

mapGoogleFinishReason now accepts optional hasToolCalls. For STOP, it returns tool_calls when tool calls are present and stop otherwise; other reasons (e.g. MAX_TOKENS → length) are unchanged.

Both non-streaming (toOpenAI / candidate mapping) and streaming (GoogleToOpenAIStreamConverter, using accumulated toolCallIndex) pass whether tool calls were emitted.

Adds google-finish-reason.test.ts for non-streaming and streaming cases with and without tool calls, including MAX_TOKENS still mapping to length when tools are present.

Reviewed by Cursor Bugbot for commit 58246a4. Bugbot is set up for automated code reviews on this repo. Configure here.

…nish_reason

Gemini returns finishReason STOP even when the turn ended with functionCall
parts, so the gateway reported finish_reason 'stop' to OpenAI clients that
rely on 'tool_calls' to run tools. Map STOP to tool_calls when the candidate
has tool calls, in both the non-streaming and streaming converters.
@vercel

vercel Bot commented Nov 7, 2026

Copy link
Copy Markdown

@pcbeingused333 is attempting to deploy a commit to the Helicone Team on Vercel.

A member of the Team first needs to authorize it.

This branch has not been deployed

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant