Skip to content

feat(go/plugins): sort tools by name for stable provider output#5334

Open
Anthony-Bible wants to merge 1 commit into
genkit-ai:mainfrom
Anthony-Bible:sort-anthropic-tools
Open

feat(go/plugins): sort tools by name for stable provider output#5334
Anthony-Bible wants to merge 1 commit into
genkit-ai:mainfrom
Anthony-Bible:sort-anthropic-tools

Conversation

@Anthony-Bible
Copy link
Copy Markdown
Contributor

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.

Copy link
Copy Markdown
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 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.

Comment thread go/plugins/googlegenai/tools.go
Comment thread go/plugins/internal/anthropic/anthropic.go
Comment thread go/plugins/ollama/ollama.go
Comment thread go/plugins/internal/tools.go Outdated
Comment thread go/plugins/internal/tools.go Outdated
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.
@Anthony-Bible Anthony-Bible force-pushed the sort-anthropic-tools branch from 2c6595f to 49656e7 Compare May 29, 2026 13:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant