Skip to content

fix(gemini): accept snake_case function_call/function_response parts - #662

Merged
jpr5 merged 1 commit into
mainfrom
fix/gemini-snake-case-function-parts
Dec 3, 2026
Merged

jpr5 merged 1 commit into
mainfrom
fix/gemini-snake-case-function-parts

Conversation

@jpr5

@jpr5 jpr5 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

Fixes #658

The Gemini API takes the proto field names in snake_case as well as camelCase, and LiteLLM's Gemini / Vertex AI transformation (so Google ADK agents on LiteLlm) sends tool calls and results as function_call / function_response. The request parser in src/gemini.ts only looked at functionCall / functionResponse, so a snake_case tool result turned into an empty user turn and hasToolResult / toolResultContains / userMessage fixtures didn't match (404).

The fix maps function_call / function_response onto the camelCase names before the parts are read, in both the user and model branches of geminiToCompletionRequest. A part that already uses camelCase is returned as the same object, so camelCase requests behave exactly as before. If a part somehow has both spellings, camelCase wins.

Red / green

New test src/__tests__/gemini-snake-case-parts.test.ts starts a real aimock server with the fixture from the issue and sends the issue's three-turn request to both streamGenerateContent?alt=sse and generateContent, once with snake_case parts and once with camelCase (control).

Before the fix (unmodified main):

× ... > streamGenerateContent matches a tool-result fixture with snake_case parts
  → expected 404 to be 200 // Object.is equality
× ... > generateContent matches a tool-result fixture with snake_case parts
  → expected 404 to be 200 // Object.is equality
✓ ... > streamGenerateContent matches a tool-result fixture with camelCase (control) parts
✓ ... > generateContent matches a tool-result fixture with camelCase (control) parts
Tests  2 failed | 2 passed (4)

After the fix:

✓ ... > streamGenerateContent matches a tool-result fixture with snake_case parts
✓ ... > generateContent matches a tool-result fixture with snake_case parts
✓ ... > streamGenerateContent matches a tool-result fixture with camelCase (control) parts
✓ ... > generateContent matches a tool-result fixture with camelCase (control) parts
Tests  4 passed (4)

I also ran the issue's repro (empty fixtures, fixture added through /__aimock/fixtures, then the streaming request) against the CLI:

published 1.49.0:   snake_case: HTTP 404 (no ANSWER)   camelCase: HTTP 200 (ANSWER in body)
this branch (dist): snake_case: HTTP 200 (ANSWER in body)   camelCase: HTTP 200 (ANSWER in body)

Not in this PR

  • Gemini Live (src/ws-gemini-live.ts) reads the same part keys, but a snake_case Live client would also send client_content / tool_response / function_responses at the message level, which that handler doesn't read either. Fixing only the parts there wouldn't help anyone, so I left Live alone.
  • The issue also mentions inline_data / file_data. The request parser doesn't read inlineData / fileData either, so both spellings are already treated the same way and there is nothing to fix for matching.

@pkg-pr-new

pkg-pr-new Bot commented Dec 3, 2026 •

Copy link
Copy Markdown

Open in StackBlitz

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

commit: b80d5a7

The Gemini API accepts proto field names in snake_case, and LiteLLM's
Gemini/Vertex transformation sends tool calls and results that way.
The request parser only read functionCall/functionResponse, so a
snake_case tool result became an empty user turn and tool-result
fixtures did not match (404). Map the snake_case keys onto the
camelCase names before parsing; camelCase parts pass through
unchanged.

Fixes #658
@jpr5
jpr5 force-pushed the fix/gemini-snake-case-function-parts branch from 4494be7 to b80d5a7 Compare December 3, 2026 23:33
@jpr5
jpr5 merged commit 7c04cf0 into main Dec 3, 2026
30 checks passed
@jpr5
jpr5 deleted the fix/gemini-snake-case-function-parts branch December 3, 2026 23:44
jpr5 added a commit that referenced this pull request Dec 4, 2026
#665)

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 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.

Gemini: snake_case function_call / function_response parts are ignored, so tool-result fixtures don't match

1 participant