fix: include tool_choice in ChatCompletionCache cache key#7948
Open
HumphreySun98 wants to merge 1 commit into
Open
fix: include tool_choice in ChatCompletionCache cache key#7948HumphreySun98 wants to merge 1 commit into
HumphreySun98 wants to merge 1 commit into
Conversation
`ChatCompletionCache` forwards `tool_choice` to the underlying client, where it materially changes the model's output (`"auto"` vs `"required"` vs `"none"` vs a specific forced tool). However `_check_cache` did not include `tool_choice` when computing the cache key, so two otherwise-identical calls that differed only in `tool_choice` collided and the second call silently returned the first call's cached result. Include `tool_choice` in the cache-key data (representing a forced `Tool` by its name) and pass it through from both `create` and `create_stream`. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
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.
Why are these changes needed?
ChatCompletionCacheforwardstool_choiceto the underlying client, where itmaterially changes the model's output (
"auto"vs"required"vs"none"vs aspecific forced
Tool). However_check_cachedid not includetool_choicewhen computing the cache key, so two otherwise-identical calls that differ only
in
tool_choicecollide on the same key — the second call silently returns thefirst call's cached result:
This PR includes
tool_choicein the cache-key data (representing a forcedToolby its schema name) and passes it through from bothcreateandcreate_stream. A regression test asserts the fourtool_choiceforms producedistinct cache keys and that the default matches explicit
"auto".Note: existing caches are effectively invalidated by the key change (same as any
cache-key fix); entries were ambiguous between
tool_choicevalues anyway.Related issue number
N/A — found during review of the cache key computation.
Checks
ruff,mypy, and the relevantpytestlocally).