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 queue persistence to Redis (#19304)

Emerson Gomes13d887a275de
diff --git a/litellm/scheduler.py b/litellm/scheduler.py
index 3225ba0451c..5f3dd4cbf61 100644
--- a/litellm/scheduler.py
+++ b/litellm/scheduler.py
@@ -84,6 +84,7 @@ class Scheduler:
if queue[0][1] == id:
# Remove the item from the queue
heapq.heappop(queue)
+ await self.save_queue(queue=queue, model_name=model_name)
print_verbose(f"Popped id: {id}")
return True
else:
diff --git a/tests/local_testing/test_scheduler.py b/tests/local_testing/test_scheduler.py
index 8a2a117e6ab..f5b44224853 100644
--- a/tests/local_testing/test_scheduler.py
+++ b/tests/local_testing/test_scheduler.py
@@ -10,7 +10,7 @@ sys.path.insert(
0, os.path.abspath("../..")
) # Adds the parent directory to the system path
from litellm import Router
-from litellm.scheduler import FlowItem, Scheduler
+from litellm.scheduler import FlowItem, Scheduler, SchedulerCacheKeys
from litellm import ModelResponse
@@ -40,6 +40,35 @@ async def test_scheduler_diff_model_names():
)
+async def test_scheduler_poll_persists_queue_to_cache():
+ class StubRedisCache:
+ def __init__(self):
+ self.store = {}
+
+ async def async_get_cache(self, key, **kwargs):
+ return self.store.get(key)
+
+ async def async_set_cache(self, key, value, **kwargs):
+ self.store[key] = value
+
+ redis_cache = StubRedisCache()
+ scheduler = Scheduler(redis_cache=redis_cache)
+
+ item1 = FlowItem(priority=0, request_id="10", model_name="gpt-3.5-turbo")
+ item2 = FlowItem(priority=0, request_id="11", model_name="gpt-3.5-turbo")
+ await scheduler.add_request(item1)
+ await scheduler.add_request(item2)
+
+ await scheduler.poll(
+ id="10", model_name="gpt-3.5-turbo", health_deployments=[]
+ )
+
+ queue_key = f"{SchedulerCacheKeys.queue.value}:{item1.model_name}"
+ updated_queue = redis_cache.store[queue_key]
+ assert updated_queue[0][1] == "11"
+
+
@pytest.mark.parametrize("p0, p1", [(0, 0), (0, 1), (1, 0)])
@pytest.mark.parametrize("healthy_deployments", [[{"key": "value"}], []])
@pytest.mark.asyncio