From b85ce96d90d8effe827c7d231accd6856e679627 Mon Sep 17 00:00:00 2001 From: Abhimanyu Kapur <38531241+akapur99@users.noreply.github.com> Date: Sat, 8 Aug 2026 16:21:53 -0700 Subject: [PATCH] style(auto-router): build reachable_by_key functionally to satisfy the type-discipline gate The mutable-dict accumulation loop from the quadratic-scan fix tripped LIT001/ LIT002/LIT010 (mutable collections, append-in-a-loop, non-Final binding). Same single-pass grouping, expressed as sort + groupby into a frozen mapping: still O(n log n) rather than the old O(keys * rows), with nothing mutable left holding the result. Co-Authored-By: Claude --- .../proxy/management_endpoints/auto_router_endpoints.py | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) diff --git a/litellm/proxy/management_endpoints/auto_router_endpoints.py b/litellm/proxy/management_endpoints/auto_router_endpoints.py index d179e93c8f0..7df1a632bed 100644 --- a/litellm/proxy/management_endpoints/auto_router_endpoints.py +++ b/litellm/proxy/management_endpoints/auto_router_endpoints.py @@ -6,6 +6,7 @@ POST /auto_router/test_routing - Route one prompt through an unsaved complexity- from collections.abc import Mapping, Sequence from datetime import datetime, timedelta, timezone +from itertools import groupby from types import MappingProxyType from typing import TYPE_CHECKING, Annotated, Final @@ -557,13 +558,9 @@ def _quality_signals_for( baseline_rows: Final = tuple(row for row in turns if row.router_name is None and row.api_key in router_keys) router_key_rows: Final = tuple(row for row in turns if row.api_key in router_keys) - reachable_by_key_dict: dict[str, set[str]] = {} - for row in router_key_rows: - if row.api_key not in reachable_by_key_dict: - reachable_by_key_dict[row.api_key] = set() - reachable_by_key_dict[row.api_key].add(row.model) + rows_by_key: Final = groupby(sorted(router_key_rows, key=lambda row: row.api_key), key=lambda row: row.api_key) reachable_by_key: Final = MappingProxyType( - {key: frozenset(models) for key, models in reachable_by_key_dict.items()} + {key: frozenset(row.model for row in group) for key, group in rows_by_key} ) reachable_models: Final = tuple(frozenset(row.model for row in router_key_rows)) ranks: Final = rank_models_by_cost(llm_router, reachable_models) if llm_router is not None else MappingProxyType({})