Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion langfuse/_utils/prompt_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -182,9 +182,10 @@ def delete(self, key: str) -> None:

def invalidate(self, prompt_name: str) -> None:
"""Invalidate all cached prompts with the given prompt name."""
prefixes = (f"{prompt_name}-version:", f"{prompt_name}-label:")
with self._lock:
for key in list(self._cache):
if key.startswith(prompt_name):
if key.startswith(prefixes):
Comment on lines +185 to +188

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 Some names still lose cache. Invalidating foo still removes the generated key foo-label:bar-label:production for a different prompt named foo-label:bar. Prompt names are plain strings, and the documented name rules do not exclude this case. This leaves the exact-name fix incomplete and causes unnecessary fetches. Store the prompt name separately, or use an unambiguous key format, and add a test for names containing these separators.

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

Comment:
**Some names still lose cache.** Invalidating `foo` still removes the generated key `foo-label:bar-label:production` for a different prompt named `foo-label:bar`. Prompt names are plain strings, and the documented name rules do not exclude this case. This leaves the exact-name fix incomplete and causes unnecessary fetches. Store the prompt name separately, or use an unambiguous key format, and add a test for names containing these separators.

---

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

del self._cache[key]

def add_refresh_prompt_task(self, key: str, fetch_func: Callable[[], None]) -> None:
Expand Down
38 changes: 38 additions & 0 deletions tests/unit/test_prompt_cache.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
from threading import RLock

from langfuse._utils.prompt_cache import PromptCache


def _cache_with(keys):
cache = object.__new__(PromptCache)
cache._lock = RLock()
cache._cache = {key: object() for key in keys}
return cache


def test_invalidate_does_not_remove_prompts_with_name_prefix():
cache = _cache_with(
["foo-label:production", "foobar-label:production", "unrelated-version:2"]
)

cache.invalidate("foo")

assert "foo-label:production" not in cache._cache
assert "foobar-label:production" in cache._cache
assert "unrelated-version:2" in cache._cache


def test_invalidate_does_not_remove_hyphenated_name_prefix():
k = PromptCache.generate_cache_key
keys = [
k("my-prompt", version=None, label=None),
k("my-prompt", version=3, label=None),
k("my-prompt-extra", version=None, label=None),
]
cache = _cache_with(keys)

cache.invalidate("my-prompt")

assert k("my-prompt", version=None, label=None) not in cache._cache
assert k("my-prompt", version=3, label=None) not in cache._cache
assert k("my-prompt-extra", version=None, label=None) in cache._cache