Skip to content

fix(cache): include tool_choice in ChatCompletionCache key - #8213

Open
ZIFeIYUuuuuuu wants to merge 1 commit into
microsoft:mainfrom
ZIFeIYUuuuuuu:fix/8211-tool-choice-cache-key
Open

fix(cache): include tool_choice in ChatCompletionCache key#8213
ZIFeIYUuuuuuu wants to merge 1 commit into
microsoft:mainfrom
ZIFeIYUuuuuuu:fix/8211-tool-choice-cache-key

Conversation

@ZIFeIYUuuuuuu

Copy link
Copy Markdown

Why are these changes needed?

ChatCompletionCache currently computes its cache key without including tool_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 with tool_choice="required" can return the response cached for tool_choice="none" (and vice versa). The same collision occurs for create_stream.

This is especially serious for agents because tool_choice changes 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, and none) are stored directly; a concrete Tool is represented by its schema, matching the existing treatment of the tools argument. Both create and create_stream pass the selected policy into the key calculation.

Related issue number

Closes #8211

Checks

  • I've included any doc changes needed for https://microsoft.github.io/autogen/. No documentation behavior changed.
  • I've added tests (if relevant) corresponding to the changes introduced in this PR.
  • I've made sure the focused auto checks have passed.

Reproduction and regression coverage

Before this change, the following sequence incorrectly returned the first cached result on the second call:

await cache.create(messages, tools=[tool], tool_choice="required")
await cache.create(messages, tools=[tool], tool_choice="none")

The regression test verifies that required, none, and a concrete tool choice each produce separate non-streaming entries. It also verifies that streaming calls with required and none do not collide.

Validation

  • uv run pytest packages/autogen-ext/tests/models/test_chat_completion_cache.py -q27 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.py2 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.pyAll checks passed.
  • git diff --checkpassed.

The optional model-client test modules in the full packages/autogen-ext/tests/models collection require separately installed torch, ollama, and semantic_kernel; collection stops before executing those optional tests in this local environment.

@ZIFeIYUuuuuuu

Copy link
Copy Markdown
Author

@microsoft-github-policy-service agree

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 omits tool_choice from the cache key, so 'must call a tool' and 'must not' share one entry

1 participant