fix(autogen-ext): include tool_choice in ChatCompletionCache hash key - #8096
Open
zerafachris (zerafachris) wants to merge 1 commit into
Open
fix(autogen-ext): include tool_choice in ChatCompletionCache hash key#8096zerafachris (zerafachris) wants to merge 1 commit into
zerafachris (zerafachris) wants to merge 1 commit into
Conversation
Two back-to-back create() / create_stream() calls that differed only in tool_choice (e.g. "auto" vs "none") hashed to the same SHA-256 key and the second call was silently served the first call's cached result. Add tool_choice as a parameter to _check_cache() (serialised via tool.schema for Tool instances, or as-is for string literals) and include it in the hash dict alongside the existing fields. Both create() and create_stream() now pass the argument through. Adds a regression test (test_cache_different_tool_choice_is_cache_miss) that asserts auto / none / auto round-trip produces two cache misses followed by a hit. Fixes microsoft#7968 Co-Authored-By: Claude Sonnet 4.6 <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
注册 for free
to join this conversation on GitHub.
Already have an account?
登录 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.
Summary
ChatCompletionCache._check_cache()builds a SHA-256 key frommessages,tools,json_output, andextra_create_args, but never includestool_choice. Two successivecreate()/create_stream()calls that differ only intool_choice(e.g."auto"vs"none") hash to the same key, so the second call is silently served the first call's cached result.Fixes #7968.
Changes
_check_cache()gains atool_choiceparameter (default"auto"to preserve existing behaviour).tool_choiceis serialised into the hash dict:tool.schemaforToolinstances, the string literal otherwise.create()andcreate_stream()passtool_choicethrough to_check_cache().test_cache_different_tool_choice_is_cache_missverifies thatauto→none→autoproduces two misses then a hit.Test plan
pytest tests/models/test_chat_completion_cache.py)test_cache_different_tool_choice_is_cache_miss) passesPrepared with AI assistance (Claude Code, Anthropic), reviewed for correctness before submission.