fix(router): delegate multi-group fallback lookup to the canonical resolver

Addresses Copilot review feedback on PR #40977: once
_get_fallback_model_group_from_fallbacks started honoring the "*" wildcard,
_get_fallback_model_group_for_lookup_groups broke -- it took the first
non-None result across lookup_groups (tier first, then requested group), so
a "*" wildcard match for the tier-specific group would win and shadow a
later group's own, more specific fallback chain.

Delegate to the existing get_fallback_model_group_for_lookup_groups instead
of re-implementing the multi-group search: it computes every group's result
up front and only falls back to a generic "*" chain once every group has
missed a specific match, so a catch-all can no longer shadow a later
group's own chain.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01MLsCM4Wa2A7Yvsqf5hABdm
Signed-off-by: Thippaluri Yaseen Basha <yasenroycey6@gmail.com>
This commit is contained in:
Thippaluri Yaseen Basha 2026-09-13 13:04:33 +05:30
parent 4727d85380
commit 7ea187190f
2 changed files with 32 additions and 11 deletions

View file

@ -8008,18 +8008,13 @@ class Router:
fallbacks: list[dict[str, list[str]]], # mutable-ok: mirrors the sibling resolver's contract
lookup_groups: tuple[str, ...],
) -> list[str] | None: # mutable-ok: mirrors the sibling resolver's contract
"""First lookup group whose exact-key chain resolves (tier first, then requested group)."""
return next(
(
resolved
for resolved in (
self._get_fallback_model_group_from_fallbacks(fallbacks=fallbacks, model_group=group)
for group in lookup_groups
)
if resolved is not None
),
None,
"""First lookup group with a specifically-keyed chain wins (tier first, then requested
group); a generic "*" chain applies only once every group has missed a specific
match, so it cannot shadow a later group's own chain."""
fallback_model_group, _ = get_fallback_model_group_for_lookup_groups(
fallbacks=fallbacks, lookup_groups=lookup_groups
)
return fallback_model_group
def _get_first_default_fallback(self) -> str | None:
"""

View file

@ -10641,6 +10641,32 @@ def test_get_fallback_model_group_from_fallbacks_honors_wildcard_and_stripped_ke
) == ["gpt-4"]
def test_get_fallback_model_group_for_lookup_groups_prefers_specific_chain_over_earlier_wildcard():
"""A "*" wildcard match for an earlier (tier-specific) lookup group must not shadow a
later lookup group's own specifically-keyed fallback chain. Once the single-group
resolver started honoring "*", naively taking the first non-None result across
lookup_groups let the tier-specific group's wildcard match win over the requested
group's own exact match -- delegate to the canonical multi-group resolver instead,
which only falls back to "*" once every group has missed a specific match."""
router = litellm.Router(
model_list=[
{
"model_name": "gpt-3.5-turbo",
"litellm_params": {"model": "gpt-3.5-turbo"},
}
],
)
resolved = router._get_fallback_model_group_for_lookup_groups(
fallbacks=[
{"*": ["generic-fallback"]},
{"gpt-3.5-turbo": ["specific-fallback"]},
],
lookup_groups=("tier:premium/gpt-3.5-turbo", "gpt-3.5-turbo"),
)
assert resolved == ["specific-fallback"]
class _LogCapture(logging.Handler):
def __init__(self, level):
super().__init__(level=level)