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
Get and Update route for UI Settings
yuneng-jiang3683614a6b21
diff --git a/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py b/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.pyindex af68100910a..cf0960b4423 100644--- a/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py+++ b/litellm/proxy/ui_crud_endpoints/proxy_setting_endpoints.py@@ -63,6 +63,25 @@ class UIThemeSettingsResponse(SettingsResponse):pass+class UISettings(BaseModel):+ """Configuration for UI-specific flags"""++ disable_model_add_for_internal_users: bool = Field(+ default=False,+ description="If true, internal users cannot add models from the UI",+ )+++class UISettingsResponse(SettingsResponse):+ """Response model for UI settings"""++ pass+++# Allowlist of UI settings that can be stored+ALLOWED_UI_SETTINGS_FIELDS = {"disable_model_add_for_internal_users"}++@router.get("/get/allowed_ips",tags=["Budget & Spend Tracking"],@@ -648,6 +667,87 @@ async def update_ui_theme_settings(theme_config: UIThemeConfig):}+ "/get/ui_settings",+ tags=["UI Settings"],+ dependencies=[Depends(user_api_key_auth)],+ response_model=UISettingsResponse,+)+async def get_ui_settings():+ """+ Get UI-specific configuration flags.+ All authenticated users can fetch these settings for client-side behavior.+ """+ from litellm.proxy.proxy_server import proxy_config++ config = await proxy_config.get_config()++ # Sanitize any unexpected keys from persisted config before returning+ if (+ "litellm_settings" in config+ and isinstance(config["litellm_settings"].get("ui_settings"), dict)+ ):+ config["litellm_settings"]["ui_settings"] = {+ k: v+ for k, v in config["litellm_settings"]["ui_settings"].items()+ if k in ALLOWED_UI_SETTINGS_FIELDS+ }++ return await _get_settings_with_schema(+ settings_key="ui_settings",+ settings_class=UISettings,+ config=config,+ )+++ "/update/ui_settings",+ tags=["UI Settings"],+ dependencies=[Depends(user_api_key_auth)],+)+async def update_ui_settings(+ settings: UISettings, user_api_key_dict: UserAPIKeyAuth = Depends(user_api_key_auth)+):+ """+ Update UI-specific configuration flags.+ Only proxy admins are allowed to modify these settings.+ """+ from litellm.proxy.proxy_server import proxy_config, store_model_in_db++ if user_api_key_dict.user_role != LitellmUserRoles.PROXY_ADMIN:+ raise HTTPException(+ status_code=403, detail="Only proxy admins can update UI settings."+ )++ if store_model_in_db is not True:+ raise HTTPException(+ status_code=500,+ detail={+ "error": "Set `'STORE_MODEL_IN_DB='True'` in your env to enable this feature."+ },+ )++ config = await proxy_config.get_config()++ if "litellm_settings" not in config:+ config["litellm_settings"] = {}++ settings_dict = settings.model_dump(exclude_none=True)++ # Enforce allowlist and drop anything unexpected+ ui_settings = {+ k: v for k, v in settings_dict.items() if k in ALLOWED_UI_SETTINGS_FIELDS+ }+ config["litellm_settings"]["ui_settings"] = ui_settings++ await proxy_config.save_config(new_config=config)++ return {+ "message": "UI settings updated successfully",+ "status": "success",+ "settings": ui_settings,+ }+@router.post("/upload/logo",tags=["UI Theme Settings"],diff --git a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.pyindex eff59729fef..5730852fc98 100644--- a/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py+++ b/tests/test_litellm/proxy/ui_crud_endpoints/test_proxy_setting_endpoints.py@@ -622,6 +622,88 @@ class TestProxySettingEndpoints:assert "UI_LOGO_PATH" in updated_config["environment_variables"]assert mock_proxy_config["save_call_count"]() == 1+ def test_get_ui_settings(self, mock_proxy_config, mock_auth):+ """Test retrieving UI settings with allowlist sanitization"""+ mock_proxy_config["config"]["litellm_settings"]["ui_settings"] = {+ "disable_model_add_for_internal_users": True,+ "unexpected_flag": True,+ }++ response = client.get("/get/ui_settings")++ assert response.status_code == 200+ data = response.json()+ assert data["values"]["disable_model_add_for_internal_users"] is True+ assert "unexpected_flag" not in data["values"]+ assert "disable_model_add_for_internal_users" in data["field_schema"]["properties"]++ def test_update_ui_settings_allowlisted_value(+ self, mock_proxy_config, mock_auth, monkeypatch+ ):+ """Test updating UI settings with an allowlisted field"""++ class MockUser:+ def __init__(self, user_role):+ self.user_role = user_role++ async def mock_admin_auth():+ return MockUser(LitellmUserRoles.PROXY_ADMIN)++ monkeypatch.setattr(+ "litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints.user_api_key_auth",+ mock_admin_auth,+ )+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)++ payload = {"disable_model_add_for_internal_users": True}++ response = client.patch("/update/ui_settings", json=payload)++ assert response.status_code == 200+ data = response.json()+ assert data["status"] == "success"+ assert data["settings"]["disable_model_add_for_internal_users"] is True++ ui_settings = mock_proxy_config["config"]["litellm_settings"]["ui_settings"]+ assert ui_settings["disable_model_add_for_internal_users"] is True+ assert mock_proxy_config["save_call_count"]() == 1++ def test_update_ui_settings_ignores_non_allowlisted_value(+ self, mock_proxy_config, mock_auth, monkeypatch+ ):+ """Test non-allowlisted UI settings are ignored on update"""++ class MockUser:+ def __init__(self, user_role):+ self.user_role = user_role++ async def mock_admin_auth():+ return MockUser(LitellmUserRoles.PROXY_ADMIN)++ monkeypatch.setattr(+ "litellm.proxy.ui_crud_endpoints.proxy_setting_endpoints.user_api_key_auth",+ mock_admin_auth,+ )+ monkeypatch.setattr("litellm.proxy.proxy_server.store_model_in_db", True)++ payload = {+ "disable_model_add_for_internal_users": False,+ "unsupported_flag": True,+ }++ response = client.patch("/update/ui_settings", json=payload)++ assert response.status_code == 200+ data = response.json()+ assert data["status"] == "success"+ assert "unsupported_flag" not in data["settings"]+ assert data["settings"]["disable_model_add_for_internal_users"] is False++ ui_settings = mock_proxy_config["config"]["litellm_settings"]["ui_settings"]+ assert "unsupported_flag" not in ui_settings+ assert ui_settings["disable_model_add_for_internal_users"] is False+ assert mock_proxy_config["save_call_count"]() == 1+def test_get_sso_settings_from_database(self, mock_proxy_config, mock_auth, monkeypatch):"""Test getting SSO settings from the dedicated database table"""import json