Skip to content

Commit 68178f1

Browse files
committed
fix(prompts): invalidate only cache entries of the given prompt name
PromptCache.invalidate() deleted every key starting with the prompt name, so create_prompt("summary") or update_prompt(name="summary") also evicted cached prompts such as "summary_detailed" and "summary-v2". Matching on "{name}-" is not enough, because prompt names can contain hyphens. Compare the name of each cached prompt instead of the key prefix.
1 parent 077448c commit 68178f1

2 files changed

Lines changed: 42 additions & 2 deletions

File tree

‎langfuse/_utils/prompt_cache.py‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -183,8 +183,10 @@ def delete(self, key: str) -> None:
183183
def invalidate(self, prompt_name: str) -> None:
184184
"""Invalidate all cached prompts with the given prompt name."""
185185
with self._lock:
186-
for key in list(self._cache):
187-
if key.startswith(prompt_name):
186+
# Compare names, not key prefixes: "summary" is a prefix of
187+
# "summary-v2-label:production" too.
188+
for key, item in list(self._cache.items()):
189+
if item.value.name == prompt_name:
188190
del self._cache[key]
189191

190192
def add_refresh_prompt_task(self, key: str, fetch_func: Callable[[], None]) -> None:

‎tests/unit/test_prompt.py‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -748,3 +748,41 @@ def test_get_fresh_prompt_when_version_changes(langfuse: Langfuse):
748748
result_call_2 = langfuse.get_prompt(prompt_name, version=2)
749749
assert mock_server_call.call_count == 2
750750
assert result_call_2 == version_changed_prompt_client
751+
752+
753+
def test_invalidate_only_evicts_entries_of_the_given_prompt_name():
754+
cache = PromptCache()
755+
756+
def cache_prompt(name: str, *, version=None, label=None) -> str:
757+
key = PromptCache.generate_cache_key(name, version=version, label=label)
758+
prompt = Prompt_Text(
759+
name=name,
760+
version=version or 1,
761+
prompt="prompt",
762+
labels=[],
763+
type="text",
764+
config={},
765+
tags=[],
766+
)
767+
cache.set(key, TextPromptClient(prompt), None)
768+
return key
769+
770+
try:
771+
invalidated_keys = [
772+
cache_prompt("summary"),
773+
cache_prompt("summary", label="staging"),
774+
cache_prompt("summary", version=3),
775+
]
776+
kept_keys = [
777+
cache_prompt("summary_detailed"),
778+
cache_prompt("summary-detailed"),
779+
cache_prompt("summary-v2", version=1),
780+
cache_prompt("summary/nested"),
781+
]
782+
783+
cache.invalidate("summary")
784+
785+
assert [key for key in invalidated_keys if cache.get(key)] == []
786+
assert [key for key in kept_keys if cache.get(key) is None] == []
787+
finally:
788+
cache._task_manager.shutdown()

0 commit comments

Comments
 (0)