Skip to content

fix(proxy): only create Router when models or search_tools exist#20661

Merged
ishaan-jaff merged 1 commit intomainfrom
litellm_fix_router_creation_condition
Feb 7, 2026
Merged

fix(proxy): only create Router when models or search_tools exist#20661
ishaan-jaff merged 1 commit intomainfrom
litellm_fix_router_creation_condition

Conversation

@shin-bot-litellm
Copy link
Contributor

Summary

Fixes test_add_and_delete_deployments[0-None] by adding back a conditional check for Router creation.

Problem

PR #19818 (merged via #20205) removed the if len(_model_list) > 0 check entirely, causing Router to be created even when there are no models AND no search_tools.

Solution

Add back a conditional check that creates the Router only when:

  • There are models to route (len(_model_list) > 0), OR
  • There are search_tools configured (search_tools is truthy)
if len(_model_list) > 0 or search_tools:
    llm_router = litellm.Router(...)

Behavior

Scenario Before PR #19818 After PR #19818 (bug) After this fix
Models only ✅ Create Router ✅ Create Router ✅ Create Router
Search tools only ❌ No Router ✅ Create Router ✅ Create Router
Both ✅ Create Router ✅ Create Router ✅ Create Router
Neither ❌ No Router ⚠️ Creates Router ❌ No Router

This preserves the PR #19818 goal (search-tools-only deployments work) while avoiding unnecessary Router creation.

Testing

All 6 test cases pass locally:

tests/local_testing/test_config.py::test_add_and_delete_deployments[0-None] PASSED
tests/local_testing/test_config.py::test_add_and_delete_deployments[0-llm_router1] PASSED
tests/local_testing/test_config.py::test_add_and_delete_deployments[0-llm_router2] PASSED
tests/local_testing/test_config.py::test_add_and_delete_deployments[1-None] PASSED
tests/local_testing/test_config.py::test_add_and_delete_deployments[1-llm_router1] PASSED
tests/local_testing/test_config.py::test_add_and_delete_deployments[1-llm_router2] PASSED

Previously, PR #19818 (via #20205) removed the model_list check entirely,
causing Router to be created even with no models AND no search_tools.

This fix adds back a conditional check that creates the Router only when:
- There are models to route, OR
- There are search_tools configured

This preserves the PR #19818 goal (search-tools-only deployments) while
avoiding unnecessary Router creation when there's nothing to route.

Fixes test_add_and_delete_deployments[0-None]
@vercel
Copy link

vercel bot commented Feb 7, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
litellm Ready Ready Preview, Comment Feb 7, 2026 8:14pm

Request Review

@CLAassistant
Copy link

CLA assistant check
Thank you for your submission! We really appreciate it. Like many open source projects, we ask that you sign our Contributor License Agreement before we can accept your contribution.
You have signed the CLA already but the status is still pending? Let us recheck it.

@greptile-apps
Copy link
Contributor

greptile-apps bot commented Feb 7, 2026

Greptile Overview

Greptile Summary

This PR correctly fixes a regression from PR #19818/#20205 where the Router was being created unconditionally, even when there were no models and no search_tools to route. The fix adds back the conditional check if len(_model_list) > 0 or search_tools: before Router creation.

Changes:

  • Wraps litellm.Router() instantiation in a conditional that checks for either models or search_tools
  • Preserves the ability to create Router with search_tools only (the goal of PR fix: search tools not found when using per-request routers #19818)
  • Prevents unnecessary Router creation when both model_list and search_tools are empty

Impact:

  • Fixes failing test test_add_and_delete_deployments[0-None]
  • Aligns with custom rule 0c2a17ad-5f29-423f-a48b-371852ac4169 about avoiding unnecessary Router object creation
  • No breaking changes; all existing functionality preserved

Confidence Score: 5/5

  • This PR is safe to merge with minimal risk
  • The fix is a simple, well-tested conditional check that addresses a clear regression. The logic correctly handles all four scenarios (models only, search_tools only, both, neither) as documented in the PR description. All six test cases pass, including the previously failing test. The change is minimal, focused, and aligns with the custom rule about avoiding unnecessary Router object creation.
  • No files require special attention

Important Files Changed

Filename Overview
litellm/proxy/proxy_server.py Adds conditional check to only create Router when models or search_tools exist, preventing unnecessary Router creation

Sequence Diagram

sequenceDiagram
    participant Client as Client Request
    participant PS as ProxyServer
    participant PC as ProxyConfig
    participant R as Router
    participant DB as Database

    Client->>PS: Add/Delete Deployments
    PS->>PC: _update_llm_router(new_models)
    PC->>DB: get_config()
    DB-->>PC: config_data
    PC->>PC: parse_search_tools(config_data)
    PC->>PC: decrypt_model_list_from_db(models_list)
    
    alt llm_router is None AND master_key exists
        alt len(_model_list) > 0 OR search_tools exists
            PC->>R: Create litellm.Router(model_list, search_tools)
            R-->>PC: Router instance
            Note over PC,R: ✅ Router created when models or search_tools present
        else Neither models nor search_tools
            Note over PC: ❌ Skip Router creation (Fix)
        end
    else llm_router already exists
        PC->>R: Update search_tools if provided
        PC->>PC: _delete_deployment(db_models)
        PC->>PC: _add_deployment(db_models)
    end
    
    PC-->>PS: Router updated/skipped
    PS-->>Client: Success
Loading

Copy link
Contributor

@greptile-apps greptile-apps bot left a comment

Choose a reason for hiding this comment

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

1 file reviewed, no comments

Edit Code Review Agent Settings | Greptile

@ishaan-jaff ishaan-jaff merged commit 5fb7644 into main Feb 7, 2026
26 of 58 checks passed
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.

3 participants