Repository navigation
refactor(router): route the router family through RoutingContext and close the outside surface - #49118
Conversation
Merging this PR will not alter performance1 warning
Comparing Footnotes |
Codecov Report❌ Patch coverage is 📢 Thoughts on this report? Let us know! |
399e7c5 to
ae80b60
Compare
There was a problem hiding this comment.
Devin Review found 1 potential issue.
1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)
| slack_alerting_logger: Final = ctx.observers().slack_alerting_logger | ||
| if slack_alerting_logger is None: |
There was a problem hiding this comment.
🟡 Missing router crashes exception alerts
When the router is absent, send_llm_exception_alert calls ctx.observers() on None. It previously skipped the alert, so callers handling an unavailable router now fail instead.
| slack_alerting_logger: Final = ctx.observers().slack_alerting_logger | |
| if slack_alerting_logger is None: | |
| if ctx is None: | |
| return | |
| slack_alerting_logger: Final = ctx.observers().slack_alerting_logger | |
| if slack_alerting_logger is None: | |
| return |
Was this helpful? React with 👍 or 👎 to provide feedback.
ae80b60 to
e44f1ca
Compare
8c10c9b to
14f6d02
Compare
Pull Request is not mergeable
…close the outside surface Strategies, pre-call checks, cooldown and fallback helpers, and the auto, complexity and quality sub-routers now take a RoutingContext instead of the Router, and read the router only through registry(), settings(), health() and the context's forwarding methods. Router-free helpers move from router_utils to litellm_core_utils, outside callers of router state switch to entry points, and the surface allowlist shrinks to the endpoint methods plus the entry points
95ebc38 to
3d33aed
Compare
Resolves conflicts from main's RoutingContext refactor (BerriAI#49118): complexity_router keeps both the NADIR_DECISIONS_CONFIG import and main's router_deprecated import, and jev_classifier drops the _DecisionsRouter protocol as main does. RouterJevClassifierClient now calls RoutingContext.adecisions, which forwards kwargs as given, so the Nadir conversation state (DecisionsJSON) still passes through unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Resolve the import conflict in _health_endpoints.py and follow the get_hidden_params_dict move into litellm_core_utils (#49118).
…_bridge Main's BerriAI#49118 moved reasoning_effort_capability from router_utils to litellm_core_utils. The resolution drops the old import in main.py and adds this PR's RequestedReasoningEffort to main's new one
…ontext refactor The same upstream commit (BerriAI#49118) that introduced RoutingContext also moved litellm/router_utils/common_utils.py to litellm/litellm_core_utils/routing_common_utils.py and litellm/router_utils/reasoning_effort_capability.py to litellm/litellm_core_utils/reasoning_effort_capability.py. Two fork-only imports still pointed at the old paths: - fallback_event_handlers.py's filter_team_based_models import (restored in the previous commit using a now-stale fork-patches.txt pattern) - azure_ai/chat/transformation.py's REASONING_EFFORT_STRENGTH_ORDER import Both raised ModuleNotFoundError at `import litellm` time (caught by the py3.11/3.12/3.13/3.14 merge-smoke jobs), which ruff's F821/F401 checks cannot detect since they don't verify that an imported module path resolves to a real file. Verified with an AST-based module-resolution sweep of the whole litellm/ tree.
…t call The new router_surface_check.py (landed by the same BerriAI#49118 RoutingContext refactor this sync's router.py/fallback_event_handlers.py fixes adapt to) flags fallback_event_handlers.py's restored ctx._router.get_model_list(...) call as a new, un-allowlisted Router member use from the router family. The allowlist was regenerated from upstream's own tree, which has no visibility into this fork-only caller. Widened members_router_family via the script's own --write mode.
azure_ai/chat/transformation.py imported REASONING_EFFORT_STRENGTH_ORDER from litellm.router_utils.reasoning_effort_capability, a module upstream's RoutingContext refactor (BerriAI#49118) moved to litellm.litellm_core_utils.reasoning_effort_strength. The merge never touched this fork-only import, breaking `import litellm` on every Python version (lint/smoke/unit all cascaded from it). litellm-rust/Cargo.lock was left inconsistent with Cargo.toml changes from the same refactor and the new litellm-secret-value crate split, failing every --locked rust job. Regenerated with a minimal non-generate-lockfile update (cargo check, no --locked) to avoid an unrelated aws-config/aws-smithy-schema version conflict a full regenerate introduces; verified with cargo check/clippy/fmt --locked. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01B2ZP4KouUNUVt84W4UYopH
Step 4 of the router migration. Strategies, pre-call checks, the cooldown and fallback helpers and the auto, complexity and quality sub-routers stop holding the whole Router and talk to it through a RoutingContext instead. Code outside the router now uses only the endpoint methods and the entry points, which is what lets the next PR move the routing logic behind a swappable core
Details
TLDR
Problem this solves:
litellm.router_utilsmodules_get_client,get_routing_group,get_valid_args,priority_header_update,team_public_model_names_ofHow it solves it:
ctx: RoutingContextinstead of the Routerregistry,settings,health,observers,select,with_fallbacks, the endpoint calls,apply, the pre-call and fallback checks,log_retryctx. A check that still declaresllm_router=gets the Router with aDeprecationWarningrouter_utilstolitellm_core_utils(common_utilsbecomesrouting_common_utils)registry().routing_group(),registry().client(),registry().names(kind="team_public", team_id=...),router_init_args(),health().priority_header_fields(),health().record_failure()router_utilsimports to 43, 0 and 0ctxsignaturesPre-Submission checklist
Please complete all items before asking a LiteLLM maintainer to review your PR
uv run pytest tests/unit/<your_test_file>.py -v. Leave the suites (make test-unit-*,make test-unit) to CI: it finishes in ~15 minutes where a laptop takes an hour or moreScreenshots / Proof of Fix
What ran at e44f1ca62d, rebased on main (969ded2)
python tests/code_coverage_tests/router_surface_check.pypasses with 43 members outside, 0 from the router family, 0 router_utils modules imported outsidepython scripts/ruff_strict_gate.py --base origin/mainpasses, no strict rule over its merge-base countpython -c "import litellm.router, litellm.proxy.proxy_server"imports cleanlyWhat ran at the same commit before the clean restack onto 969ded2
pytest tests/unit/proxy/auth/test_model_checks_fallbacks.py tests/unit/proxy/management_endpoints/search_endpoints tests/unit/router_strategy tests/unit/router_utils tests/unit/typesgives 6110 passed, 16 skippedWhat ran at ae80b60dc2, the previous push
pytest tests/unit/router_utils tests/unit/router_strategy tests/unit/proxy/common_utils/test_cache_aware_routing.pygives 4239 passed, 16 skippedpytest tests/unit/test_routergives 2181 passed, 8 failed, 11 skipped. Run against main (ef9385c), the same 8 give the same result there, 4 failed and 4 passed. None come from this PRTestAutoRouterTraceProvenancetests fail on both withNo module named 'semantic_router', whichpyproject.tomlonly installs below Python 3.14 (local venv is 3.14)test_router_helper_utilsdeployment IO-token tests pass on both when run serially, and fail only in some-n 8runspytest tests/unit/proxy/guardrails/guardrail_hooks -k headroomgives 126 passedType
🧹 Refactoring
Caveats (if any)
High
litellm_router_instance=,litellm_router=,router_instance=,router=becamectx=Medium
health().record_failure()test_background_health.pycache_aware_routingnow gets a RoutingContext and checks identity withbelongs_tollm_router=keep working through the deprecation shim, which hands them the real RouterLow
run_async_fallback,async_search_with_fallbacksandasync_search_with_fallbacks_helpertake the context aslitellm_routing_ctx, because they also take request kwargs and a request field namedctxwould collide.get_matching_search_toolstakes no request kwargs and usesctxexplicit_allowed_failsreaches the router through the context, notsettings()