genkit
genkit copied to clipboard
fix(go/googlegenai): Merge tools instead of replacing when using ai.WithTools()
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 via config.Tools instead of merging them. Silently.
The fix was simple replacing gcc.Tools = tools with gcc.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:
Error 400: Tool use with function calling is unsupported
Unfortunate, but hopefully temporary.
Once this pull request is merged:
- the SDK will report a clear API error instead of silently dropping tools
- the code will be ready the moment Google enables the natural fit of tools working together
- the Go SDK will match the JS SDK’s behavior, which already merges tools. Here's the test:
https://github.com/firebase/genkit/blob/f88c7bc7dfb73fd39aeffbdc99015f21c553c9ec/js/plugins/google-genai/tests/googleai/gemini_test.ts#L266
Testing
- Added
TestToolMergingto verify the merge behavior - Updated the live test to document and expect the API limitation