Skip to content

Conversation

@ProdByBuddha
Copy link

Summary

Prevents 400 INVALID_ARGUMENT errors by sanitizing function_declaration names in the tool schema to meet Gemini’s naming constraints:

  • Must start with a letter (A-Z, a-z) or underscore (_)
  • Allowed characters thereafter: letters, digits (0-9), underscore (_), dot (.), colon (:), dash (-)
  • Maximum length: 64 characters
    Sanitization is applied both during discovery (prefixed tool names) and immediately prior to sending the schema to the API. Diagnostic logs are emitted when a name is altered.

Details

This change introduces:

  • A NAME_RE regex and sanitizeName() helper to enforce naming rules.
  • Mutation of tool names at registration/discovery time to avoid invalid names reaching the API.
  • Updates to getFunctionDeclarations() and getFunctionDeclarationsFiltered() so schema names are sanitized just before submission.
  • Retains original names internally for execution but exposes validated names to the model/LLM interface.
  • Console logging when sanitization occurs, enabling easier detection of upstream tool-name issues.

Related Issues

Closes #12869
Closes #10663
Closes #5468
Closes #5270

  • more...

How to Validate

  1. Build the package:

    npm ci
    npm run build
  2. Link locally (optional):

    npm link
  3. Run a sample tool discovery which registers names with invalid characters (e.g., leading digits, spaces, slashes).

  4. Inspect logs for entries like:

    [Gemini CLI] Sanitized invalid function name: { original: "123tool/run", sanitized: "_23tool.run" }

  5. Trigger a conversation or call using the sanitized tool. The API should succeed, no longer returning a 400 INVALID_ARGUMENT error.

  6. Confirm valid tool names (that already met naming rules) remain unchanged.

  7. Edge cases:

    • A tool name longer than 64 characters is truncated.
    • A tool name starting with a digit or symbol gets transformed to start with an underscore.
    • Tool names containing spaces or special characters get them replaced with underscores.
Screen Shot 2025-11-12 at 4 36 15 AM Screen Shot 2025-11-12 at 4 37 30 AM Screen Shot 2025-11-12 at 4 39 16 AM Screen Shot 2025-11-12 at 4 40 38 AM Screen Shot 2025-11-12 at 4 41 05 AM

Signed-off-by: Billy Coleman III thebuddhaverse@icloud.com

@ProdByBuddha ProdByBuddha requested a review from a team as a code owner November 12, 2025 12:59
@google-cla
Copy link

google-cla bot commented Nov 12, 2025

Thanks for your pull request! It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

View this failed invocation of the CLA check for more information.

For the most up to date status, view the checks section at the bottom of the pull request.

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @ProdByBuddha, 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 addresses a critical compatibility issue with the Gemini API by implementing robust sanitization for tool function names. By ensuring that all function_declaration names conform to Gemini's specific naming conventions before being sent to the API, it effectively eliminates 400 INVALID_ARGUMENT errors and improves the reliability of tool interactions. The changes introduce a dedicated sanitization utility and integrate it at key points in the tool registration and declaration retrieval process, while also providing clear diagnostic feedback when names are modified.

Highlights

  • API Compatibility: Ensures function_declaration names adhere to Gemini API constraints, preventing 400 INVALID_ARGUMENT errors by sanitizing names before submission.
  • Name Sanitization Logic: Introduces a sanitizeName() helper function and NAME_RE regex to enforce naming rules (start with letter/underscore, allowed characters, max 64 length).
  • Dual-Phase Sanitization: Applies sanitization both during tool discovery (for prefixed names) and immediately prior to sending the schema to the Gemini API.
  • Internal Name Preservation: Original tool names are retained internally for execution, while only the sanitized names are exposed to the model/LLM interface.
  • Diagnostic Logging: Emits console logs when a tool name is altered due to sanitization, aiding in debugging and identifying upstream naming issues.
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 introduces a sanitization mechanism for tool function names to comply with Gemini API constraints, preventing 400 INVALID_ARGUMENT errors. The approach of sanitizing names at registration and just before API submission is solid. However, I've found a critical issue in the sanitization logic itself where the regex for handling invalid starting characters is too greedy, leading to incorrect name transformations. My review includes a specific code suggestion to correct this behavior.

@ProdByBuddha
Copy link
Author

Code Review

This pull request introduces a sanitization mechanism for tool function names to comply with Gemini API constraints, preventing 400 INVALID_ARGUMENT errors. The approach of sanitizing names at registration and just before API submission is solid. However, I've found a critical issue in the sanitization logic itself where the regex for handling invalid starting characters is too greedy, leading to incorrect name transformations. My review includes a specific code suggestion to correct this behavior.

Good eye. Committed.

@ProdByBuddha
Copy link
Author

Bump

ProdByBuddha and others added 2 commits December 26, 2025 18:14
Co-authored-by: gemini-code-assist[bot] <176961590+gemini-code-assist[bot]@users.noreply.github.com>
@ProdByBuddha ProdByBuddha force-pushed the fix/sanitize-function-declarations branch from ab01e65 to e8f0f24 Compare December 27, 2025 02:15
@ProdByBuddha
Copy link
Author

ProdByBuddha commented Dec 27, 2025

bump

@gemini-cli gemini-cli bot added priority/p2 Important but can be addressed in a future release. area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality labels Jan 7, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area/agent Issues related to Core Agent, Tools, Memory, Sub-Agents, Hooks, Agent Quality priority/p2 Important but can be addressed in a future release.

Projects

None yet

1 participant