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 Slack alerting not working if using a DB (#10370)
Gary Caldwell78c264d7a5ff
diff --git a/litellm/integrations/SlackAlerting/slack_alerting.py b/litellm/integrations/SlackAlerting/slack_alerting.pyindex 7e7aa4d370e..16305061ec8 100644--- a/litellm/integrations/SlackAlerting/slack_alerting.py+++ b/litellm/integrations/SlackAlerting/slack_alerting.py@@ -85,6 +85,7 @@ class SlackAlerting(CustomBatchLogger):self.alerting_args = SlackAlertingArgs(**alerting_args)self.default_webhook_url = default_webhook_urlself.flush_lock = asyncio.Lock()+ self.periodic_started = Falsesuper().__init__(**kwargs, flush_lock=self.flush_lock)def update_values(@@ -99,12 +100,17 @@ class SlackAlerting(CustomBatchLogger):if alerting is not None:self.alerting = alertingasyncio.create_task(self.periodic_flush())+ self.periodic_started = Trueif alerting_threshold is not None:self.alerting_threshold = alerting_thresholdif alert_types is not None:self.alert_types = alert_typesif alerting_args is not None:self.alerting_args = SlackAlertingArgs(**alerting_args)+ if not self.periodic_started:+ asyncio.create_task(self.periodic_flush())+ self.periodic_started = True+if alert_to_webhook_url is not None:# update the dictif self.alert_to_webhook_url is None:diff --git a/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py b/tests/litellm/integrations/SlackAlerting/test_slack_alerting.pyindex 425bf8b6a58..b9d0328c5f1 100644--- a/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py+++ b/tests/litellm/integrations/SlackAlerting/test_slack_alerting.py@@ -4,7 +4,7 @@ import osimport sysimport unittestfrom typing import List, Optional, Tuple-from unittest.mock import ANY, MagicMock, Mock, patch+from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patchsys.path.insert(0, os.path.abspath("../../..")@@ -161,3 +161,14 @@ class TestSlackAlerting(unittest.TestCase):)self.assertEqual(event, "soft_budget_crossed")self.assertTrue("Total Soft Budget" in event_message)++ # Calling update_values with alerting args should try to start the periodic task+ @patch("asyncio.create_task")+ def test_update_values_starts_periodic_task(self, mock_create_task):+ # Make it do nothing (or return a dummy future)+ mock_create_task.return_value = AsyncMock() # prevents awaiting errors++ assert(self.slack_alerting.periodic_started == False)++ self.slack_alerting.update_values(alerting_args={"slack_alerting": "True"})+ assert(self.slack_alerting.periodic_started == True)