feat(go/plugins): sort tools by name for stable provider output#5334
Open
Anthony-Bible wants to merge 1 commit into
Open
feat(go/plugins): sort tools by name for stable provider output#5334Anthony-Bible wants to merge 1 commit into
Anthony-Bible wants to merge 1 commit into
Conversation
Contributor
There was a problem hiding this comment.
Code Review
This pull request introduces a SortToolDefinitions utility in a new internal package to ensure stable ordering of tool definitions across various plugins, including OpenAI, Google GenAI, Anthropic, and Ollama. This stability is necessary for consistent provider-side prompt caching. The review feedback identifies potential nil pointer dereferences in several plugins because the sorting utility preserves nil entries rather than filtering them out. Additionally, suggestions were made to optimize the sorting function for small slices and to correct the terminology used in the code documentation regarding panics.
Anthony-Bible
added a commit
to Anthony-Bible/genkit
that referenced
this pull request
May 15, 2026
Move nil-handling into SortToolDefinitions so callers can iterate the result without guarding against nil-pointer dereference. Addresses PR review feedback on genkit-ai#5334. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
Genkit's tool registry is a Go map, so iteration order is randomized. Without sorting, two requests with identical tool sets produce different wire bytes — which breaks Anthropic's prompt cache (and prevents stable output more generally). Adds a shared SortToolDefinitions helper in go/plugins/internal and wires it into toAnthropicTools plus the compat_oai, googlegenai, and ollama providers.
2c6595f to
49656e7
Compare
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.
Genkit's tool registry is a Go map, so iteration order is randomized. Without sorting, two requests with identical tool sets produce different wire bytes — which breaks Anthropic's prompt cache (and prevents stable output more generally) and from what I can tell it affects other providers like openai and gemini since that's how prompt caching works. I understand we currently don't have caching in the Go SDK and it's being worked on . But I did enable caching in a local fork to verify that sorted tools for at least Anthropic does help with caching, without sorting prompt caching is basically non-existent
I haven't actually tested this with OpenAI or Gemini, but I'd expect the same problem, their caches also depend on the request looking the same byte-for-byte each turn, and random tool order would break that too. Sorting in the shared helper is cheap, so I included them. Happy to drop those call sites if you'd rather land them separately once someone verifies.
Adds a shared SortToolDefinitions helper in go/plugins/internal and wires it into toAnthropicTools plus the compat_oai, googlegenai, and ollama providers.