From f0c2495780f867aa8e78db947a34f7ca54445cc2 Mon Sep 17 00:00:00 2001 From: Mo0nl19ht <81469045+Mo0nl19ht@users.noreply.github.com> Date: Mon, 16 Nov 2026 18:54:29 +0900 Subject: [PATCH] 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. --- langfuse/_utils/prompt_cache.py | 4 ++-- tests/unit/test_prompt.py | 38 +++++++++++++++++++++++++++++++++ 2 files changed, 40 insertions(+), 2 deletions(-) diff --git a/langfuse/_utils/prompt_cache.py b/langfuse/_utils/prompt_cache.py index e286dca49..8ba227b51 100644 --- a/langfuse/_utils/prompt_cache.py +++ b/langfuse/_utils/prompt_cache.py @@ -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: diff --git a/tests/unit/test_prompt.py b/tests/unit/test_prompt.py index eadfb8221..6704670bc 100644 --- a/tests/unit/test_prompt.py +++ b/tests/unit/test_prompt.py @@ -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()