Skip to content

refactor(router): route the router family through RoutingContext and close the outside surface - #49118

Merged
narmstrong-berri merged 1 commit into
mainfrom
nate_router_step4_plugins
Oct 31, 2026
Merged

narmstrong-berri merged 1 commit into
mainfrom
nate_router_step4_plugins

Conversation

@narmstrong-berri

@narmstrong-berri narmstrong-berri commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

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:

  • Router-family modules read 44 Router members directly
  • Proxy and other outside code imported 15 litellm.router_utils modules
  • Five Router members used outside the router had no entry point: _get_client, get_routing_group, get_valid_args, priority_header_update, team_public_model_names_of

How it solves it:

  • Router-family functions take ctx: RoutingContext instead of the Router
  • RoutingContext forwards what plugins need: registry, settings, health, observers, select, with_fallbacks, the endpoint calls, apply, the pre-call and fallback checks, log_retry
  • Fallback access and budget checks receive ctx. A check that still declares llm_router= gets the Router with a DeprecationWarning
  • Ten router-free helpers move from router_utils to litellm_core_utils (common_utils becomes routing_common_utils)
  • Outside callers of the other five modules and the five members switch to entry points, e.g. registry().routing_group(), registry().client(), registry().names(kind="team_public", team_id=...), router_init_args(), health().priority_header_fields(), health().record_failure()
  • The surface allowlist shrinks from 48 outside members, 44 router-family members and 15 router_utils imports to 43, 0 and 0
  • 65 test files move to the ctx signatures

Pre-Submission checklist

Please complete all items before asking a LiteLLM maintainer to review your PR

  • I have added meaningful tests
  • The handful of test files covering my change pass locally, e.g. 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 more
  • My PR passes all required CI/CD checks (e.g., lint, schema.d.ts sync check, etc.)
  • My PR's scope is as isolated as possible; it only solves 1 specific problem
  • Every automated review on my PR's latest commit has finished, and I have addressed or replied to each of its findings before requesting a maintainer review

Screenshots / Proof of Fix

What ran at e44f1ca62d, rebased on main (969ded2)

  1. python tests/code_coverage_tests/router_surface_check.py passes with 43 members outside, 0 from the router family, 0 router_utils modules imported outside
  2. python scripts/ruff_strict_gate.py --base origin/main passes, no strict rule over its merge-base count
  3. python -c "import litellm.router, litellm.proxy.proxy_server" imports cleanly

What ran at the same commit before the clean restack onto 969ded2

  1. 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/types gives 6110 passed, 16 skipped

What ran at ae80b60dc2, the previous push

  1. pytest tests/unit/router_utils tests/unit/router_strategy tests/unit/proxy/common_utils/test_cache_aware_routing.py gives 4239 passed, 16 skipped
  2. pytest tests/unit/test_router gives 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 PR
    • 4 TestAutoRouterTraceProvenance tests fail on both with No module named 'semantic_router', which pyproject.toml only installs below Python 3.14 (local venv is 3.14)
    • 4 test_router_helper_utils deployment IO-token tests pass on both when run serially, and fail only in some -n 8 runs
  3. pytest tests/unit/proxy/guardrails/guardrail_hooks -k headroom gives 126 passed

Type

🧹 Refactoring

Caveats (if any)

High

  • Keyword renames break any third-party caller of these helpers
    • litellm_router_instance=, litellm_router=, router_instance=, router= became ctx=

Medium

  • Proxy health writes now go through health().record_failure()
    • Same two calls as before, covered only by a mocked-router test in test_background_health.py
  • cache_aware_routing now gets a RoutingContext and checks identity with belongs_to
  • Fallback checks that still take llm_router= keep working through the deprecation shim, which hands them the real Router

Low

  • run_async_fallback, async_search_with_fallbacks and async_search_with_fallbacks_helper take the context as litellm_routing_ctx, because they also take request kwargs and a request field named ctx would collide. get_matching_search_tools takes no request kwargs and uses ctx
  • explicit_allowed_fails reaches the router through the context, not settings()

Devin Review

@codspeed

codspeed Bot commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

1 warning

⚠️ 1 benchmark is dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

✅ 43 untouched benchmarks


Comparing nate_router_step4_plugins (3d33aed) with main (837f36e)1

Open in CodSpeed

Footnotes

  1. No successful run was found on main (5029d02) during the generation of this report, so 837f36e was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩

@codecov

codecov Bot commented Oct 29, 2026 •

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 97.67932% with 11 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
...llm/router_strategy/auto_router/litellm_encoder.py 79.16% 5 Missing ⚠️
litellm/router_api.py 96.51% 3 Missing ⚠️
litellm/proxy/proxy_server.py 88.88% 1 Missing ⚠️
litellm/router_deprecated.py 98.07% 1 Missing ⚠️
litellm/router_utils/search_api_router.py 92.85% 1 Missing ⚠️

📢 Thoughts on this report? Let us know!

@narmstrong-berri
narmstrong-berri force-pushed the nate_router_step4_plugins branch 5 times, most recently from 399e7c5 to ae80b60 Compare October 30, 2026 23:09
@narmstrong-berri narmstrong-berri self-assigned this Oct 30, 2026
@narmstrong-berri
narmstrong-berri marked this pull request as ready for review October 30, 2026 23:10

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 1 potential issue.

1 flag not posted on this PR by your GitHub settings — view it in Devin Review. (Configure)

Devin Review

Comment on lines +42 to +43
slack_alerting_logger: Final = ctx.observers().slack_alerting_logger
if slack_alerting_logger is None:

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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.

Suggested change
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

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

@narmstrong-berri
narmstrong-berri force-pushed the nate_router_step4_plugins branch from ae80b60 to e44f1ca Compare October 31, 2026 00:06
@narmstrong-berri
narmstrong-berri force-pushed the nate_router_step4_plugins branch 2 times, most recently from 8c10c9b to 14f6d02 Compare October 31, 2026 00:58
@narmstrong-berri
narmstrong-berri enabled stacked auto-merge (squash) October 31, 2026 01:25
stacked auto-merge was automatically disabled October 31, 2026 01:59

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
@narmstrong-berri
narmstrong-berri force-pushed the nate_router_step4_plugins branch from 95ebc38 to 3d33aed Compare October 31, 2026 02:03
@narmstrong-berri
narmstrong-berri merged commit 87d91d7 into main Oct 31, 2026
114 of 115 checks passed
@narmstrong-berri
narmstrong-berri deleted the nate_router_step4_plugins branch October 31, 2026 02:18
doramirdor added a commit to doramirdor/litellm that referenced this pull request Oct 31, 2026
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>
devin-ai-integration Bot added a commit that referenced this pull request Oct 31, 2026
Resolve the import conflict in _health_endpoints.py and follow the
get_hidden_params_dict move into litellm_core_utils (#49118).
mateo-berri added a commit to 6matt/litellm that referenced this pull request Oct 31, 2026
…_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
shudonglin pushed a commit to rayward-external/litellm that referenced this pull request Oct 31, 2026
…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.
shudonglin pushed a commit to rayward-external/litellm that referenced this pull request Oct 31, 2026
…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.
shudonglin pushed a commit to rayward-external/litellm that referenced this pull request Oct 31, 2026
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
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants