Skip to content

fix: return tool_calls finish_reason for Gemini function calls#1534

Open
roh26it wants to merge 2 commits into2.0.0from
fix/gemini-tool-calls-finish-reason
Open

fix: return tool_calls finish_reason for Gemini function calls#1534
roh26it wants to merge 2 commits into2.0.0from
fix/gemini-tool-calls-finish-reason

Conversation

@roh26it
Copy link
Collaborator

@roh26it roh26it commented Feb 21, 2026

Summary

  • Fixes Gemini/Vertex AI returning finish_reason: "stop" instead of "tool_calls" when function calls are present
  • Gemini API returns finishReason: "STOP" even when the response contains function calls (there is no FUNCTION_CALL finish reason in Gemini's API)
  • This fix detects tool calls in the response content and returns finish_reason: "tool_calls" for OpenAI compatibility

Changes

  • Modified GoogleChatCompleteResponseTransform (non-streaming) for both Google and Vertex AI
  • Modified GoogleChatCompleteStreamChunkTransform (streaming) for both Google and Vertex AI

Test plan

  • Verify Gemini tool calls return finish_reason: "tool_calls" in non-streaming mode
  • Verify Gemini tool calls return finish_reason: "tool_calls" in streaming mode
  • Verify regular completions still return appropriate finish reasons

Made with Cursor

Gemini API returns finishReason: "STOP" even when the response contains
function calls. This fix detects tool calls in the response content and
returns finish_reason: "tool_calls" for OpenAI compatibility.

Affects both streaming and non-streaming responses for Google and
Google Vertex AI providers.

Co-authored-by: Cursor <cursoragent@cursor.com>
Copy link
Member

@narengogi narengogi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

would recommend extracting this to google-vertex-ai/utils.ts and also making use of FINISH_REASON.tool_calls instead of hard coding the string

- Add hasToolCalls helper function to google-vertex-ai/utils.ts
- Use FINISH_REASON.tool_calls instead of hardcoded string
- Import and reuse helper in both Google and Vertex AI providers

Co-authored-by: Cursor <cursoragent@cursor.com>
provider: GOOGLE_VERTEX_AI,
choices:
parsedChunk.candidates?.map((generation, index) => {
const containsToolCalls = hasToolCalls(generation.content?.parts);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you'd want to store this variable in streamState and make use of it because the last chunk might not have the tool call in the chunk

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.

2 participants