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 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.py
index 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_url
self.flush_lock = asyncio.Lock()
+ self.periodic_started = False
super().__init__(**kwargs, flush_lock=self.flush_lock)
def update_values(
@@ -99,12 +100,17 @@ class SlackAlerting(CustomBatchLogger):
if alerting is not None:
self.alerting = alerting
asyncio.create_task(self.periodic_flush())
+ self.periodic_started = True
if alerting_threshold is not None:
self.alerting_threshold = alerting_threshold
if alert_types is not None:
self.alert_types = alert_types
if 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 dict
if 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.py
index 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 os
import sys
import unittest
from typing import List, Optional, Tuple
-from unittest.mock import ANY, MagicMock, Mock, patch
+from unittest.mock import ANY, AsyncMock, MagicMock, Mock, patch
sys.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)