You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Cache: fix key to be request-based, not Example-based #71
dspy-rs caches LM responses keyed on Vec<(String, Value)> derived from the Example struct. This is semantically wrong:
Same logical request through different Example shapes = cache miss
The cache should key on the actual formatted request (messages + params), not the pre-formatting input
DSPy Python keys on SHA256(json(request_params)) with ignored args stripped (api_key, api_base, etc.), which correctly caches at the LM request boundary.
What's needed
Cache key = hash of (model, messages, temperature, max_tokens, tools, ...) — the actual CompletionRequest
Strip non-deterministic fields (api_key, timestamps) from key
Consider rollout_id semantics for cache-busting during optimization (DSPy uses this when temperature > 0)
CacheEntry should store raw response + metadata, not Prediction
The key is derived from the Example's data HashMap — field name/value pairs from the pre-formatting input. This means:
Same logical LM request formatted through different Example shapes = cache miss
Temperature, model, max_tokens, tools are NOT part of the key
Changing the adapter's prompt template invalidates nothing (stale hits)
What the key should be:
// Hash of the actual request sent to the LMstructCacheKey{model:String,messages:Vec<Message>,// the formatted chattemperature:f32,max_tokens:u32,tools:Vec<ToolDefinition>,// NOT included: api_key, api_base, timestamps}
Then SHA256(serde_json::to_string(&cache_key)) as the foyer key.
rollout_id semantics: DSPy uses a rollout_id field that gets rotated during optimization to bust the cache when temperature > 0 (you want different samples, not cached ones). This matters for COPRO/GEPA/MIPRO — without it, optimizers may get stale cached responses when they need fresh samples.
Migration path:
Change CacheKey type to String (the SHA256 hex)
Compute key from CompletionRequest fields in LM::call() before sending
Activity
darinkishore commentedon Feb 12, 2026
Codebase audit — current cache keying
utils/cache.rs:The key is derived from the
Example'sdataHashMap — field name/value pairs from the pre-formatting input. This means:What the key should be:
Then
SHA256(serde_json::to_string(&cache_key))as the foyer key.rollout_id semantics: DSPy uses a
rollout_idfield that gets rotated during optimization to bust the cache whentemperature > 0(you want different samples, not cached ones). This matters for COPRO/GEPA/MIPRO — without it, optimizers may get stale cached responses when they need fresh samples.Migration path:
CacheKeytype toString(the SHA256 hex)CompletionRequestfields inLM::call()before sendingCacheEntry { messages, params, raw_response, parsed: BamlValue, usage, timestamp }rollout_id: Option<String>to key for optimizer cache-busting