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]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): re-read router and alert types on each deprecation check
mateo25f343a54760
diff --git a/litellm/integrations/SlackAlerting/slack_alerting.py b/litellm/integrations/SlackAlerting/slack_alerting.pyindex b40f4ac03e0..7cafc461000 100644--- a/litellm/integrations/SlackAlerting/slack_alerting.py+++ b/litellm/integrations/SlackAlerting/slack_alerting.py@@ -5,6 +5,7 @@ import datetimeimport osimport randomimport time+from collections.abc import Callablefrom datetime import timedeltafrom typing import TYPE_CHECKING, Any, Final, Literal@@ -56,6 +57,12 @@ else:Router = Any+def _proxy_llm_router() -> Router | None:+ from litellm.proxy.proxy_server import llm_router++ return llm_router++class SlackAlerting(CustomBatchLogger):"""Class for sending Slack Alerts@@ -1070,14 +1077,13 @@ Model Info:)return True- async def _run_scheduled_deprecation_check(self, llm_router: Router | None = None) -> None:- """Alert once on startup, then daily, so operators see the current state"""- if self.alerting is None or AlertType.model_deprecation_warnings not in self.alert_types:- return-+ 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"""while True:try:- await self.send_model_deprecation_alert(llm_router=llm_router)+ await self.send_model_deprecation_alert(llm_router=get_llm_router())except Exception as e: # noqa: BLE001 # a failed alert must not kill the daily loopverbose_proxy_logger.exception("Error in model deprecation alert loop: %s", e)await asyncio.sleep(DEFAULT_DEPRECATION_CHECK_INTERVAL_SECONDS)diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.pyindex 8de4605ecd2..1d6d1e69ca4 100644--- a/litellm/proxy/utils.py+++ b/litellm/proxy/utils.py@@ -482,14 +482,8 @@ class ProxyLogging:) # RUN HANGING REQUEST CHECK (if user wants to alert on hanging requests)self.hanging_requests_check_started = True- if (- self.slack_alerting_instance is not None- and AlertType.model_deprecation_warnings in self.slack_alerting_instance.alert_types- and not self.deprecation_check_started- ):- asyncio.create_task(- self.slack_alerting_instance._run_scheduled_deprecation_check(llm_router=llm_router)- ) # RUN MODEL DEPRECATION ALERT LOOP (if scheduled)+ if self.slack_alerting_instance is not None and not self.deprecation_check_started:+ asyncio.create_task(self.slack_alerting_instance._run_scheduled_deprecation_check())self.deprecation_check_started = Truedef update_values(diff --git a/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py b/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.pyindex 1cf6bbe0354..9b4bb26fe22 100644--- a/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py+++ b/tests/test_litellm/integrations/SlackAlerting/test_model_deprecation_alert.py@@ -1,5 +1,6 @@"""Tests for the Slack alerting model deprecation hook."""+import asyncioimport osimport sysfrom unittest.mock import AsyncMock, MagicMock, patch@@ -98,3 +99,49 @@ async def test_should_dispatch_high_severity_when_deprecated(monkeypatch):assert call_kwargs["alerting_metadata"]["deprecated_count"] == 1assert call_kwargs["alerting_metadata"]["imminent_count"] == 0assert "dead-alias" in call_kwargs["message"]+++async def test_should_alert_once_the_alert_type_and_router_arrive_after_startup(+ monkeypatch,+):+ """The daily loop starts before config reload, so it must re-read both each pass"""+ monkeypatch.setattr(+ litellm,+ "model_cost",+ {"dead-model": {"deprecation_date": "2020-01-01", "litellm_provider": "openai"}},+ )+ alerting = SlackAlerting(alerting=["slack"], alert_types=[AlertType.llm_exceptions])+ router = _make_router(+ [+ {+ "model_name": "dead-alias",+ "litellm_params": {"model": "dead-model"},+ "model_info": {"id": "1"},+ }+ ]+ )+ routers = [None, router]++ async def stop_after_second_pass(_seconds):+ if alerting.alert_types == [AlertType.llm_exceptions]:+ alerting.update_values(+ alert_types=[AlertType.model_deprecation_warnings]+ ) # simulates a config reload enabling the alert+ return+ 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=stop_after_second_pass,+ ),+ pytest.raises(asyncio.CancelledError),+ ):+ await alerting._run_scheduled_deprecation_check(+ get_llm_router=lambda: routers.pop(0)+ )++ mock_send_alert.assert_awaited_once()+ assert "dead-alias" in mock_send_alert.await_args.kwargs["message"]