Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Caching fix - only try/cache key creation #1918

Merged
merged 4 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dspy/clients/lm.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
13 changes: 13 additions & 0 deletions tests/caching/test_caching.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fails on main:

FAILED test_caching.py::test_lms_called_expected_number_of_times_for_cache_key_generation_failures - AssertionError: assert 2 == 1

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
Loading