From 5cfef6306c23c62707efdfe018095945c7371c3f Mon Sep 17 00:00:00 2001 From: dor Date: Wed, 21 Oct 2026 13:06:34 -0400 Subject: [PATCH] fix(complexity-router): keep the Nadir deployment check and prior-turn markers fully typed The type gate counted seven new partially-unknown errors: the deployment check read litellm_params through an isinstance(Mapping) narrowing and EMPTY_MAPPING, both Mapping[Unknown, Unknown]. Validate the params as Mapping[str, object] instead, and give the prior-turn helper a typed empty mapping. basedpyright over litellm/ now nets no new errors against main. Co-Authored-By: Claude Opus 5.5 --- .../complexity_router/complexity_router.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/litellm/router_strategy/complexity_router/complexity_router.py b/litellm/router_strategy/complexity_router/complexity_router.py index f95e8e3f088..c2eb692bd34 100644 --- a/litellm/router_strategy/complexity_router/complexity_router.py +++ b/litellm/router_strategy/complexity_router/complexity_router.py @@ -888,11 +888,20 @@ def _truncate(text: str, limit: int) -> str: return f"{text[:head_chars]}{_TRUNCATION_MARKER}{text[len(text) - tail_chars :]}" +_DEPLOYMENT_PARAMS: Final[TypeAdapter[Mapping[str, object]]] = TypeAdapter(Mapping[str, object]) +_NO_REQUEST_KWARGS: Final[Mapping[str, object]] = MappingProxyType({}) + + def _is_nadir_deployment(deployment: Mapping[str, object]) -> bool: - litellm_params: Final = deployment.get("litellm_params") - params: Final = litellm_params if isinstance(litellm_params, Mapping) else EMPTY_MAPPING - provider: Final = params.get("custom_llm_provider") or str(params.get("model") or "").split("/", 1)[0] - return provider == "nadir" + try: + params: Final = _DEPLOYMENT_PARAMS.validate_python(deployment.get("litellm_params")) + except ValidationError: + return False + provider: Final = params.get("custom_llm_provider") + if isinstance(provider, str) and provider: + return provider == "nadir" + model: Final = params.get("model") + return isinstance(model, str) and model.split("/", 1)[0] == "nadir" def _iter_context_turns_newest_first( @@ -2607,7 +2616,7 @@ class ComplexityRouter(CustomLogger): budget_chars=self.config.classifier_context_budget_chars, per_turn_chars=self.config.classifier_context_per_turn_chars, include_assistant=self.config.classifier_context_include_assistant_turns, - marker_pairs=self._reminder_markers_for_request(request_kwargs or EMPTY_MAPPING), + marker_pairs=self._reminder_markers_for_request(request_kwargs or _NO_REQUEST_KWARGS), ) def _classifier_context_messages(