Skip to content

The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]

fix(proxy): wait for the router before the first deprecation alert

mateo6276eabf190f
diff --git a/litellm/integrations/SlackAlerting/slack_alerting.py b/litellm/integrations/SlackAlerting/slack_alerting.py
index 7cafc461000..e7cd3cb048d 100644
--- a/litellm/integrations/SlackAlerting/slack_alerting.py
+++ b/litellm/integrations/SlackAlerting/slack_alerting.py
@@ -43,6 +43,8 @@ from litellm.repositories.user_repository import UserRepository
from litellm.types.integrations.slack_alerting import *
from litellm.types.proxy.model_deprecation import (
DEFAULT_DEPRECATION_CHECK_INTERVAL_SECONDS,
+ DEPRECATION_ROUTER_WAIT_ATTEMPTS,
+ DEPRECATION_ROUTER_WAIT_SECONDS,
)
from ..email_templates.templates import *
@@ -1080,7 +1082,12 @@ Model Info:
async def _run_scheduled_deprecation_check(
self, get_llm_router: Callable[[], Router | None] = _proxy_llm_router
) -> None:
- """Alert once on startup, then daily, re-reading the router and alert types each pass"""
+ """Alert once the router is loaded, then daily, re-reading the router and alert types each pass"""
+ for _ in range(DEPRECATION_ROUTER_WAIT_ATTEMPTS):
+ if get_llm_router() is not None:
+ break
+ await asyncio.sleep(DEPRECATION_ROUTER_WAIT_SECONDS)
+
while True:
try:
await self.send_model_deprecation_alert(llm_router=get_llm_router())
diff --git a/litellm/types/proxy/model_deprecation.py b/litellm/types/proxy/model_deprecation.py
index 74b7ea866f4..9f640c383fc 100644
--- a/litellm/types/proxy/model_deprecation.py
+++ b/litellm/types/proxy/model_deprecation.py
@@ -9,6 +9,10 @@ DEFAULT_DEPRECATION_WARN_DAYS: Final = 30
DEFAULT_DEPRECATION_CHECK_INTERVAL_SECONDS: Final = 24 * 60 * 60
+DEPRECATION_ROUTER_WAIT_SECONDS: Final = 30
+
+DEPRECATION_ROUTER_WAIT_ATTEMPTS: Final = 20
+
DeprecationStatus = Literal["upcoming", "imminent", "deprecated"]
diff --git a/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py b/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py
index 9b4bb26fe22..5d0e6b19975 100644
--- a/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py
+++ b/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py
@@ -3,6 +3,7 @@
import asyncio
import os
import sys
+from itertools import chain, repeat
from unittest.mock import AsyncMock, MagicMock, patch
import pytest
@@ -12,6 +13,7 @@ sys.path.insert(0, os.path.abspath("../../../.."))
import litellm
from litellm.integrations.SlackAlerting.slack_alerting import SlackAlerting
from litellm.proxy._types import AlertType
+from litellm.types.proxy.model_deprecation import DEPRECATION_ROUTER_WAIT_SECONDS
def _make_router(deployments):
@@ -121,7 +123,6 @@ async def test_should_alert_once_the_alert_type_and_router_arrive_after_startup(
}
]
)
- routers = [None, router]
async def stop_after_second_pass(_seconds):
if alerting.alert_types == [AlertType.llm_exceptions]:
@@ -138,10 +139,53 @@ async def test_should_alert_once_the_alert_type_and_router_arrive_after_startup(
side_effect=stop_after_second_pass,
),
pytest.raises(asyncio.CancelledError),
+ ):
+ await alerting._run_scheduled_deprecation_check(get_llm_router=lambda: router)
+
+ mock_send_alert.assert_awaited_once()
+ assert "dead-alias" in mock_send_alert.await_args.kwargs["message"]
+
+
+async def test_should_wait_for_the_router_instead_of_sleeping_a_full_day(monkeypatch):
+ """Config load can start the loop before the router exists, which must not cost a day of alerts"""
+ monkeypatch.setattr(
+ litellm,
+ "model_cost",
+ {"dead-model": {"deprecation_date": "2020-01-01", "litellm_provider": "openai"}},
+ )
+ alerting = SlackAlerting(
+ alerting=["slack"], alert_types=[AlertType.model_deprecation_warnings]
+ )
+ router = _make_router(
+ [
+ {
+ "model_name": "dead-alias",
+ "litellm_params": {"model": "dead-model"},
+ "model_info": {"id": "1"},
+ }
+ ]
+ )
+ routers = chain((None, None), repeat(router))
+ slept: list[float] = []
+
+ async def record_sleep(seconds):
+ slept.append(seconds)
+ if len(slept) > 2:
+ raise asyncio.CancelledError
+
+ with (
+ patch.object(alerting, "send_alert", new_callable=AsyncMock) as mock_send_alert,
+ patch(
+ "litellm.integrations.SlackAlerting.slack_alerting.asyncio.sleep",
+ side_effect=record_sleep,
+ ),
+ pytest.raises(asyncio.CancelledError),
):
await alerting._run_scheduled_deprecation_check(
- get_llm_router=lambda: routers.pop(0)
+ get_llm_router=lambda: next(routers)
)
+ assert slept[:2] == [DEPRECATION_ROUTER_WAIT_SECONDS] * 2
mock_send_alert.assert_awaited_once()
assert "dead-alias" in mock_send_alert.await_args.kwargs["message"]