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
4 changes: 2 additions & 2 deletions langfuse/_utils/prompt_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,8 +183,8 @@ def delete(self, key: str) -> None:
def invalidate(self, prompt_name: str) -> None:
"""Invalidate all cached prompts with the given prompt name."""
with self._lock:
for key in list(self._cache):
if key.startswith(prompt_name):
for key, item in list(self._cache.items()):
if item.value.name == prompt_name:
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.py
Original file line number Diff line number Diff line change
Expand Up @@ -748,3 +748,41 @@ def test_get_fresh_prompt_when_version_changes(langfuse: Langfuse):
result_call_2 = langfuse.get_prompt(prompt_name, version=2)
assert mock_server_call.call_count == 2
assert result_call_2 == version_changed_prompt_client


def test_invalidate_only_evicts_entries_of_the_given_prompt_name():
cache = PromptCache()

def cache_prompt(name: str, *, version=None, label=None) -> str:
key = PromptCache.generate_cache_key(name, version=version, label=label)
prompt = Prompt_Text(
name=name,
version=version or 1,
prompt="prompt",
labels=[],
type="text",
config={},
tags=[],
)
cache.set(key, TextPromptClient(prompt), None)
return key

try:
invalidated_keys = [
cache_prompt("summary"),
cache_prompt("summary", label="staging"),
cache_prompt("summary", version=3),
]
kept_keys = [
cache_prompt("summary_detailed"),
cache_prompt("summary-detailed"),
cache_prompt("summary-v2", version=1),
cache_prompt("summary/nested"),
]

cache.invalidate("summary")

assert [key for key in invalidated_keys if cache.get(key)] == []
assert [key for key in kept_keys if cache.get(key) is None] == []
finally:
cache._task_manager.shutdown()
Loading