fix(cache): include model identity in structured-output cache key (#334)#336
Merged
Merged
Conversation
Two Generator instances with different model_name or base_url but identical messages/output_model/generation_params produced the same cache key, causing the second model to silently receive the first model's cached result without making an API call. Fix: add model_name (str) and base_url (str | None) to _get_cache_key and propagate them through get/set/get_async/set_async so each (model, endpoint, request) triple maps to a distinct key. Co-Authored-By: Claude Opus 4.7 <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.
Summary
Fixes #334.
StructuredOutputCachebuilt its cache key only from request content (messages+ output schema +generation_params), omitting model identity. TwoGenerators with differentmodel_name/base_urlbut identical prompt/schema/params collided — the second model was silently served the first model's cached output and made no API call. This corrupted any experiment comparing/ensembling multiple LLMs on the same prompts (a normal online-case bug, high severity).model_nameandbase_urlinto the hashed key across all fourget/set/get_async/set_asyncpaths and_get_cache_key.Generatorpasses its existingself.model_name/self.base_url.Test plan
tests/generation/structured_output/test_cache_unit.py: differentmodel_nameand differentbase_urldo not collide (sync + async); same identity still hits the cache.🤖 Generated with Claude Code