Skip to content

fix: include tool_choice in ChatCompletionCache cache key - #8146

Closed
MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/7968-cache-tool-choice-v2
Closed

MOHAMMED WASIM KHAN (wasim-builds) wants to merge 1 commit into
microsoft:mainfrom
wasim-builds:fix/7968-cache-tool-choice-v2

Conversation

@wasim-builds

Copy link
Copy Markdown

Summary

Fixes #7968

Previously, calls with identical messages/tools but different tool_choice values would incorrectly return cached results. Now tool_choice is included in the cache key computation.

Changes

  • Updated _check_cache to accept and include tool_choice in cache key
  • Handles both string and Tool object tool_choice values
  • Added regression test

Test plan

  • New test passes
  • Existing tests still pass

Previously, calls with identical messages/tools but different tool_choice
values would incorrectly return cached results. Now tool_choice is
included in the cache key computation.

Fixes microsoft#7968
Copilot AI lite review requested due to automatic review settings September 2, 2026 05:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The new Tool-valued tool_choice cache-key path is currently untested and the cache-key construction can be simplified to avoid double-serializing tool schemas.

Once you've addressed the issues Copilot identified, you can request another Copilot review.

Pull request overview

This PR fixes a correctness issue in autogen-ext’s ChatCompletionCache where cached results could be incorrectly reused when only tool_choice differs, by incorporating tool_choice into the cache key so calls that change tool invocation behavior no longer collide.

Changes:

  • Extend ChatCompletionCache._check_cache to accept tool_choice and incorporate it into the cache-key payload.
  • Update create() and create_stream() to pass tool_choice through to cache checking.
  • Add a regression test that demonstrates cache misses when tool_choice changes.
File summaries
File Description
python/packages/autogen-ext/src/autogen_ext/models/cache/_chat_completion_cache.py Adds tool_choice into cache-key computation and threads it through create/create_stream cache checks.
python/packages/autogen-ext/tests/models/test_chat_completion_cache.py Adds a regression test ensuring differing tool_choice values don’t share cache entries.
Review details
  • Files reviewed: 2/2 changed files
  • Comments generated: 2
  • Review effort level: Lite

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +197 to +202
tool_choice_data: str | None = None
if tool_choice is not None:
if isinstance(tool_choice, str):
tool_choice_data = tool_choice
elif isinstance(tool_choice, Tool):
tool_choice_data = json.dumps(tool_choice.schema, sort_keys=True)
Comment on lines +961 to +982
@pytest.mark.asyncio
async def test_cache_key_includes_tool_choice() -> None:
"""Test that different tool_choice values produce different cache keys."""
from autogen_core import InMemoryStore
from autogen_core.models import UserMessage
from autogen_ext.models.cache import ChatCompletionCache
from autogen_ext.models.replay import ReplayChatCompletionClient

responses = ["response 0", "response 1"]
replay_client = ReplayChatCompletionClient(responses)
replay_client.set_cached_bool_value(False)
cached_client = ChatCompletionCache(replay_client, InMemoryStore())

message = [UserMessage(content="hi", source="user")]

r0 = await cached_client.create(message, tool_choice="auto")
assert not r0.cached
assert r0.content == "response 0"

r1 = await cached_client.create(message, tool_choice="none")
assert not r1.cached # Should be cache miss due to different tool_choice
assert r1.content == "response 1"
@wasim-builds

Copy link
Copy Markdown
Author

Superseded by #8185

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatCompletionCache cache key ignores tool_choice, serves stale cached response when only tool_choice differs

2 participants