From 36601ddc39ff36e2eb7fc849b8c1eddc3e3f64ce Mon Sep 17 00:00:00 2001 From: wofiporia <1029687661@qq.com> Date: Fri, 18 Sep 2026 10:58:58 +0800 Subject: [PATCH] fix(proxy): return 400 when Jev auto router is missing TypeSafe API key - Require api_key or TYPESAFE_API_KEY in JevClassifierConfig validator - Wrap ComplexityRouter instantiation in try-except block in preview_auto_router_routing to surface 400 instead of 500 - Add tests covering validate_complexity_router_config, test_routing and JevClassifierConfig key validation Fixes #41713 --- .../auto_router_endpoints.py | 15 ++-- .../complexity_router/config.py | 9 +++ .../test_auto_router_endpoints.py | 72 +++++++++++++++++++ .../test_auto_router_permissions.py | 3 +- .../complexity_router/test_jev_classifier.py | 35 ++++++++- 5 files changed, 124 insertions(+), 10 deletions(-) diff --git a/litellm/proxy/management_endpoints/auto_router_endpoints.py b/litellm/proxy/management_endpoints/auto_router_endpoints.py index 200ed6c3bf3..a72a912870e 100644 --- a/litellm/proxy/management_endpoints/auto_router_endpoints.py +++ b/litellm/proxy/management_endpoints/auto_router_endpoints.py @@ -479,14 +479,6 @@ async def preview_auto_router_routing( llm_router=llm_router, ) - complexity_router: Final = ComplexityRouter( - model_name=data.router_name, - litellm_router_instance=llm_router, - complexity_router_config=data.complexity_router_config.model_dump(exclude_none=True), - default_model=data.default_model, - derive_savings_baseline=False, - ) - request_kwargs: Final = LiteLLMProxyRequestSetup.add_user_api_key_auth_to_request_metadata( data=request_data, user_api_key_dict=actor, @@ -495,6 +487,13 @@ async def preview_auto_router_routing( refresh_proxy_server_request_body_snapshot(request_kwargs) try: + complexity_router: Final = ComplexityRouter( + model_name=data.router_name, + litellm_router_instance=llm_router, + complexity_router_config=data.complexity_router_config.model_dump(exclude_none=True), + default_model=data.default_model, + derive_savings_baseline=False, + ) hook_response: Final = await complexity_router.async_pre_routing_hook( model=data.router_name, request_kwargs=request_kwargs, diff --git a/litellm/router_strategy/complexity_router/config.py b/litellm/router_strategy/complexity_router/config.py index aa39dff8c53..2a7417f810c 100644 --- a/litellm/router_strategy/complexity_router/config.py +++ b/litellm/router_strategy/complexity_router/config.py @@ -713,6 +713,15 @@ class JevClassifierConfig(BaseModel): ) return self + @model_validator(mode="after") + def _require_api_key(self) -> "JevClassifierConfig": + from litellm.secret_managers.main import get_secret_str + + api_key: Final = self.api_key or get_secret_str("TYPESAFE_API_KEY") + if not api_key: + raise ValueError("jev_classifier_config.api_key or TYPESAFE_API_KEY is required for classifier_type 'jev'") + return self + MAX_CUSTOM_PATTERN_REPEAT: Final[int] = 64 MAX_CUSTOM_PATTERN_WORK: Final[int] = 2048 diff --git a/tests/test_litellm/proxy/management_endpoints/test_auto_router_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_auto_router_endpoints.py index 067f30c2fd7..7a60a3cf3a3 100644 --- a/tests/test_litellm/proxy/management_endpoints/test_auto_router_endpoints.py +++ b/tests/test_litellm/proxy/management_endpoints/test_auto_router_endpoints.py @@ -2693,6 +2693,78 @@ async def test_validate_config_returns_the_write_gates_verdict_without_saving(): assert rejected.error is not None and "newline" in rejected.error +@pytest.mark.asyncio +async def test_jev_classifier_missing_key_validation_and_test_routing(monkeypatch: pytest.MonkeyPatch): + """A Jev complexity router requires TYPESAFE_API_KEY or jev_classifier_config.api_key; + without one, validate reports valid=False with the missing key error, and test_routing + rejects with 400.""" + from litellm.proxy import proxy_server + from litellm.proxy.management_endpoints.auto_router_endpoints import ( + preview_auto_router_routing, + validate_complexity_router_config, + ) + from litellm.router_strategy.complexity_router.config import JevClassifierConfig + from litellm.types.management_endpoints.auto_router_endpoints import ( + AutoRouterRoutingTestRequest, + ComplexityRouterConfigValidationRequest, + RequestComplexityRouterConfig, + ) + + monkeypatch.delenv("TYPESAFE_API_KEY", raising=False) + + rejected = await validate_complexity_router_config( + ComplexityRouterConfigValidationRequest( + complexity_router_config={ + "tiers": TIERS, + "classifier_type": "jev", + "jev_classifier_config": {"model": "jev-latest"}, + } + ), + ADMIN, + ) + assert rejected.valid is False + assert rejected.error is not None + assert "jev_classifier_config.api_key or TYPESAFE_API_KEY is required for classifier_type 'jev'" in rejected.error + + valid = await validate_complexity_router_config( + ComplexityRouterConfigValidationRequest( + complexity_router_config={ + "tiers": TIERS, + "classifier_type": "jev", + "jev_classifier_config": {"model": "jev-latest", "api_key": "sk-provided"}, + } + ), + ADMIN, + ) + assert valid.valid is True + assert valid.error is None + + with pytest.raises(ValidationError) as schema_exc: + _request( + "what is 2+2", + classifier_type="jev", + jev_classifier_config={"model": "jev-latest"}, + ) + assert "jev_classifier_config.api_key or TYPESAFE_API_KEY is required for classifier_type 'jev'" in str(schema_exc.value) + + monkeypatch.setattr(proxy_server, "llm_router", _router()) + bad_config = RequestComplexityRouterConfig.model_construct( + tiers=TIERS, + classifier_type="jev", + jev_classifier_config=JevClassifierConfig.model_construct(model="jev-latest"), + ) + bad_request = AutoRouterRoutingTestRequest.model_construct( + prompt="what is 2+2", + messages=[{"role": "user", "content": "what is 2+2"}], + complexity_router_config=bad_config, + router_name="test-router", + ) + with pytest.raises(HTTPException) as http_exc: + await preview_auto_router_routing(bad_request, ADMIN, ROUTING_HTTP_REQUEST) + assert http_exc.value.status_code == 400 + assert "jev_classifier_config.api_key or TYPESAFE_API_KEY is required for classifier_type 'jev'" in str(http_exc.value.detail) + + @pytest.mark.asyncio async def test_routing_test_never_confirms_models_the_caller_cannot_use(monkeypatch: pytest.MonkeyPatch): """routed_model_configured must not be an existence oracle for the whole proxy: a team diff --git a/tests/test_litellm/proxy/management_helpers/test_auto_router_permissions.py b/tests/test_litellm/proxy/management_helpers/test_auto_router_permissions.py index 2884efb0825..81577964e2e 100644 --- a/tests/test_litellm/proxy/management_helpers/test_auto_router_permissions.py +++ b/tests/test_litellm/proxy/management_helpers/test_auto_router_permissions.py @@ -151,7 +151,8 @@ def test_members_cannot_move_the_jev_classifier_off_the_proxys_typesafe_account( assert denied.value.detail == f"Invalid member auto-router configuration at {rejected_at}." -def test_members_can_still_tune_the_jev_classifier() -> None: +def test_members_can_still_tune_the_jev_classifier(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.setenv("TYPESAFE_API_KEY", "sk-proxy-typesafe") validated: Final = validate_member_auto_router_config( { "tiers": {"SIMPLE": "allowed"}, diff --git a/tests/test_litellm/router_strategy/complexity_router/test_jev_classifier.py b/tests/test_litellm/router_strategy/complexity_router/test_jev_classifier.py index f27729d29e8..3472259a1cb 100644 --- a/tests/test_litellm/router_strategy/complexity_router/test_jev_classifier.py +++ b/tests/test_litellm/router_strategy/complexity_router/test_jev_classifier.py @@ -37,11 +37,44 @@ def test_jev_config_is_rejected_for_other_classifier_types() -> None: with pytest.raises(ValueError, match="has no effect"): ComplexityRouterConfig.model_validate( { - "jev_classifier_config": {}, + "jev_classifier_config": {"api_key": "sk-own"}, } ) +def test_jev_config_requires_api_key_or_environment_secret(monkeypatch: pytest.MonkeyPatch) -> None: + monkeypatch.delenv("TYPESAFE_API_KEY", raising=False) + with pytest.raises( + ValueError, match=r"jev_classifier_config\.api_key or TYPESAFE_API_KEY is required for classifier_type 'jev'" + ): + JevClassifierConfig() + + with pytest.raises( + ValueError, match=r"jev_classifier_config\.api_key or TYPESAFE_API_KEY is required for classifier_type 'jev'" + ): + ComplexityRouterConfig.model_validate( + { + "classifier_type": "jev", + "jev_classifier_config": {"model": "jev-latest"}, + } + ) + + configured = JevClassifierConfig(api_key="sk-direct") + assert configured.api_key == "sk-direct" + + monkeypatch.setenv("TYPESAFE_API_KEY", "sk-env") + from_env = JevClassifierConfig() + assert from_env.api_key is None + + router_cfg = ComplexityRouterConfig.model_validate( + { + "classifier_type": "jev", + "jev_classifier_config": {"model": "jev-latest"}, + } + ) + assert router_cfg.jev_classifier_config is not None + + def test_jev_instructions_reject_blank_values() -> None: with pytest.raises(ValueError, match="instructions must be non-empty"): JevClassifierConfig(instructions=" \t")