Skip to content

fix(cache): prevent over-invalidation of similarly named prompt prefixes in PromptCache - #1829

Open
Xayar145 wants to merge 4 commits into
langfuse:mainfrom
Xayar145:fix/prompt-cache-invalidate-exact-prefix
Open

fix(cache): prevent over-invalidation of similarly named prompt prefixes in PromptCache#1829
Xayar145 wants to merge 4 commits into
langfuse:mainfrom
Xayar145:fix/prompt-cache-invalidate-exact-prefix

Conversation

@Xayar145

@Xayar145 Xayar145 commented Aug 22, 2026

Copy link
Copy Markdown

Summary

Fixes cache invalidation in PromptCache.invalidate():

  • Checks for exact prompt name matches or keys prefixed with {prompt_name}- instead of bare substring prefix matching (key.startswith(prompt_name)).
  • Added unit test test_prompt_cache_invalidate_exact_prefix_match.

Motivation

Previously, PromptCache.invalidate("summary") would unintentionally evict other prompt keys sharing the same name prefix (for example "summary_detailed-version:1" or "summary_qa-label:production"). Checking exact key match or hyphen-delimited prefix ensures only the target prompt is evicted.

Changes

  • langfuse/_utils/prompt_cache.py: Refactored invalidate() to match key == prompt_name or key.startswith(f"{prompt_name}-").
  • tests/unit/test_prompt.py: Added test ensuring sibling prompts with similar prefixes are not accidentally evicted.

Tests

  • Added test_prompt_cache_invalidate_exact_prefix_match verifying selective invalidation.

Greptile Summary

The PR narrows prompt-cache invalidation to exact keys and hyphen-delimited prefixes and adds a regression test for similarly prefixed names. The delimiter remains ambiguous for valid prompt names containing hyphens.

  • Updates PromptCache.invalidate() to avoid bare prefix matching.
  • Adds coverage showing that invalidating summary preserves summary_detailed.
  • Does not cover or preserve a sibling such as summary-detailed.

Confidence Score: 4/5

The PR is safe to merge with a non-blocking cache-correctness improvement recommended for hyphen-prefixed sibling prompt names.

The change fixes underscore-style prefix collisions, but the shared hyphen delimiter still causes distinct names such as summary-detailed to be evicted when summary is invalidated.

Files Needing Attention: langfuse/_utils/prompt_cache.py, tests/unit/test_prompt.py

Prompt To Fix All With AI
### Issue 1
langfuse/_utils/prompt_cache.py:186
**Hyphen-delimited names still collide**

For cached prompts named `summary` and `summary-detailed`, invalidating `summary` also matches `summary-detailed-version:1`, unnecessarily evicting the distinct sibling and forcing another API fetch on its next access. The added underscore case does not cover this collision because prompt names accept hyphens and cache metadata uses the same delimiter.

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

Reviews (1): Last reviewed commit: "test(prompt): add unit test for exact pr..." | Re-trigger Greptile

Greptile also left 1 inline comment on this PR.

Context used:

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@CLAassistant

Copy link
Copy Markdown

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

Comment thread langfuse/_utils/prompt_cache.py Outdated
with self._lock:
for key in list(self._cache):
if key.startswith(prompt_name):
if key == prompt_name or key.startswith(prefix):

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.

P2 Hyphen-delimited names still collide

For cached prompts named summary and summary-detailed, invalidating summary also matches summary-detailed-version:1, unnecessarily evicting the distinct sibling and forcing another API fetch on its next access. The added underscore case does not cover this collision because prompt names accept hyphens and cache metadata uses the same delimiter.

Knowledge Base Used: Utils and Support Types

Prompt To Fix With AI
This is a comment left during a code review.
Path: langfuse/_utils/prompt_cache.py
Line: 186

Comment:
**Hyphen-delimited names still collide**

For cached prompts named `summary` and `summary-detailed`, invalidating `summary` also matches `summary-detailed-version:1`, unnecessarily evicting the distinct sibling and forcing another API fetch on its next access. The added underscore case does not cover this collision because prompt names accept hyphens and cache metadata uses the same delimiter.

**Knowledge Base Used:** [Utils and Support Types](https://app.greptile.com/personal-org-4986/-/custom-context/knowledge-base/langfuse/langfuse-python/-/docs/utils-support.md)

---

For each issue above, determine whether it is valid and should be fixed. If so, fix it directly.

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.

2 participants