Skip to content

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

Merged
jpr5 merged 3 commits into
mainfrom
fix/gemini-finish-reason-and-snake-case
Dec 4, 2026
Merged

jpr5 merged 3 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: 00f31a1

The write-fixtures skill's finishReason row listed only the explicit
override mappings, which read as if a Gemini tool-call turn defaults
to FUNCTION_CALL. Say that with no override, Gemini and Vertex AI
tool-call turns end with STOP, and that an explicit "tool_calls" still
gives FUNCTION_CALL.

Prettier re-pads the table, so the other rows change only in spacing.
@jpr5
jpr5 merged commit d053146 into main Dec 4, 2026
30 checks passed
@jpr5
jpr5 deleted the fix/gemini-finish-reason-and-snake-case branch December 4, 2026 02:28
@jpr5 jpr5 mentioned this pull request Dec 4, 2026
jpr5 added a commit that referenced this pull request Dec 4, 2026
**Merging this publishes @copilotkit/aimock 1.49.1 to npm and
aimock-pytest 0.7.5 to PyPI.**

## [1.49.1] - 2026-12-03

### Changed

- Gemini/Vertex tool-call turns now end with `STOP`, not
`FUNCTION_CALL`, unless overridden (#665)
- `aimock-pytest` 0.7.5 defaults to `@copilotkit/aimock` 1.49.1

### Fixed

- OpenRouter streams and records reasoning as `delta.reasoning` +
`reasoning_details` (#663, #664)
- Gemini matches tool results sent as snake_case
`function_call`/`function_response` parts (#662)
- Gemini reads snake_case `system_instruction`, `generation_config`,
`function_declarations` (#665)

Bug fixes only, so this is a patch release. Heads up on the first line:
a Gemini or Vertex AI tool-call turn used to end with `finishReason:
"FUNCTION_CALL"` and now ends with `"STOP"`, which is what real Gemini
sends. A test that asserts `FUNCTION_CALL` will now see `STOP`. A
fixture that sets `finishReason: "tool_calls"` still gets
`FUNCTION_CALL`.

#660 and #661 also merged since 1.49.0. They only change tests, so they
are not in the CHANGELOG.

## Docs

Checked each change against the docs, the README, the write-fixtures
skill and the aimock-pytest README.

- `docs/gemini/index.html`: added a line after the tool-call example
saying the turn ends with `STOP` and how to get `FUNCTION_CALL`. The
Request Translation section now says snake_case request keys are read as
their camelCase names.
- `docs/openrouter-chat/index.html`: the Streaming section now says a
reasoning delta carries `reasoning` and `reasoning_details`, and keeps
`reasoning_content`.
- `docs/fixtures/index.html` and `skills/write-fixtures/SKILL.md` were
already updated for `STOP` in #665.
- No change needed: `docs/vertex-ai` (it points to the Gemini wire
format and shows no finish reasons), `docs/record-replay` (it does not
describe how reasoning is recorded), README.md and the aimock-pytest
README (no statement about these behaviors).

## Version carriers (from `git grep -F 1.49.0`)

Bumped to 1.49.1:
- `package.json` version
- `charts/aimock/Chart.yaml` appVersion
- `.claude-plugin/plugin.json` version
- `.claude-plugin/marketplace.json` source version (`^1.49.1`)
- `docs/index.html` banner
- `packages/aimock-pytest/README.md` `--aimock-version` default
- `packages/aimock-pytest/src/aimock_pytest/_version.py`
`AIMOCK_VERSION`

Also bumped: `packages/aimock-pytest/pyproject.toml` 0.7.4 → 0.7.5,
because the pin moved (same as 1.49.0).

Left as is: the `## [1.49.0]` CHANGELOG section (its heading and the
0.7.4 line).

## CHANGELOG edits

- `## [Unreleased]` becomes `## [1.49.1] - 2026-12-03`.
- The two OpenRouter lines (#663, #664) are now one. The `STOP` line
moved to `Changed`, because it changes what tests see. Added the
aimock-pytest `Changed` line.

## Checks

prettier, eslint, `pnpm run typecheck`, `pnpm build` and `pnpm
api:report` (no `etc/` change) all pass. commitlint passes.
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