Implementation:BerriAI Litellm Pattern Match Deployments
| Attribute | Value |
|---|---|
| Sources | litellm/router_utils/pattern_match_deployments.py |
| Domains | Router, Utilities, Wildcard Routing, Regex Matching |
| last_updated | 2026-02-15 16:00 GMT |
Overview
The Pattern Match Deployments module provides wildcard and regex-based routing for LLM model names, enabling flexible deployment matching patterns like openai/* or bedrock/meta.llama3*.
Description
This module contains two classes: PatternUtils (static utility methods for pattern analysis) and PatternMatchRouter (the main routing class). The PatternMatchRouter stores a mapping from regex patterns to lists of deployment dictionaries. When a model name is requested, it converts wildcard patterns to regex (replacing * with (.*)), sorts patterns by specificity (longer and more complex patterns first), and returns the first matching set of deployments. Critically, it dynamically rewrites deployment model names by substituting captured wildcard groups into the deployment's litellm model pattern, enabling patterns like llmengine/* to map to openai/* where the wildcard segment is preserved.
Usage
Import PatternMatchRouter when configuring provider-specific wildcard routing in the LiteLLM proxy or router. Patterns are typically added during router initialization from the model list configuration.
Code Reference
Source Location
litellm/router_utils/pattern_match_deployments.py
Classes
class PatternUtils:
@staticmethod
def calculate_pattern_specificity(pattern: str) -> Tuple[int, int]:
@staticmethod
def sorted_patterns(patterns: Dict[str, List[Dict]]) -> List[Tuple[str, List[Dict]]]:
class PatternMatchRouter:
def __init__(self):
self.patterns: Dict[str, List] = {}
Key Methods
| Method | Signature | Description |
|---|---|---|
add_pattern |
def add_pattern(self, pattern: str, llm_deployment: Dict) |
Converts a wildcard pattern to regex and adds the deployment mapping |
route |
def route(self, request: Optional[str], filtered_model_names: Optional[List[str]] = None) -> Optional[List[Dict]] |
Matches a requested model name against stored patterns and returns matching deployments with rewritten model names |
set_deployment_model_name |
@staticmethod def set_deployment_model_name(matched_pattern: Match, litellm_deployment_litellm_model: str) -> str |
Substitutes wildcard capture groups into the deployment model name |
get_pattern |
def get_pattern(self, model: str, custom_llm_provider: Optional[str] = None) -> Optional[List[Dict]] |
Checks if a pattern exists for the given model, trying both bare and provider-prefixed forms |
get_deployments_by_pattern |
def get_deployments_by_pattern(self, model: str, custom_llm_provider: Optional[str] = None) -> List[Dict] |
Returns matching deployments or an empty list |
Import
from litellm.router_utils.pattern_match_deployments import PatternMatchRouter, PatternUtils
I/O Contract
Inputs (route)
| Parameter | Type | Description |
|---|---|---|
request |
Optional[str] |
The requested model name from the user (e.g., "openai/gpt-4")
|
filtered_model_names |
Optional[List[str]] |
If provided, only match patterns corresponding to these model names |
Outputs (route)
| Return Type | Description |
|---|---|
Optional[List[Dict]] |
List of deployment dictionaries with dynamically rewritten model names, or None if no pattern matches
|
Usage Examples
from litellm.router_utils.pattern_match_deployments import PatternMatchRouter
router = PatternMatchRouter()
# Add wildcard patterns
router.add_pattern("openai/*", {
"model_name": "openai/*",
"litellm_params": {"model": "openai/*", "api_key": "sk-..."},
"model_info": {"id": "openai-wildcard"},
})
router.add_pattern("bedrock/meta.llama3*", {
"model_name": "bedrock/meta.llama3*",
"litellm_params": {"model": "bedrock/meta.llama3*"},
"model_info": {"id": "bedrock-llama"},
})
# Route a request - returns deployments with model rewritten to "openai/gpt-4"
result = router.route("openai/gpt-4")
# Route with multi-wildcard pattern
router.add_pattern("llmengine/fo::*::static::*", {
"model_name": "llmengine/fo::*::static::*",
"litellm_params": {"model": "openai/fo::*::static::*"},
"model_info": {"id": "multi-wildcard"},
})
result = router.route("llmengine/fo::bar::static::baz")
# Deployment model becomes "openai/fo::bar::static::baz"
Related Pages
- BerriAI_Litellm_Tag_Based_Routing - Tag-based deployment filtering (complementary filtering)
- BerriAI_Litellm_Simple_Shuffle_Strategy - Random selection applied after pattern matching
- BerriAI_Litellm_Batch_Utils - Batch utilities including model name replacement