fix(prompts): evict prompt cache on NotFound error (#1442) #1456
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.
Important
Evict prompt cache entries on NotFoundError during refresh in
client.pyand add corresponding test.client.py,fetch_promptsnow evicts cache entry onNotFoundErrorby callingdelete()onPromptCache.delete()method toPromptCacheinprompt_cache.pyto remove cache entries by key.test_evict_prompt_cache_entry_when_refresh_returns_not_foundintest_prompt.pyto verify cache eviction onNotFoundError.This description was created by
for 24e7924. You can customize this summary. It will automatically update as commits are pushed.
Disclaimer: Experimental PR review
Greptile Overview
Greptile Summary
Fixes a cache staleness issue where deleted prompts remain cached indefinitely by evicting cache entries when the API returns a
NotFoundError.Changes:
NotFoundErrorexception handling in_fetch_prompt_and_update_cache()to detect when prompts are deleted from the serverdelete()method inPromptCacheclass to safely remove cache entriesNotFoundErrorduring background refresh_resourceswith a realPromptCacheinstanceImpact:
This ensures that when a prompt is deleted on the server, subsequent calls after cache expiry will properly receive a
NotFoundErrorand fall back to the fallback prompt (if provided) instead of serving stale cached data indefinitely.Confidence Score: 5/5
delete()method uses safepop()with a default value, preventingKeyError. The new exception handler is placed before the generic exception handler, ensuring correct catch order. The test comprehensively validates the new behavior including background refresh scenarios.Important Files Changed
File Analysis
NotFoundErrorhandling to evict stale cache entries when prompts are deleted, with proper import and error handlingdelete()method to remove cache entries by key using safepop()operationNotFoundErrorand updated fixture to properly initialize_resourcesSequence Diagram
sequenceDiagram participant Client as Client Code participant Langfuse as Langfuse.get_prompt() participant Cache as PromptCache participant Fetch as _fetch_prompt_and_update_cache() participant API as API.prompts.get() Client->>Langfuse: get_prompt(name, cache_ttl) Langfuse->>Cache: get(cache_key) alt Cache Hit (Expired) Cache-->>Langfuse: cached_prompt (expired) Langfuse->>Cache: add_refresh_prompt_task() Note over Cache: Background refresh triggered Langfuse-->>Client: return stale prompt Cache->>Fetch: refresh in background Fetch->>API: prompts.get() alt Prompt Deleted (NotFoundError) API-->>Fetch: NotFoundError Note over Fetch: NEW: Catch NotFoundError Fetch->>Cache: delete(cache_key) Note over Cache: Cache entry evicted Fetch-->>Cache: raise NotFoundError else Success API-->>Fetch: prompt data Fetch->>Cache: set(cache_key, prompt) end else Cache Miss Cache-->>Langfuse: None Langfuse->>Fetch: fetch directly Fetch->>API: prompts.get() alt NotFoundError API-->>Fetch: NotFoundError Fetch->>Cache: delete(cache_key) Fetch-->>Langfuse: raise NotFoundError Langfuse-->>Client: fallback or error else Success API-->>Fetch: prompt data Fetch->>Cache: set(cache_key, prompt) Fetch-->>Langfuse: prompt Langfuse-->>Client: return prompt end end