Repository: BerriAI/litellm
Pull request: Fix Slack alerting not working if using a DB
## Title

Fix Slack alerting not working if using a DB. 

## Relevant issues

Fixes #10368

## Pre-Submission checklist

**Please complete all items before asking a LiteLLM maintainer to review your PR**

- [X] I have Added testing in the [`tests/litellm/`](https://github.com/BerriAI/litellm/tree/main/tests/litellm) directory, **Adding at least 1 test is a hard requirement** - [see details](https://docs.litellm.ai/docs/extras/contributing_code)
- [X] I have added a screenshot of my new test passing locally 
- [X] My PR passes all unit tests on (`make test-unit`)[https://docs.litellm.ai/docs/extras/contributing_code]
- [X] My PR's scope is as isolated as possible, it only solves 1 specific problem


## Type

🐛 Bug Fix

## Changes
Add starting the periodic tasks in updateValues if alerting_args is updated but adds a flag to make sure dups arent started.

<img width="732" alt="Screenshot 2025-05-01 at 9 55 15 PM" src="https://github.com/user-attachments/assets/67697065-8c17-4912-a5fd-0bd66fe7c45b" />




Problem statement:
[Bug]: Slack reporting's periodic batch send is never initialized if using a DB.
### What happened?

For Slack alerting the periodic task for the CustomBatchLogger is never initialized if using a DB because alerting is never set in updateValues calls.   Therefore Slack alerts never happen until 512 queue up.  The UT test button doesnt appear to work because of it.

### Relevant log output

```shell

```

### Are you a ML Ops Team?

No

### What LiteLLM version are you on ?

v1.67.4

### Twitter / LinkedIn details

_No response_

Hints from issue discussion:
PR to fix here: https://github.com/BerriAI/litellm/pull/10370

Commit message: Add a unit test for the change

Patch to review:
diff --git a/litellm/integrations/SlackAlerting/slack_alerting.py b/litellm/integrations/SlackAlerting/slack_alerting.py
index 9fde042ae79b..1d4eeb04ddea 100644
--- a/litellm/integrations/SlackAlerting/slack_alerting.py
+++ b/litellm/integrations/SlackAlerting/slack_alerting.py
@@ -78,6 +78,7 @@ def __init__(
         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(
@@ -92,12 +93,17 @@ def update_values(
         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/test_slack.py b/tests/litellm/integrations/test_slack.py
new file mode 100644
index 000000000000..114e7b914946
--- /dev/null
+++ b/tests/litellm/integrations/test_slack.py
@@ -0,0 +1,25 @@
+import os
+import sys
+from unittest.mock import patch, AsyncMock
+
+from litellm.integrations.SlackAlerting.slack_alerting import (
+    SlackAlerting,
+)
+
+sys.path.insert(
+    0, os.path.abspath("../../..")
+)  # Adds the parent directory to the system path
+
+# Calling update_values with alerting args should try to start the periodic task which will fail
+@patch("asyncio.create_task")
+def test_update_values_starts_periodic_task(mock_create_task):
+    # Make it do nothing (or return a dummy future)
+    mock_create_task.return_value = AsyncMock()  # prevents awaiting errors
+
+    sa = SlackAlerting()
+    assert(sa.periodic_started == False)
+
+    sa.update_values(alerting_args={"slack_alerting": "True"})
+    assert(sa.periodic_started == True)
+
+


Code review comments:
path: tests/litellm/integrations/test_slack.py
text: @user1:
follow same naming convention + filepath as in `litellm/` 

so it should be `tests/litellm/integrations/SlackAlerting/test_slack_alerting.py` 

@author:
Fixed thanks