fix(router): require full wildcard route matches

This commit is contained in:
ZHUI33 2026-09-20 12:18:26 +08:00
parent 8df260a13d
commit ea3f367a1f
2 changed files with 23 additions and 1 deletions

View file

@ -148,7 +148,7 @@ class PatternMatchRouter:
for pattern, llm_deployments in self.patterns.items():
if filtered_model_names is not None and pattern not in regex_filtered_model_names:
continue
pattern_match = re.match(pattern, request)
pattern_match = re.fullmatch(pattern, request)
if pattern_match:
return self._return_pattern_matched_deployments(
matched_pattern=pattern_match, deployments=llm_deployments

View file

@ -2,8 +2,11 @@
from __future__ import annotations
from typing import Final
from unittest.mock import Mock
import pytest
from litellm.router_utils import pattern_match_deployments
from litellm.router_utils.pattern_match_deployments import PatternMatchRouter, PatternUtils
@ -16,6 +19,25 @@ def _matched_models(matches: list[dict] | None) -> list[str]:
return [deployment["litellm_params"]["model"] for deployment in matches or []]
@pytest.mark.parametrize("extra_suffix", ["-extra", "\n"])
def test_route_requires_the_entire_request_to_match(extra_suffix: str) -> None:
router: Final = PatternMatchRouter()
router.add_pattern(
"gateway/*-chat",
{"model_name": "gateway/*-chat", "litellm_params": {"model": "openai/*-chat"}},
)
assert _matched_models(router.route("gateway/demo-chat")) == ["openai/demo-chat"]
assert router.route(f"gateway/demo-chat{extra_suffix}") is None
router.add_pattern(
"gateway/*",
{"model_name": "gateway/*", "litellm_params": {"model": "openai/*"}},
)
assert _matched_models(router.route("gateway/demo-chat")) == ["openai/demo-chat"]
assert _matched_models(router.route("gateway/demo-chat-extra")) == ["openai/demo-chat-extra"]
def test_get_pattern_never_resolves_declared_authenticating_providers(monkeypatch):
"""Regression: resolving a github_copilot/chatgpt name through ``get_llm_provider`` runs the
provider's OAuth device flow; the auth layer walks every wildcard router on every request, so