fix(cache): include tool_choice in ChatCompletionCache key - #8213
Open
ZIFeIYUuuuuuu wants to merge 1 commit into
Open
fix(cache): include tool_choice in ChatCompletionCache key#8213ZIFeIYUuuuuuu wants to merge 1 commit into
ZIFeIYUuuuuuu wants to merge 1 commit into
Conversation
Author
|
@microsoft-github-policy-service agree |
This was referenced Sep 11, 2026
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?
ChatCompletionCachecurrently computes its cache key without includingtool_choice. As a result, calls that share messages and tools but request different tool policies can reuse the same cached response. For example, a request withtool_choice="required"can return the response cached fortool_choice="none"(and vice versa). The same collision occurs forcreate_stream.This is especially serious for agents because
tool_choicechanges the model contract: a caller requiring a tool call must not receive a completion produced under a no-tool policy.The cache key now includes a JSON-serializable representation of
tool_choice. Literal policies (auto,required, andnone) are stored directly; a concreteToolis represented by its schema, matching the existing treatment of thetoolsargument. Bothcreateandcreate_streampass the selected policy into the key calculation.Related issue number
Closes #8211
Checks
Reproduction and regression coverage
Before this change, the following sequence incorrectly returned the first cached result on the second call:
The regression test verifies that
required,none, and a concrete tool choice each produce separate non-streaming entries. It also verifies that streaming calls withrequiredandnonedo not collide.Validation
uv run pytest packages/autogen-ext/tests/models/test_chat_completion_cache.py -q— 27 passed (3 existing deprecation warnings).uv run ruff format --check packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.py packages/autogen-ext/tests/models/test_chat_completion_cache.py— 2 files already formatted.uv run ruff check packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.py packages/autogen-ext/tests/models/test_chat_completion_cache.py— All checks passed.git diff --check— passed.The optional model-client test modules in the full
packages/autogen-ext/tests/modelscollection require separately installedtorch,ollama, andsemantic_kernel; collection stops before executing those optional tests in this local environment.