Skip to content

fix: backfill empty functionResponse.name from preceding functionCall#2076

Merged
luispater merged 2 commits intorouter-for-me:devfrom
aikins01:fix/backfill-empty-function-response-names
Mar 12, 2026
Merged

fix: backfill empty functionResponse.name from preceding functionCall#2076
luispater merged 2 commits intorouter-for-me:devfrom
aikins01:fix/backfill-empty-function-response-names

Conversation

@aikins01
Copy link
Contributor

@aikins01 aikins01 commented Mar 12, 2026

problem

Claude Code sends functionResponse with empty name fields in Gemini conversation history. The Gemini API strictly requires non-empty names and rejects these requests with:

400 GenerateContentRequest.contents[N].parts[0].function_response.name: Name cannot be empty.

This happens on the second+ request in any thread that uses tool calls (e.g., code search, code review), making Gemini 3/3.1 models unusable in extended conversations.

Fixes #1903

solution

Backfill empty functionResponse.name from the corresponding preceding functionCall.name using positional matching. The fix covers all three Gemini translator paths:

  • gemini/gemini — direct Gemini API key provider
  • antigravity/gemini — OAuth/Antigravity provider
  • gemini-cli/gemini — Gemini CLI provider

Also switches fixCLIToolResponse pending group matching from LIFO to FIFO, correctly handling multiple sequential tool call groups

when Amp or Claude Code sends functionResponse with an empty name in Gemini
conversation history, the Gemini API rejects the request with 400
"Name cannot be empty". this fix backfills empty names from the
corresponding preceding functionCall parts using positional matching.

covers all three Gemini translator paths:
- gemini/gemini (direct API key)
- antigravity/gemini (OAuth)
- gemini-cli/gemini (Gemini CLI)

also switches fixCLIToolResponse pending group matching from LIFO to
FIFO to correctly handle multiple sequential tool call groups.

fixes router-for-me#1903
@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request resolves a critical compatibility issue preventing certain clients from engaging in extended tool-calling conversations with Gemini models. It introduces a robust mechanism to automatically populate missing function response names, ensuring that API requests conform to Gemini's requirements and enabling seamless interaction for complex, multi-turn operations.

Highlights

  • Problem Addressed: Amp and Claude Code send functionResponse with empty name fields in Gemini conversation history, which the Gemini API strictly rejects, making Gemini models unusable in extended conversations with tool calls.
  • Solution Implemented: Empty functionResponse.name fields are now backfilled from the corresponding preceding functionCall.name using positional matching across all three Gemini translator paths: gemini/gemini, antigravity/gemini, and gemini-cli/gemini.
  • Tool Response Grouping Logic: The fixCLIToolResponse function's pending group matching logic was switched from Last-In, First-Out (LIFO) to First-In, First-Out (FIFO) to correctly handle multiple sequential tool call groups.
  • Testing: Fifteen new tests were added to cover various backfill scenarios, parallel calls, preservation of existing names, handling of excess responses, and FIFO group matching. All existing tests passed, and end-to-end verification was performed with Amp code search and code review using Gemini models through CLIProxyAPI.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • internal/translator/antigravity/gemini/antigravity_gemini_request.go
    • Updated FunctionCallGroup to include CallNames for backfilling.
    • Modified parseFunctionResponseRaw to accept and utilize a fallbackName for empty function response names.
    • Refactored fixCLIToolResponse to switch from LIFO to FIFO processing for pending function call groups and to pass CallNames for backfilling.
  • internal/translator/antigravity/gemini/antigravity_gemini_request_test.go
    • Added TestFixCLIToolResponse_BackfillsEmptyFunctionResponseName to verify single name backfill.
    • Added TestFixCLIToolResponse_BackfillsMultipleEmptyNames to test parallel name backfilling.
    • Added TestFixCLIToolResponse_PreservesExistingName to ensure existing names are not overwritten.
    • Added TestFixCLIToolResponse_MoreResponsesThanCalls to check behavior when responses exceed calls.
    • Added TestFixCLIToolResponse_MultipleGroupsFIFO to confirm correct FIFO matching for sequential groups.
  • internal/translator/gemini-cli/gemini/gemini-cli_gemini_request.go
    • Imported the strings package.
    • Added a new backfillFunctionResponseName helper function.
    • Updated FunctionCallGroup to include CallNames for backfilling.
    • Modified fixCLIToolResponse to switch from LIFO to FIFO processing for pending function call groups and to use backfillFunctionResponseName.
  • internal/translator/gemini/gemini/gemini_gemini_request.go
    • Imported strings and log packages.
    • Introduced backfillEmptyFunctionResponseNames function to handle name backfilling for this translator path.
    • Integrated backfillEmptyFunctionResponseNames into ConvertGeminiRequestToGemini.
  • internal/translator/gemini/gemini/gemini_gemini_request_test.go
    • Added a new test file for gemini_gemini_request.go.
    • Added TestBackfillEmptyFunctionResponseNames_Single to verify single name backfill.
    • Added TestBackfillEmptyFunctionResponseNames_Parallel to test parallel name backfilling.
    • Added TestBackfillEmptyFunctionResponseNames_PreservesExisting to ensure existing names are not overwritten.
    • Added TestConvertGeminiRequestToGemini_BackfillsEmptyName to test end-to-end conversion with backfilling.
    • Added TestBackfillEmptyFunctionResponseNames_MoreResponsesThanCalls to check behavior when responses exceed calls.
    • Added TestBackfillEmptyFunctionResponseNames_MultipleGroups to confirm correct handling of multiple sequential groups.
Activity
  • The pull request was created by aikins01 to address issues with Gemini API requests failing due to empty function response names.
  • The author provided a detailed description of the problem, the proposed solution, and comprehensive testing efforts.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for GitHub and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request effectively addresses the issue of empty functionResponse.name fields from certain clients causing errors with the Gemini API. The solution to backfill the name from the preceding functionCall is implemented across all three Gemini translator paths, ensuring comprehensive coverage. The introduction of extensive unit tests for various scenarios, including single and parallel backfills, name preservation, and handling of multiple groups, is commendable and significantly increases confidence in the fix. Additionally, the switch from LIFO to FIFO for processing tool response groups is a good improvement for handling sequential tool calls correctly. I have a couple of minor suggestions to improve code consistency.

@xkonjin
Copy link

xkonjin commented Mar 12, 2026

Code Review - PR #2076

Summary

This PR adds backfill logic for empty functionResponse.name fields in Gemini tool responses. The change tracks function call names during model turns and injects them into subsequent function response parts where the name is empty.

✅ Strengths

  • Comprehensive test coverage with 5+ new test cases covering single/parallel calls, name preservation, edge cases
  • Consistent implementation across 3 translator paths (antigravity/gemini, gemini-cli, gemini/gemini)
  • Good handling of FIFO ordering for sequential call/response groups

⚠️ Issues Found

1. Unnecessary field allocation in hot path

Location: internal/translator/antigravity/gemini/antigravity_gemini_request.go:247-248
Issue: You're allocating a slice even when there are no function calls. For high-throughput scenarios, this is wasted allocation. Consider:

callNames := make([]string, 0, 4) // Pre-allocate with expected capacity

2. Silent fallback could mask bugs

Location: internal/translator/antigravity/gemini/antigravity_gemini_request.go:172-174
Issue: If sjson.Set fails (returns error), you silently ignore it. While rare, this could lead to malformed JSON reaching the API. Consider at least logging the error.

3. Inconsistent role handling

Location: backfillEmptyFunctionResponseNames() function
The function handles role == "model" and then backfills for the next content block, but it doesn't explicitly check that the next role is user or function. If there's a non-function user message between model and function response, the backfill could be wrong.

Suggestion: Validate that the backfill target is a function-related role.

4. Test coverage gap - malformed input

Tests cover valid JSON structures but don't test malformed inputs where functionResponse.name is missing entirely (not just empty string). While gjson handles this gracefully, explicit test coverage for missing fields would strengthen robustness.

Security Considerations

None identified. The changes are defensive (backfilling required fields) and don't introduce validation bypasses.

Recommendation

Approve with minor improvements

The core logic is sound and well-tested. These are polish/refinement suggestions.

@luispater luispater changed the base branch from main to dev March 12, 2026 02:35
@luispater luispater merged commit 683f370 into router-for-me:dev Mar 12, 2026
1 of 2 checks passed
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.

antigravity gemini-3/3.1的模型不能用,其他可以 - v6.8.45

3 participants