Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

Caching fix - only try/cache key creation (#1918)

Corey Zumarf11ccc0f6f69
diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py
index 349e65339..35c623dee 100644
--- a/dspy/clients/lm.py
+++ b/dspy/clients/lm.py
@@ -278,11 +278,11 @@ def request_cache(maxsize: Optional[int] = None):
def wrapper(request: dict, *args, **kwargs):
try:
key = cache_key(request)
- return func_cached(key, request, *args, **kwargs)
except Exception:
# If the cache key cannot be computed (e.g. because it contains a value that cannot
# be converted to JSON), bypass the cache and call the target function directly
return func(request, *args, **kwargs)
+ return func_cached(key, request, *args, **kwargs)
return wrapper
diff --git a/tests/caching/test_caching.py b/tests/caching/test_caching.py
index 023fe45dc..f890dfad3 100644
--- a/tests/caching/test_caching.py
+++ b/tests/caching/test_caching.py
@@ -150,3 +150,16 @@ def test_lm_calls_with_callables_are_cached_as_expected():
lm_without_callable("Query")
assert mock_completion.call_count == 2
+
+
+def test_lms_called_expected_number_of_times_for_cache_key_generation_failures():
+ with pytest.raises(Exception), patch("litellm.completion") as mock_completion:
+ mock_completion.side_effect = Exception("Mocked exception")
+ lm = dspy.LM(
+ model="openai/dspy-test-model",
+ api_base="fakebase",
+ api_key="fakekey",
+ )
+ lm("Do not retry")
+
+ assert mock_completion.call_count == 1