Skip to content

fix(gemini): STOP on tool-call turns; accept snake_case request fields - #665

Open
jpr5 wants to merge 2 commits into
mainfrom
fix/gemini-finish-reason-and-snake-case
Open

jpr5 wants to merge 2 commits into
mainfrom
fix/gemini-finish-reason-and-snake-case

Conversation

@jpr5

@jpr5 jpr5 commented Dec 4, 2026

Copy link
Copy Markdown
Contributor

Two small fixes to the Gemini handler in src/gemini.ts. They're in one PR because both touch that file; each is its own commit. Related to #658.

1. Tool-call turns end with finishReason: "STOP"

Real Gemini ends a function-call turn with STOP. Its FinishReason enum has no FUNCTION_CALL value, and the live drift reader in src/__tests__/drift/gemini.drift.ts doesn't list one either. The mock sent FUNCTION_CALL by default on every tool-call path: tool-only, content+toolCalls, blocks, and audio turns with a tool call, on generateContent and streamGenerateContent, for both Gemini and Vertex AI. All of those now default to STOP.

An explicit fixture finishReason still wins and maps exactly as before: "FUNCTION_CALL" passes through, and "tool_calls" still maps to FUNCTION_CALL (mapGeminiFinish is untouched). Gemini Live doesn't send a finishReason, so it's unchanged.

Tests that pinned the old default now expect STOP. None were deleted. The gemini-sse frame-codec goldens were regenerated, and only the finishReason value changed. docs/fixtures gets one sentence about the default.

Red (unmodified main), with src/__tests__/gemini-tool-call-finish-reason.test.ts against a real server on port 0:

   × gemini generateContent: tool-only fixture defaults to STOP
   × gemini generateContent: content-and-tool fixture defaults to STOP
   × gemini generateContent: blocks-tool-last fixture defaults to STOP
   ✓ gemini generateContent: explicit finishReason "FUNCTION_CALL" is kept
   ✓ gemini generateContent: explicit finishReason "tool_calls" keeps its existing mapping
   ✓ gemini generateContent: explicit finishReason "SAFETY" is kept
   × gemini streamGenerateContent (last chunk): tool-only fixture defaults to STOP
   × gemini streamGenerateContent (last chunk): content-and-tool fixture defaults to STOP
   × gemini streamGenerateContent (last chunk): blocks-tool-last fixture defaults to STOP
   ✓ gemini streamGenerateContent (last chunk): explicit finishReason "FUNCTION_CALL" is kept
   ✓ gemini streamGenerateContent (last chunk): explicit finishReason "tool_calls" keeps its existing mapping
   ✓ gemini streamGenerateContent (last chunk): explicit finishReason "SAFETY" is kept
   × vertex generateContent: tool-only fixture defaults to STOP
   × vertex generateContent: content-and-tool fixture defaults to STOP
   × vertex generateContent: blocks-tool-last fixture defaults to STOP
   ✓ vertex generateContent: explicit finishReason "FUNCTION_CALL" is kept
   ✓ vertex generateContent: explicit finishReason "tool_calls" keeps its existing mapping
   ✓ vertex generateContent: explicit finishReason "SAFETY" is kept
   × vertex streamGenerateContent (last chunk): tool-only fixture defaults to STOP
   × vertex streamGenerateContent (last chunk): content-and-tool fixture defaults to STOP
   × vertex streamGenerateContent (last chunk): blocks-tool-last fixture defaults to STOP
   ✓ vertex streamGenerateContent (last chunk): explicit finishReason "FUNCTION_CALL" is kept
   ✓ vertex streamGenerateContent (last chunk): explicit finishReason "tool_calls" keeps its existing mapping
   ✓ vertex streamGenerateContent (last chunk): explicit finishReason "SAFETY" is kept
      Tests  12 failed | 12 passed (24)

Every failure was expected 'FUNCTION_CALL' to be 'STOP'. The 12 passing tests are the explicit-override controls.

Green:

      Tests  24 passed (24)

2. snake_case top-level request fields

#662 handled function_call / function_response parts. The parser still ignored the snake_case top-level keys that LiteLLM-style and Python REST clients send:

  • system_instruction: systemMessage fixtures didn't match
  • tools[].function_declarations (and parameters_json_schema): tools were dropped, so toolName fixtures didn't match
  • generation_config with max_output_tokens / top_p / top_k: the request's max_tokens and sampling values were lost

normalizeGeminiRequest copies these onto their camelCase names when the camelCase key is absent. camelCase wins if both are set, and a camelCase request comes back as the same object. It runs before body validation on generateContent and streamGenerateContent (Gemini and Vertex AI), and in countTokens, which also accepts a generate_content_request wrapper. Record mode still proxies the raw body. Gemini Live is out of scope.

The parser doesn't read toolConfig, stopSequences, responseMimeType, thinkingConfig, or inlineData / fileData in request parts, so those aren't mapped.

Red (unmodified main), with src/__tests__/gemini-snake-case-request.test.ts against a real server on port 0. It covers all four routes plus countTokens. These are the generateContent and countTokens rows; the stream and Vertex rows fail the same way:

   × generateContent: snake_case system instruction matches a systemMessage fixture
   × generateContent: snake_case function declarations convert to tools
   × generateContent: snake_case generation config sets max_tokens and sampling
   × generateContent: snake_case keys inside camelCase generationConfig are read
   × generateContent: a wrong-typed system_instruction is rejected with 400, not 500
   × countTokens: counts system_instruction and function_declarations like their camelCase forms
   × countTokens: counts a snake_case generate_content_request like generateContentRequest
   × countTokens: rejects a wrong-typed system_instruction with 400
      Tests  23 failed | 16 passed (39)

The 16 passing tests are the camelCase controls and the "camelCase wins" checks.

Green:

      Tests  39 passed (39)

Checks

  • npx vitest run src/__tests__/gemini*.test.ts src/__tests__/vertex*.test.ts: 408 passed
  • countTokens suites (response-count-features, metrics, openapi-catalog, auth/required-headers/model/policy): all passed
  • prettier, eslint, pnpm run typecheck: clean
  • pnpm build && pnpm api:report: no etc/ change
  • Full suite: 20416 passed, 5 failed. The failures were in response-errors-routes and validate-cli, and both files pass on their own (180/180). The machine was heavily loaded.

jpr5 added 2 commits December 3, 2026 17:13
Real Gemini ends a function-call turn with finishReason "STOP". Its
FinishReason enum has no "FUNCTION_CALL" value, and the live drift
reader does not list one. The mock sent "FUNCTION_CALL" by default on
every tool-call path: generateContent, streamGenerateContent, the
content+toolCalls and blocks paths, audio turns with a tool call, and
the same paths on Vertex AI.

The default is now "STOP" on all of these paths. An explicit fixture
finishReason still wins and maps as before, so "FUNCTION_CALL" and
"tool_calls" fixtures still produce "FUNCTION_CALL". Gemini Live does
not send a finishReason and is unchanged.

Tests that pinned the old default now expect STOP, and the gemini-sse
frame goldens are regenerated (only the finishReason value changed).
The Gemini REST API accepts proto field names in snake_case as well as
camelCase. PR 662 handled function_call / function_response parts, but
the request parser still ignored the snake_case top-level keys that
LiteLLM-style and Python REST clients send.

- system_instruction, so systemMessage fixtures did not match
- tools[].function_declarations (and parameters_json_schema), so the
  tools were dropped and toolName fixtures did not match
- generation_config and its max_output_tokens / top_p / top_k, so the
  request max_tokens and sampling values were lost

normalizeGeminiRequest maps these onto their camelCase names when the
camelCase key is absent; camelCase wins if both are set, and a
camelCase request is returned as the same object. It runs before body
validation in generateContent / streamGenerateContent (Gemini and
Vertex AI) and in countTokens, which also accepts a snake_case
generate_content_request wrapper. The raw body is still what record
mode proxies upstream. Gemini Live is out of scope.

The parser does not read toolConfig, inlineData or fileData from
request parts, so those keys are not mapped.
@pkg-pr-new

pkg-pr-new Bot commented Dec 4, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/@copilotkit/aimock@665

commit: 4968622

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