Skip to content

Conversation

AlexsanderHamir
Copy link
Collaborator

Title

[Fix] - Router: add model_name index for O(1) deployment lookups

Relevant issues

Pre-Submission checklist

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

  • I have Added testing in the tests/litellm/ directory, Adding at least 1 test is a hard requirement - see details
  • I have added a screenshot of my new test passing locally
  • My PR passes all unit tests on make test-unit
  • My PR's scope is as isolated as possible, it only solves 1 specific problem

Type

🧹 Refactoring

Changes

  • Add model_name_to_deployment_indices index for O(1) model name lookups
  • Optimize _get_all_deployments() from O(n) scan to O(1) + O(k) index lookup
  • Maintain both model_id and model_name indices in _add_model_to_list_and_index_map()

Performance Gains

Summary

Average, min and rps changed the most.

Before

Type Name # Requests # Fails Median (ms) 95%ile (ms) 99%ile (ms) Average (ms) Min (ms) Max (ms) Average size (bytes) Current RPS Current Failures/s
POST /chat/completions 102040 2 500 830 1200 532.05 67 2527 398 778.4 0
Custom LiteLLM Overhead Duration (ms) 102038 0 47 71 97 50.42 4 1438 0 778.4 0
  Aggregated 204078 2 290 700 1100 291.24 4 2527 199 1556.8 0
                         

After

Type Name # Requests # Fails Median (ms) 95%ile (ms) 99%ile (ms) Average (ms) Min (ms) Max (ms) Average size (bytes) Current RPS Current Failures/s
POST /chat/completions 115475 6 450 800 1300 486.82 23 2606 397.98 877.9 0
Custom LiteLLM Overhead Duration (ms) 115469 0 44 73 110 47.86 1 1779 0 877.9 0
  Aggregated 230944 6 200 660 1200 267.35 1 2606 198.99 1755.8 0

Add model_name_to_deployment_indices mapping to optimize _get_all_deployments()
from O(n) to O(1) + O(k) lookups.

- Add model_name_to_deployment_indices: Dict[str, List[int]]
- Add _build_model_name_index() to build/maintain the index
- Update _add_model_to_list_and_index_map() to maintain both indices
- Refactor to use idx = len(self.model_list) before append (cleaner)
- Optimize _get_all_deployments() to use index instead of linear scan
Copy link

vercel bot commented Oct 1, 2025

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

Project Deployment Preview Comments Updated (UTC)
litellm Error Error Oct 1, 2025 8:33pm

Add single comprehensive test for _build_model_name_index() function to fix
code coverage CI failure.

The test verifies:
- Index correctly maps model_name to deployment indices
- Handles multiple deployments per model_name
- Clears and rebuilds index correctly

Fixes: CI code coverage error for _build_model_name_index
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.

1 participant