Skip to content

Commit f11ccc0

Browse files
authored
Caching fix - only try/cache key creation (#1918)
* fix Signed-off-by: dbczumar <[email protected]> * Test Signed-off-by: dbczumar <[email protected]> * fix Signed-off-by: dbczumar <[email protected]> * fix Signed-off-by: dbczumar <[email protected]> --------- Signed-off-by: dbczumar <[email protected]>
1 parent 736118a commit f11ccc0

File tree

2 files changed

+14
-1
lines changed

2 files changed

+14
-1
lines changed

dspy/clients/lm.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,11 +278,11 @@ def func_cached(key: str, request: Dict[str, Any], *args, **kwargs):
278278
def wrapper(request: dict, *args, **kwargs):
279279
try:
280280
key = cache_key(request)
281-
return func_cached(key, request, *args, **kwargs)
282281
except Exception:
283282
# If the cache key cannot be computed (e.g. because it contains a value that cannot
284283
# be converted to JSON), bypass the cache and call the target function directly
285284
return func(request, *args, **kwargs)
285+
return func_cached(key, request, *args, **kwargs)
286286

287287
return wrapper
288288

tests/caching/test_caching.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,3 +150,16 @@ def test_lm_calls_with_callables_are_cached_as_expected():
150150
lm_without_callable("Query")
151151

152152
assert mock_completion.call_count == 2
153+
154+
155+
def test_lms_called_expected_number_of_times_for_cache_key_generation_failures():
156+
with pytest.raises(Exception), patch("litellm.completion") as mock_completion:
157+
mock_completion.side_effect = Exception("Mocked exception")
158+
lm = dspy.LM(
159+
model="openai/dspy-test-model",
160+
api_base="fakebase",
161+
api_key="fakekey",
162+
)
163+
lm("Do not retry")
164+
165+
assert mock_completion.call_count == 1

0 commit comments

Comments
 (0)