litellm/tests/unit/proxy/test_model_deprecations_endpoint.py
Krrish Dholakia c5da69cd13
feat(ui): show model deprecation banner on the models page (#50080)
* feat(ui): show model deprecation banner on the models page

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* style(ui): format model deprecation banner

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): refresh model deprecations when models change

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): address review feedback on model deprecation banner

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(ui): cover deprecation role gate and agent builder refresh

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): separate deprecation banner toggle from alerts link

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): dedupe and allow dismissing the model deprecation banner

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): refresh table once and key banner entries on successor

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix: quiet successor logs, count banner models, use local dates

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): pin banner test date and bucket upcoming entries locally

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test: cover mixed-successor log level through the deprecations endpoint

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* test(ui): move deprecation banner cases into integration tests

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

* fix(ui): keep deprecation banner when a refetch fails

Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>

---------

Co-authored-by: Krrish Dholakia <krrishdholakia@berri.ai>
Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
2026-11-04 18:50:14 -08:00

218 lines
7.7 KiB
Python

import logging
from typing import Final
from unittest.mock import MagicMock
import pytest
from fastapi.testclient import TestClient
import litellm
from litellm._logging import verbose_logger
from litellm.proxy import proxy_server
from litellm.proxy._types import LitellmUserRoles, UserAPIKeyAuth
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
from litellm.proxy.proxy_server import app
from tests.unit.proxy.router_entry_point_stubs import route_entry_points_to_stubs
client = TestClient(app)
@pytest.fixture
def authenticated_client(monkeypatch):
app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth(
user_role=LitellmUserRoles.PROXY_ADMIN, api_key="sk-9876"
)
monkeypatch.setattr(
litellm,
"model_cost",
{
"sunset-model": {
"deprecation_date": "2020-01-01",
"litellm_provider": "openai",
},
"future-model": {
"deprecation_date": "2099-01-01",
"litellm_provider": "openai",
},
},
)
router = route_entry_points_to_stubs(MagicMock())
router.get_model_list.return_value = [
{
"model_name": "sunset-alias",
"litellm_params": {"model": "sunset-model"},
"model_info": {"id": "1"},
},
{
"model_name": "future-alias",
"litellm_params": {"model": "future-model"},
"model_info": {"id": "2"},
},
]
monkeypatch.setattr(proxy_server, "llm_router", router)
yield client
app.dependency_overrides.pop(user_api_key_auth, None)
def test_should_bucket_configured_models_by_urgency(authenticated_client):
response = authenticated_client.get("/model/deprecations")
assert response.status_code == 200
payload = response.json()
assert [m["model_name"] for m in payload["deprecated"]] == ["sunset-alias"]
assert [m["model_name"] for m in payload["upcoming"]] == ["future-alias"]
assert payload["imminent"] == []
assert payload["warn_within_days"] == 30
assert payload["deprecated"][0]["days_until_deprecation"] < 0
def test_should_rebucket_with_warn_within_days_override(authenticated_client):
response = authenticated_client.get("/v1/model/deprecations", params={"warn_within_days": 40000})
assert response.status_code == 200
payload = response.json()
assert [m["model_name"] for m in payload["imminent"]] == ["future-alias"]
assert payload["upcoming"] == []
assert payload["warn_within_days"] == 40000
@pytest.fixture
def client_with_cost_map_successors(monkeypatch):
app.dependency_overrides[user_api_key_auth] = lambda: UserAPIKeyAuth(
user_role=LitellmUserRoles.PROXY_ADMIN, api_key="sk-9876"
)
monkeypatch.setattr(
litellm,
"model_cost",
{
"retiring-model": {
"deprecation_date": "2020-01-01",
"litellm_provider": "openai",
"successor_model": "fresh-model",
},
"orphan-model": {"deprecation_date": "2020-01-01", "litellm_provider": "openai"},
},
)
router = route_entry_points_to_stubs(MagicMock())
router.get_model_list.return_value = [
{
"model_name": "retiring-alias",
"litellm_params": {"model": "retiring-model"},
"model_info": {"id": "1"},
},
{
"model_name": "retiring-overridden",
"litellm_params": {"model": "retiring-model"},
"model_info": {"id": "2", "successor_model": "my-own-pick"},
},
{
"model_name": "shared-name",
"litellm_params": {"model": "retiring-model", "api_key": "fake"},
"model_info": {"id": "4"},
},
{
"model_name": "shared-name",
"litellm_params": {"model": "retiring-model", "api_key": "fake"},
"model_info": {"id": "5", "successor_model": "my-own-pick"},
},
{
"model_name": "dated-by-hand",
"litellm_params": {"model": "retiring-model", "api_key": "fake"},
"model_info": {"id": "6", "deprecation_date": "2020-06-01"},
},
{
"model_name": "orphan-alias",
"litellm_params": {"model": "orphan-model"},
"model_info": {"id": "3"},
},
]
monkeypatch.setattr(proxy_server, "llm_router", router)
yield client
app.dependency_overrides.pop(user_api_key_auth, None)
def test_cost_map_successor_is_reported_unless_the_deployment_names_its_own(client_with_cost_map_successors):
response = client_with_cost_map_successors.get("/model/deprecations")
assert response.status_code == 200, response.text
successors = {m["model_name"]: m["successor_model"] for m in response.json()["deprecated"]}
assert successors == {
"retiring-alias": "fresh-model",
"retiring-overridden": "my-own-pick",
"shared-name": "my-own-pick",
"dated-by-hand": "fresh-model",
"orphan-alias": None,
}
def test_unmatched_successor_logs_below_warning_through_endpoint(client_with_cost_map_successors, caplog, monkeypatch):
monkeypatch.setattr(
proxy_server.llm_router.get_model_list,
"side_effect",
lambda model_name=None, **kwargs: (
proxy_server.llm_router.get_model_list.return_value if model_name is None else []
),
)
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
response = client_with_cost_map_successors.get("/model/deprecations")
assert response.status_code == 200, response.text
successor_records = tuple(
record
for record in caplog.records
if record.name == verbose_logger.name and "successor" in record.getMessage()
)
assert successor_records
assert all(record.levelno < logging.WARNING for record in successor_records)
def test_mixed_successor_group_logs_below_warning_through_endpoint(
client_with_cost_map_successors: TestClient,
caplog: pytest.LogCaptureFixture,
monkeypatch: pytest.MonkeyPatch,
) -> None:
router: Final = proxy_server.llm_router
models: Final[tuple[dict[str, object], ...]] = (
{
"model_name": "mixed-successor-group",
"litellm_params": {"model": "retiring-model"},
"model_info": {"id": "10", "successor_model": "successor-a"},
},
{
"model_name": "mixed-successor-group",
"litellm_params": {"model": "retiring-model"},
"model_info": {"id": "11", "successor_model": "successor-b"},
},
{
"model_name": "successor-a",
"litellm_params": {"model": "fresh-model"},
"model_info": {"id": "12"},
},
{
"model_name": "successor-b",
"litellm_params": {"model": "fresh-model"},
"model_info": {"id": "13"},
},
)
def filtered_model_list(
model_name: str | None = None, **_kwargs: object
) -> tuple[dict[str, object], ...]:
return models if model_name is None else tuple(model for model in models if model["model_name"] == model_name)
monkeypatch.setattr(router.get_model_list, "side_effect", filtered_model_list)
with caplog.at_level(logging.DEBUG, logger=verbose_logger.name):
response = client_with_cost_map_successors.get("/model/deprecations")
assert response.status_code == 200, response.text
successor_records: Final = tuple(
record
for record in caplog.records
if record.name == verbose_logger.name and "successor" in record.getMessage()
)
assert successor_records
assert all(
"names several successors" in record.getMessage() and record.levelno < logging.WARNING
for record in successor_records
)