fix(go/googlegenai): Merge tools instead of replacing when using ai.WithTools() #3866
+397
−50
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.
I needed to combine a custom Genkit tool with recent FileSearch RAG feature. It fits naturally.
It didn’t work, and I eventually realized that
ai.WithTools()was overwriting any tools provided viaconfig.Toolsinstead of merging them. Silently.The fix was simple replacing
gcc.Tools = toolswithgcc.Tools = mergeTools(append(gcc.Tools, tools...))and the built-in tools (FileSearch, GoogleSearch, CodeExecution, Retrieval) remain intact when adding custom tools.
After making the change, I found out that the Gemini API currently doesn’t allow mixing built-in tools with custom (tools) function calling at all:
Unfortunate, but hopefully temporary.
Once this pull request is merged:
genkit/js/plugins/google-genai/tests/googleai/gemini_test.ts
Line 266 in f88c7bc
Testing
TestToolMergingto verify the merge behavior