mirror of
https://github.com/BerriAI/litellm.git
synced 2026-11-18 09:45:23 +00:00
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
This commit is contained in:
parent
db37977307
commit
36601ddc39
5 changed files with 124 additions and 10 deletions
|
|
@ -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,
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
|
|
@ -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"},
|
||||
|
|
|
|||
|
|
@ -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")
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue