fix(google): validate tool names to prevent API errors#11438
Open
codewarnab wants to merge 6 commits intovercel:mainfrom
Open
fix(google): validate tool names to prevent API errors#11438codewarnab wants to merge 6 commits intovercel:mainfrom
codewarnab wants to merge 6 commits intovercel:mainfrom
Conversation
Contributor
There was a problem hiding this comment.
Pull request overview
This PR adds client-side validation for Google Gemini tool names to prevent API errors. The validation ensures tool names conform to Gemini's naming requirements (must start with a letter or underscore, followed by alphanumeric characters, underscores, dots, colons, or dashes) before making API calls, providing clearer error messages to developers.
Changes:
- Added regex validation for tool names in
prepareToolsfunction - Added test case to verify invalid tool names are rejected with appropriate error
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| packages/google/src/google-prepare-tools.ts | Added validation logic to check tool names against Gemini API requirements; includes formatting adjustments to type definitions |
| packages/google/src/google-prepare-tools.test.ts | Added test case to verify error is thrown for invalid tool names starting with numbers |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Background
The Gemini API enforces strict validation on tool (function) names. Currently, the SDK passes invalid tool names (e.g., those starting with numbers) directly to the API, resulting in a 400 error. This error can be confusing and hard to debug for users.
api error can be verified with this
Summary
I added client-side validation in
prepareToolsto check if tool names match Gemini's requirements (regex:^[a-zA-Z_][a-zA-Z0-9_.:-]*$).If a name is invalid, the SDK now throws a descriptive
Errorimmediately, instead of making a failing API call.Design Decision: Validation vs. Sanitization
I considered automatically sanitizing tool names (e.g., prefixing with
fn_or removing invalid characters) to make them valid. However, I decided against this for the following reasons:But I am open to feedback if maintainers prefer a sanitization approach.
Manual Verification
I created a reproduction test case in
google-prepare-tools.test.tsthat defines a tool named123invalid.I verified this by running
pnpm test:node src/google-prepare-tools.test.tsinpackages/google.Checklist