The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix: reject unknown runtime router settings
mateo0608f0a00f2c
diff --git a/litellm/constants.py b/litellm/constants.pyindex 1bd977dd9a9..a7506cb6378 100644--- a/litellm/constants.py+++ b/litellm/constants.py@@ -38,6 +38,28 @@ DEFAULT_MAX_TOKENS: Final = int(os.getenv("DEFAULT_MAX_TOKENS", 4096))DEFAULT_ALLOWED_FAILS: Final = int(os.getenv("DEFAULT_ALLOWED_FAILS", 3))DEFAULT_REDIS_SYNC_INTERVAL: Final = int(os.getenv("DEFAULT_REDIS_SYNC_INTERVAL", 1))DEFAULT_COOLDOWN_TIME_SECONDS: Final = int(os.getenv("DEFAULT_COOLDOWN_TIME_SECONDS", 5))+RUNTIME_UPDATABLE_ROUTER_SETTINGS: Final[frozenset[str]] = frozenset(+ {+ "routing_strategy_args",+ "routing_strategy",+ "routing_groups",+ "allowed_fails",+ "cooldown_time",+ "num_retries",+ "timeout",+ "max_retries",+ "retry_after",+ "fallbacks",+ "context_window_fallbacks",+ "retry_policy",+ "model_group_retry_policy",+ "model_group_alias",+ "enable_weighted_failover",+ "enable_tag_filtering",+ "tag_routing_prefix",+ "optional_pre_call_checks",+ }+)DEFAULT_REPLICATE_POLLING_RETRIES: Final = int(os.getenv("DEFAULT_REPLICATE_POLLING_RETRIES", 5))DEFAULT_REPLICATE_POLLING_DELAY_SECONDS: Final = int(os.getenv("DEFAULT_REPLICATE_POLLING_DELAY_SECONDS", 1))DEFAULT_IMAGE_TOKEN_COUNT: Final = int(os.getenv("DEFAULT_IMAGE_TOKEN_COUNT", 250))diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 77a80ea0052..9de6b38265a 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -39,7 +39,7 @@ from typing import (import anyioimport websocketsimport websockets.exceptions-from pydantic import BaseModel, Json, JsonValue, ValidationError+from pydantic import BaseModel, Json, JsonValue, TypeAdapter, ValidationErrorfrom typing_extensions import NotRequired, ReadOnly, assert_neverfrom litellm._uuid import uuid@@ -60,6 +60,7 @@ from litellm.constants import (LITELLM_SETTINGS_SAFE_DB_OVERRIDES,LITELLM_UI_ALLOW_HEADERS,LITELLM_UI_SESSION_DURATION,+ RUNTIME_UPDATABLE_ROUTER_SETTINGS,)from litellm.litellm_core_utils.litellm_logging import (_init_custom_logger_compatible_class,@@ -16207,6 +16208,7 @@ async def invitation_delete()async def update_config(config_info: ConfigYAML,+ request: Request,user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth),):"""@@ -16218,6 +16220,23 @@ async def update_config(a side effect of an unrelated update."""global llm_router, llm_model_list, general_settings, proxy_config, proxy_logging_obj, master_key, prisma_client+ request_body: Final[Mapping[str, JsonValue]] = TypeAdapter(Mapping[str, JsonValue]).validate_python(+ await request.json()+ )+ raw_router_settings: Final = request_body.get("router_settings")+ if isinstance(raw_router_settings, dict):+ unsupported_router_settings: Final = sorted(set(raw_router_settings) - RUNTIME_UPDATABLE_ROUTER_SETTINGS)+ if unsupported_router_settings:+ raise HTTPException(+ status_code=400,+ detail={+ "error": (+ f"Unsupported router settings: {', '.join(unsupported_router_settings)} "+ "are not runtime-updatable router settings"+ )+ },+ )+try:if user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN:raise HTTPException(status_code=403, detail="Only proxy admins can update config")diff --git a/litellm/router.py b/litellm/router.pyindex 23d8907fb49..fc4f815170d 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -50,6 +50,7 @@ from litellm.constants import (DEFAULT_HEALTH_CHECK_INTERVAL,DEFAULT_HEALTH_CHECK_STALENESS_MULTIPLIER,DEFAULT_MAX_LRU_CACHE_SIZE,+ RUNTIME_UPDATABLE_ROUTER_SETTINGS,SESSION_DEPLOYMENT_AFFINITY_TTL_METADATA_KEY,)from litellm.integrations.custom_logger import CustomLogger@@ -2072,6 +2073,10 @@ class Router:if _callback is None:continue+ if self.optional_callbacks is not None and any(+ isinstance(callback, type(_callback)) for callback in self.optional_callbacks+ ):+ continueif self.optional_callbacks is None:self.optional_callbacks = []self.optional_callbacks.append(_callback)@@ -11331,27 +11336,6 @@ class Router:"""Update the router settings."""- # only the following settings are allowed to be configured- _allowed_settings: Final = [- "routing_strategy_args",- "routing_strategy",- "routing_groups",- "allowed_fails",- "cooldown_time",- "num_retries",- "timeout",- "max_retries",- "retry_after",- "fallbacks",- "context_window_fallbacks",- "retry_policy",- "model_group_retry_policy",- "model_group_alias",- "enable_weighted_failover",- "enable_tag_filtering",- "tag_routing_prefix",- ]-_int_settings: Final = ["timeout","num_retries",@@ -11364,13 +11348,15 @@ class Router:rebuild_routing_groups = Falserelink_lar1_from_args = Falsefor var in kwargs:- if var in _allowed_settings:+ if var in RUNTIME_UPDATABLE_ROUTER_SETTINGS:if var in _int_settings:_casted_value = int(kwargs[var])setattr(self, var, _casted_value)elif var == "routing_groups":self._routing_groups_input = kwargs[var]rebuild_routing_groups = True+ elif var == "optional_pre_call_checks":+ self.add_optional_pre_call_checks(kwargs[var])elif var == "retry_policy":value = kwargs[var]if isinstance(value, dict):diff --git a/litellm/types/router.py b/litellm/types/router.pyindex e0957383aac..2a5f264cee3 100644--- a/litellm/types/router.py+++ b/litellm/types/router.py@@ -106,6 +106,20 @@ class RetryPolicy(BaseModel):InternalServerErrorRetries: int | None = None+OptionalPreCallChecks = list[+ Literal[+ "prompt_caching",+ "router_budget_limiting",+ "responses_api_deployment_check",+ "deployment_affinity",+ "session_affinity",+ "forward_client_headers_by_model_group",+ "enforce_model_rate_limits",+ "encrypted_content_affinity",+ ]+]++class UpdateRouterConfig(BaseModel):"""Set of params that you can modify via `router.update_settings()`.@@ -128,6 +142,7 @@ class UpdateRouterConfig(BaseModel):model_group_alias: dict[str, str | dict] | None = {}enable_tag_filtering: bool | None = Nonetag_routing_prefix: str | None = None+ optional_pre_call_checks: OptionalPreCallChecks | None = Nonemodel_config = ConfigDict(protected_namespaces=())@@ -869,20 +884,6 @@ class FallbackAccessCheck(Protocol):async def __call__(self, *, model: str, request_kwargs: Mapping[str, object], llm_router: "Router") -> bool: ...-OptionalPreCallChecks = list[- Literal[- "prompt_caching",- "router_budget_limiting",- "responses_api_deployment_check",- "deployment_affinity",- "session_affinity",- "forward_client_headers_by_model_group",- "enforce_model_rate_limits",- "encrypted_content_affinity",- ]-]--class LiteLLM_RouterFileObject(TypedDict, total=False):"""Tracking the litellm params hash, used for mapping the file id to the right modeldiff --git a/tests/test_litellm/proxy/proxy_server/test_routes_config.py b/tests/test_litellm/proxy/proxy_server/test_routes_config.pyindex ad3c470acf3..0df8fb663e2 100644--- a/tests/test_litellm/proxy/proxy_server/test_routes_config.py+++ b/tests/test_litellm/proxy/proxy_server/test_routes_config.py@@ -60,6 +60,45 @@ def test_config_update_happy_admin(client, auth_as, mock_prisma, monkeypatch):assert normalize(response.json()) == {"message": "Config updated successfully"}+def test_config_update_persists_optional_pre_call_checks(client, auth_as, mock_prisma, monkeypatch):+ from litellm.proxy import proxy_server as ps+ from litellm.proxy._types import LitellmUserRoles++ table = _install_litellm_config(mock_prisma)+ monkeypatch.setattr(ps, "prisma_client", mock_prisma)+ fake_proxy_config = MagicMock()+ fake_proxy_config.add_deployment = AsyncMock()+ monkeypatch.setattr(ps, "proxy_config", fake_proxy_config)++ with auth_as(LitellmUserRoles.PROXY_ADMIN):+ response = client.post(+ "/config/update",+ json={"router_settings": {"optional_pre_call_checks": ["prompt_caching"]}},+ )++ assert response.status_code == 200+ persisted = json.loads(table.upsert.call_args.kwargs["data"]["create"]["param_value"])+ assert persisted["optional_pre_call_checks"] == ["prompt_caching"]+++def test_config_update_rejects_unknown_router_setting(client, auth_as, mock_prisma, monkeypatch):+ from litellm.proxy import proxy_server as ps+ from litellm.proxy._types import LitellmUserRoles++ table = _install_litellm_config(mock_prisma)+ monkeypatch.setattr(ps, "prisma_client", mock_prisma)++ with auth_as(LitellmUserRoles.PROXY_ADMIN):+ response = client.post(+ "/config/update",+ json={"router_settings": {"optional_precall_checks": ["prompt_caching"]}},+ )++ assert response.status_code == 400+ assert "optional_precall_checks" in response.json()["detail"]["error"]+ table.upsert.assert_not_called()++def test_config_update_non_admin_forbidden(client, auth_as, mock_prisma, monkeypatch):"""POST /config/update by a non-admin caller is rejected; the errorsurfaces as a ProxyException with the admin-only message."""diff --git a/tests/test_litellm/test_router_retry_policy_update.py b/tests/test_litellm/test_router_retry_policy_update.pyindex 1b98b8c1ae8..1b014cd8401 100644--- a/tests/test_litellm/test_router_retry_policy_update.py+++ b/tests/test_litellm/test_router_retry_policy_update.py@@ -26,8 +26,8 @@ from unittest.mock import AsyncMock, MagicMockimport pytestfrom pydantic import ValidationError-import litellm+from litellm.router_utils.pre_call_checks.prompt_caching_deployment_check import PromptCachingDeploymentCheckfrom litellm.types.router import RetryPolicy, UpdateRouterConfig# ---------------------------------------------------------------------------@@ -100,6 +100,19 @@ def _build_router() -> litellm.Router:)+def test_update_settings_adds_optional_pre_call_check_once():+ router = _build_router()++ router.update_settings(num_retries=7, optional_pre_call_checks=["prompt_caching"])+ router.update_settings(optional_pre_call_checks=["prompt_caching"])++ prompt_caching_callbacks = [+ callback for callback in router.optional_callbacks if isinstance(callback, PromptCachingDeploymentCheck)+ ]+ assert len(prompt_caching_callbacks) == 1+ assert router.num_retries == 7++def test_update_settings_persists_retry_policy_dict():"""When the proxy's ``_add_router_settings_from_db_config`` calls``llm_router.update_settings(retry_policy={...})`` after reading the@@ -228,7 +241,7 @@ async def test_config_update_persists_and_reads_back_retry_policy(monkeypatch):"""The exact global retry_policy save the UI performs must survive thereal ``/config/update`` -> DB -> apply -> ``/get/config/callbacks`` path,not snap back to the ``num_retries`` fallback the ticket reported."""- import litellm.proxy.proxy_server as proxy_server+ from litellm.proxy import proxy_serverfrom litellm.proxy._types import ConfigYAML, LitellmUserRoles, UserAPIKeyAuthrouter = _build_router()@@ -255,8 +268,12 @@ async def test_config_update_persists_and_reads_back_retry_policy(monkeypatch):RateLimitErrorRetries=7,))+ request = MagicMock()+ request.json = AsyncMock(return_value={"router_settings": {"retry_policy": posted.model_dump()}})+await proxy_server.update_config(config_info=ConfigYAML(router_settings=posted),+ request=request,user_api_key_dict=UserAPIKeyAuth(user_role=LitellmUserRoles.PROXY_ADMIN, api_key="sk-1234"),)diff --git a/ui/litellm-dashboard/src/lib/http/schema.d.ts b/ui/litellm-dashboard/src/lib/http/schema.d.tsindex 6f044fec3f3..bde7fd611d5 100644--- a/ui/litellm-dashboard/src/lib/http/schema.d.ts+++ b/ui/litellm-dashboard/src/lib/http/schema.d.ts@@ -37473,6 +37473,8 @@ export interface components {} | null;/** Num Retries */num_retries?: number | null;+ /** Optional Pre Call Checks */+ optional_pre_call_checks?: ("prompt_caching" | "router_budget_limiting" | "responses_api_deployment_check" | "deployment_affinity" | "session_affinity" | "forward_client_headers_by_model_group" | "enforce_model_rate_limits" | "encrypted_content_affinity")[] | null;/** Retry After */retry_after?: number | null;retry_policy?: components["schemas"]["RetryPolicy"] | null;