Skip to content

Commit 651a177

Browse files
authored
feat(prompts): clear prompt cache (#1278)
1 parent ee4096b commit 651a177

File tree

3 files changed

+67
-1
lines changed

3 files changed

+67
-1
lines changed

langfuse/_client/client.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2996,3 +2996,14 @@ def _url_encode(self, url: str, *, is_url_param: Optional[bool] = False) -> str:
29962996
# we need add safe="" to force escaping of slashes
29972997
# This is necessary for prompts in prompt folders
29982998
return urllib.parse.quote(url, safe="")
2999+
3000+
def clear_prompt_cache(self):
3001+
"""
3002+
Clear the entire prompt cache, removing all cached prompts.
3003+
3004+
This method is useful when you want to force a complete refresh of all
3005+
cached prompts, for example after major updates or when you need to
3006+
ensure the latest versions are fetched from the server.
3007+
"""
3008+
if self._resources is not None:
3009+
self._resources.prompt_cache.clear()

langfuse/_utils/prompt_cache.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,10 @@ def add_refresh_prompt_task(self, key: str, fetch_func: Callable[[], None]) -> N
162162
self._log.debug(f"Submitting refresh task for key: {key}")
163163
self._task_manager.add_task(key, fetch_func)
164164

165+
def clear(self) -> None:
166+
"""Clear the entire prompt cache, removing all cached prompts."""
167+
self._cache.clear()
168+
165169
@staticmethod
166170
def generate_cache_key(
167171
name: str, *, version: Optional[int], label: Optional[str]

tests/test_prompt.py

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -679,7 +679,11 @@ def test_prompt_end_to_end():
679679

680680
@pytest.fixture
681681
def langfuse():
682-
langfuse_instance = Langfuse()
682+
langfuse_instance = Langfuse(
683+
public_key="test-public-key",
684+
secret_key="test-secret-key",
685+
host="https://mock-host.com",
686+
)
683687
langfuse_instance.api = Mock()
684688

685689
return langfuse_instance
@@ -1410,3 +1414,50 @@ def test_update_prompt():
14101414
expected_labels = sorted(["latest", "doe", "production", "john"])
14111415
assert sorted(fetched_prompt.labels) == expected_labels
14121416
assert sorted(updated_prompt.labels) == expected_labels
1417+
1418+
1419+
def test_clear_prompt_cache(langfuse):
1420+
"""Test clearing the entire prompt cache."""
1421+
prompt_name = create_uuid()
1422+
1423+
# Mock the API calls
1424+
mock_prompt = Prompt_Text(
1425+
name=prompt_name,
1426+
version=1,
1427+
prompt="test prompt",
1428+
type="text",
1429+
labels=["production"],
1430+
config={},
1431+
tags=[],
1432+
)
1433+
1434+
# Mock the create_prompt API call
1435+
langfuse.api.prompts.create.return_value = mock_prompt
1436+
1437+
# Mock the get_prompt API call
1438+
langfuse.api.prompts.get.return_value = mock_prompt
1439+
1440+
# Create a prompt and cache it
1441+
prompt_client = langfuse.create_prompt(
1442+
name=prompt_name,
1443+
prompt="test prompt",
1444+
labels=["production"],
1445+
)
1446+
1447+
# Get the prompt to cache it
1448+
cached_prompt = langfuse.get_prompt(prompt_name)
1449+
assert cached_prompt.name == prompt_name
1450+
1451+
# Verify that the prompt is in the cache
1452+
cache_key = f"{prompt_name}-label:production"
1453+
assert langfuse._resources.prompt_cache.get(cache_key) is not None, "Prompt should be in cache before clearing"
1454+
1455+
# Clear the entire prompt cache
1456+
langfuse.clear_prompt_cache()
1457+
1458+
# Verify cache is completely cleared
1459+
assert langfuse._resources.prompt_cache.get(cache_key) is None, "Prompt should be removed from cache after clearing"
1460+
1461+
# Verify data integrity
1462+
assert prompt_client.name == prompt_name
1463+
assert cached_prompt.name == prompt_name

0 commit comments

Comments
 (0)