mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-28 00:00:38 +00:00
* feat(router): let a deployment choose which cache controls a request may override A deployment's litellm_params.cache and the request's cache are now merged one control at a time, and allowed_cache_controls on the deployment names the controls a request may set. A request control outside that list is ignored and named in the x-litellm-ignored-cache-controls response header. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): lock the s-max-age spelling and keep router default cache controls under a lock Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): lock use-cache, merge the s-max-age spelling as s-maxage, and apply the lock to sync text_completion * fix(router): apply a deployment's cache controls on prompt-managed completions and report ignored ones on sync text_completion * fix(router): report a prompt deployment's ignored cache controls when the rendered model is a group * feat(router): a model's cache lock carries over to its fallbacks Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): a fallback keeps its own cache restrictions under the requested model's lock Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): combine a model's cache locks to the strictest, and match either s-maxage spelling in lists Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): keep a virtual key's cache controls and a locked fallback's stricter rule Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): read a virtual key's cache controls only from the request before fallback params Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * fix(router): a key's cache control is exempt only at the key's exact value and type Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * refactor(router): rename the model's allowed_cache_controls to allowed_response_cache_overrides * fix(router): a key's cache control passes a locked model only where it is at least as strict --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
226 lines
9.1 KiB
Python
226 lines
9.1 KiB
Python
import pytest
|
|
|
|
from litellm.router_rust.invoke.cache_controls import (
|
|
ModelCacheControls,
|
|
model_cache_controls,
|
|
with_model_cache_controls,
|
|
)
|
|
from litellm.types.utils import is_litellm_owned_kwarg
|
|
|
|
_OFF = {"no-cache": True, "no-store": True}
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("litellm_params", "request_cache", "expected"),
|
|
[
|
|
pytest.param({}, {"ttl": 5}, ModelCacheControls(None), id="no model rule keeps the request as sent"),
|
|
pytest.param({"cache": _OFF}, None, ModelCacheControls(None), id="no request controls keeps the model rule"),
|
|
pytest.param(
|
|
{"cache": _OFF},
|
|
{"ttl": 600},
|
|
ModelCacheControls({"no-cache": True, "no-store": True, "ttl": 600}),
|
|
id="a request ttl leaves the model's no-store in place",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"ttl": 60}},
|
|
{"ttl": 5},
|
|
ModelCacheControls({"ttl": 5}),
|
|
id="without a list the request wins the control it sends",
|
|
),
|
|
pytest.param(
|
|
{"cache": _OFF, "allowed_response_cache_overrides": []},
|
|
{"ttl": 600, "no-cache": False, "no-store": False},
|
|
ModelCacheControls(_OFF, ("ttl", "no-cache", "no-store")),
|
|
id="an empty list locks every control",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": ["no-cache", "no-store"]},
|
|
{"ttl": 5, "no-cache": True},
|
|
ModelCacheControls({"ttl": 60, "no-cache": True}, ("ttl",)),
|
|
id="a listed control is kept and an unlisted one is ignored",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": ["ttl"]},
|
|
{"ttl": 5},
|
|
ModelCacheControls({"ttl": 5}),
|
|
id="a listed ttl overrides the model's",
|
|
),
|
|
pytest.param(
|
|
{"allowed_response_cache_overrides": []},
|
|
{"s-maxage": 5, "namespace": "team-a"},
|
|
ModelCacheControls({"namespace": "team-a"}, ("s-maxage",)),
|
|
id="a lock without a model rule still ignores controls and keeps other cache fields",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"s-max-age": 3600}},
|
|
{"s-maxage": 1},
|
|
ModelCacheControls({"s-maxage": 1}),
|
|
id="a request s-maxage replaces the model's s-max-age spelling",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"use-cache": False}, "allowed_response_cache_overrides": []},
|
|
{"use-cache": True},
|
|
ModelCacheControls({"use-cache": False}, ("use-cache",)),
|
|
id="a locked model ignores a request opting in to the cache",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"use-cache": False}, "allowed_response_cache_overrides": ["use-cache"]},
|
|
{"use-cache": True},
|
|
ModelCacheControls({"use-cache": True}),
|
|
id="a listed use-cache lets a request opt in",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"s-maxage": 1}, "allowed_response_cache_overrides": []},
|
|
{"s-max-age": 3600},
|
|
ModelCacheControls({"s-maxage": 1}, ("s-max-age",)),
|
|
id="the s-max-age spelling is locked with s-maxage",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"s-maxage": 1}, "allowed_response_cache_overrides": ["s-maxage"]},
|
|
{"s-max-age": 3600},
|
|
ModelCacheControls({"s-maxage": 3600}),
|
|
id="the s-max-age spelling is allowed with s-maxage",
|
|
),
|
|
pytest.param(
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": []},
|
|
None,
|
|
ModelCacheControls({"ttl": 60}),
|
|
id="a locked model with no request controls reports nothing ignored",
|
|
),
|
|
],
|
|
)
|
|
def test_model_cache_controls_merge_per_control(
|
|
litellm_params: dict[str, object], request_cache: object, expected: ModelCacheControls
|
|
) -> None:
|
|
assert model_cache_controls(litellm_params, request_cache) == expected
|
|
|
|
|
|
def test_allowed_response_cache_overrides_is_not_sent_to_the_provider() -> None:
|
|
assert is_litellm_owned_kwarg("allowed_response_cache_overrides")
|
|
|
|
|
|
def test_a_null_list_leaves_the_model_cache_controls_and_the_request_as_they_are() -> None:
|
|
request = {"model": "a"}
|
|
|
|
params, merged, ignored = with_model_cache_controls(
|
|
{"model": "openai/a", "cache": {"no-store": True}, "allowed_response_cache_overrides": None}, request
|
|
)
|
|
|
|
assert params == {"model": "openai/a", "cache": {"no-store": True}}
|
|
assert merged is request
|
|
assert ignored == ()
|
|
|
|
|
|
def test_either_spelling_of_s_maxage_in_a_lock_and_a_fallback_list_is_the_same_control() -> None:
|
|
from litellm.router_rust.invoke.cache_controls import with_cache_lock
|
|
|
|
locked = with_cache_lock(
|
|
{"model": "openai/b", "allowed_response_cache_overrides": ["s-maxage", "ttl"]},
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": ["s-max-age", "no-cache"]},
|
|
)
|
|
|
|
assert locked == {"model": "openai/b", "cache": {"ttl": 60}, "allowed_response_cache_overrides": ["s-maxage"]}
|
|
|
|
|
|
@pytest.mark.parametrize("bucket", ["metadata", "litellm_metadata"])
|
|
def test_a_control_the_virtual_key_enforces_survives_a_model_list_that_leaves_it_out(bucket: str) -> None:
|
|
request = {
|
|
"cache": {"no-store": True, "use-cache": True, "ttl": 5},
|
|
bucket: {"user_api_key_metadata": {"cache": {"no-store": True}}},
|
|
}
|
|
|
|
_, merged, ignored = with_model_cache_controls(
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": ["use-cache"]}, request
|
|
)
|
|
|
|
assert merged["cache"] == {"ttl": 60, "no-store": True, "use-cache": True}
|
|
assert ignored == ("ttl",)
|
|
|
|
|
|
def test_a_fallback_with_its_own_list_keeps_the_strictest_of_its_lock_and_the_requested_models() -> None:
|
|
from litellm.router_rust.invoke.cache_controls import with_cache_lock
|
|
|
|
locked = with_cache_lock(
|
|
{"model": "openai/b", "cache": {"no-store": True, "ttl": 60}, "allowed_response_cache_overrides": []},
|
|
{"cache": {"no-store": False, "ttl": 3600, "s-maxage": None}, "allowed_response_cache_overrides": []},
|
|
)
|
|
|
|
assert locked == {
|
|
"model": "openai/b",
|
|
"cache": {"no-store": True, "ttl": 60, "s-maxage": None},
|
|
"allowed_response_cache_overrides": [],
|
|
}
|
|
|
|
|
|
def test_the_strictest_lock_keeps_a_lifetime_that_another_lock_leaves_null() -> None:
|
|
from litellm.router_rust.invoke.cache_controls import strictest_cache_lock
|
|
|
|
locks = [
|
|
{"cache": {"ttl": None}, "allowed_response_cache_overrides": []},
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": []},
|
|
]
|
|
|
|
assert strictest_cache_lock(locks) == {"cache": {"ttl": 60}, "allowed_response_cache_overrides": []}
|
|
assert strictest_cache_lock(locks[::-1]) == {"cache": {"ttl": 60}, "allowed_response_cache_overrides": []}
|
|
|
|
|
|
def test_fallback_params_cannot_pose_as_the_virtual_keys_cache_controls() -> None:
|
|
forged = {"user_api_key_metadata": {"cache": {"no-store": False}}}
|
|
attempt = {"cache": {"no-store": False}, "litellm_metadata": forged, "metadata": forged}
|
|
|
|
_, merged, ignored = with_model_cache_controls(
|
|
{"cache": {"no-store": True}, "allowed_response_cache_overrides": []}, attempt, authenticated={"metadata": {}}
|
|
)
|
|
|
|
assert merged["cache"] == {"no-store": True}
|
|
assert ignored == ("no-store",)
|
|
|
|
|
|
def test_a_key_control_the_attempt_changed_is_no_longer_the_keys() -> None:
|
|
key = {"metadata": {"user_api_key_metadata": {"cache": {"no-store": True}}}}
|
|
|
|
_, merged, ignored = with_model_cache_controls(
|
|
{"cache": {"ttl": 60}, "allowed_response_cache_overrides": []},
|
|
{**key, "cache": {"no-store": False}},
|
|
authenticated=key,
|
|
)
|
|
|
|
assert merged["cache"] == {"ttl": 60}
|
|
assert ignored == ("no-store",)
|
|
|
|
|
|
def test_a_number_standing_in_for_the_keys_boolean_is_not_the_keys_control() -> None:
|
|
key = {"metadata": {"user_api_key_metadata": {"cache": {"no-store": True}}}}
|
|
|
|
_, merged, ignored = with_model_cache_controls(
|
|
{"cache": {"no-store": True}, "allowed_response_cache_overrides": []},
|
|
{**key, "cache": {"no-store": 1}},
|
|
authenticated=key,
|
|
)
|
|
|
|
assert merged["cache"]["no-store"] is True
|
|
assert ignored == ("no-store",)
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
("key_cache", "expected", "ignored_controls"),
|
|
[
|
|
({"no-store": False, "ttl": 86400}, {"no-store": True, "ttl": 60}, ("no-store", "ttl")),
|
|
({"no-store": True, "ttl": 5}, {"no-store": True, "ttl": 5}, ()),
|
|
({"no-cache": False, "s-maxage": 5}, {"no-store": True, "ttl": 60}, ("no-cache", "s-maxage")),
|
|
({"no-cache": True, "ttl": 60}, {"no-store": True, "ttl": 60, "no-cache": True}, ()),
|
|
],
|
|
)
|
|
def test_a_virtual_keys_control_survives_a_locked_model_only_where_it_is_at_least_as_strict(
|
|
key_cache: dict[str, object], expected: dict[str, object], ignored_controls: tuple[str, ...]
|
|
) -> None:
|
|
key = {"metadata": {"user_api_key_metadata": {"cache": key_cache}}}
|
|
|
|
_, merged, ignored = with_model_cache_controls(
|
|
{"cache": {"no-store": True, "ttl": 60}, "allowed_response_cache_overrides": []},
|
|
{**key, "cache": dict(key_cache)},
|
|
authenticated=key,
|
|
)
|
|
|
|
assert merged["cache"] == expected
|
|
assert ignored == ignored_controls
|