Skip to content

fix(router): resolve route candidate ids through the router's own resolver - #40491

Merged
tin-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_candidate_route_precedence
Sep 9, 2026
Merged

tin-berri merged 1 commit into
litellm_internal_stagingfrom
litellm_candidate_route_precedence

Conversation

@tin-berri

Copy link
Copy Markdown
Contributor

TLDR

Problem this solves:

  • Route candidate resolution did not match how the router actually routes
  • Wildcard and team-public routes could be misjudged as a different group

How it solves it:

  • Reuse the router's own resolver instead of a hand-rolled union
  • Covers provider-qualified patterns, team routes, and the default deployment

User Flow

Before: a developer replays a turn's encrypted reasoning against a bare model name that a wildcard deployment serves, and while that deployment is briefly rate-limited the gateway loses their reasoning or 503s as if the model did not exist

  1. They send POST https://litellm-domain/v1/responses with include: ["reasoning.encrypted_content"] and the prior turn's items, model set to a bare name (for example gpt-5-codex) that only a wildcard deployment like openai/* serves
  2. That deployment is momentarily cooling down from an upstream 429
  3. The gateway does not recognize the wildcard deployment as serving this bare name, so it treats the turn as a switch to a different model and drops the encrypted reasoning, or returns 503 with text about no deployment being available

After: the same replay is recognized as the same model group, so it fails fast with a retriable signal and keeps the reasoning for the retry

  1. They send the same POST https://litellm-domain/v1/responses
  2. The deployment is still cooling down
  3. The gateway recognizes the wildcard deployment as the one serving this bare name, returns 429 with a Retry-After header, and leaves the encrypted reasoning in place so the retry after the window keeps full context

Relevant issues

Follow-up to #40280, addressing a Cursor Bugbot finding on that PR ("Candidate IDs miss router resolution paths")

Linear ticket

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/test_litellm/<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.)

Final Attestation

  • The tests check the right things, including the edge cases, and regressions in the respective real-world customer use-cases are not possible after this PR

🤖 Generated with Claude Code

https://claude.ai/code/session_01KAumQbhzk6jdWWHFLA8Jar

…olver

get_candidate_model_ids_for_route (added in #40280 for the encrypted-content affinity
check) reconstructed the candidate pool by unioning the model_name and team indexes with
pattern_router.route. That diverged from how the router actually resolves a route: it took
a union instead of the first matching path, and pattern_router.route only matches the
literal name, so a provider-qualified pattern (matched by get_deployments_by_pattern, which
retries the {provider}/{model} form) was missed and the default deployment was ignored.

For an affinity follow-up on a wildcard or team-public route, that mismatch could strip
encrypted reasoning on a same-group cooldown, or return a 503 on a real cross-path switch.

Delegate the non-model_name case to _try_early_resolve_deployments_for_model_not_in_names,
the same resolver _common_checks_available_deployment uses, so candidate membership follows
the router's real precedence. With include_team_models left off it stays read-only and does
not raise. Behavior for concrete model groups and routing groups is unchanged.

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KAumQbhzk6jdWWHFLA8Jar
@tin-berri
tin-berri requested a review from a team September 9, 2026 23:22
@tin-berri

Copy link
Copy Markdown
Contributor Author

This is a follow-up to #40280 addressing the Cursor Bugbot candidate-resolution finding

@greptileai can you reply here with a score and why that score?

bugbot run

@greptile-apps

greptile-apps Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Score: 5/5

The fix correctly delegates candidate discovery to the router’s existing resolution path instead of maintaining a second, potentially divergent union of indexes. It preserves alias and routing-group precedence, then uses the early resolver for team routes, provider-qualified wildcard patterns, team pattern routes, and the default deployment before falling back to the model-name indexes. The Mapping/sequence handling matches the resolver’s possible return shapes, and the new regression test covers the important bare-name → openai/* case that triggered the Bugbot finding. The change is narrowly scoped, read-only, and the test update directly guards the reported regression.

@codspeed

codspeed Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Merging this PR will not alter performance

✅ 31 untouched benchmarks


Comparing litellm_candidate_route_precedence (66a745b) with litellm_internal_staging (8b2983b)

Open in CodSpeed

@greptile-apps

greptile-apps Bot commented Sep 9, 2026

Copy link
Copy Markdown
Contributor

Greptile Summary

This PR aligns route candidate ID discovery with the Router’s actual route-resolution precedence

  • Reuses the existing early-resolution helper for team, wildcard, team-pattern, and default routes
  • Handles mapping-shaped default deployments when extracting deployment IDs
  • Adds regression coverage for resolving a bare model name through a provider-qualified wildcard pattern

Confidence Score: 5/5

The PR appears safe to merge because candidate discovery now follows the same resolver and precedence as request routing

No actionable failures remain; known model names retain precedence, helper return shapes are handled correctly, and the regression test detects the previous bare-name wildcard mismatch

Important Files Changed

Filename Overview
litellm/router.py Replaces hand-built candidate unions with the Router’s existing ordered route resolver while safely normalizing its deployment shapes
tests/test_litellm/test_router.py Adds focused coverage for provider-qualified wildcard resolution from an unprefixed model name

Reviews (1): Last reviewed commit: "fix(router): resolve route candidate ids..." | Re-trigger Greptile

@tin-berri
tin-berri enabled auto-merge (squash) September 9, 2026 23:25
@tin-berri
tin-berri disabled auto-merge September 9, 2026 23:27
@codecov

codecov Bot commented Sep 9, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

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