From 7df4a236f46d4525327e13974c1f4e3c871ca630 Mon Sep 17 00:00:00 2001 From: CJYLZS <691086891@qq.com> Date: Wed, 18 Mar 2026 09:34:43 +0800 Subject: [PATCH] fix(router): address greptile review on least-busy tie-breaking - Remove misleading `in ("1", "2")` assertion in stale-entry test; "2" can never be returned since "1" is always the unique minimum after filtering the stale "0" entry - Replace unreachable `return random.choice(healthy_deployments)` fallback with explicit ValueError guards; the old fallback was dead code after the healthy-ID filter ensured min_deployment_ids is always non-empty, and still raised IndexError on an empty list anyway Made-with: Cursor --- litellm/router_strategy/least_busy.py | 14 ++++++++------ .../router_strategy/test_least_busy.py | 1 - 2 files changed, 8 insertions(+), 7 deletions(-) diff --git a/litellm/router_strategy/least_busy.py b/litellm/router_strategy/least_busy.py index 74b142a1d8b..e4487854f3b 100644 --- a/litellm/router_strategy/least_busy.py +++ b/litellm/router_strategy/least_busy.py @@ -215,13 +215,15 @@ class LeastBusyLoggingHandler(CustomLogger): elif v == min_traffic: min_deployment_ids.append(k) - if min_deployment_ids: - chosen_id = random.choice(min_deployment_ids) - for m in healthy_deployments: - if m["model_info"]["id"] == chosen_id: - return m + if not min_deployment_ids: + raise ValueError("No healthy deployments available") - return random.choice(healthy_deployments) + chosen_id = random.choice(min_deployment_ids) + for m in healthy_deployments: + if m["model_info"]["id"] == chosen_id: + return m + + raise ValueError(f"Chosen deployment id {chosen_id!r} not found in healthy_deployments") def get_available_deployments( self, diff --git a/tests/test_litellm/router_strategy/test_least_busy.py b/tests/test_litellm/router_strategy/test_least_busy.py index a1d678e35c9..f434f6a6f77 100644 --- a/tests/test_litellm/router_strategy/test_least_busy.py +++ b/tests/test_litellm/router_strategy/test_least_busy.py @@ -89,7 +89,6 @@ class TestLeastBusyTieBreaking: healthy_deployments=healthy, all_deployments=dict(counts_with_stale), ) - assert chosen["model_info"]["id"] in ("1", "2") assert chosen["model_info"]["id"] == "1"