diff --git a/dspy/clients/lm.py b/dspy/clients/lm.py index 349e653396..35c623deec 100644 --- a/dspy/clients/lm.py +++ b/dspy/clients/lm.py @@ -278,11 +278,11 @@ def func_cached(key: str, request: Dict[str, Any], *args, **kwargs): 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 023fe45dcb..f890dfad36 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