Skip to content

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]

[LLM] fix model reload on model update (#13216)

Jugal D. Bhatta4c11600a95e
diff --git a/docs/my-website/docs/proxy/config_settings.md b/docs/my-website/docs/proxy/config_settings.md
index 24771ff63c0..853322bd19b 100644
--- a/docs/my-website/docs/proxy/config_settings.md
+++ b/docs/my-website/docs/proxy/config_settings.md
@@ -38,8 +38,7 @@ litellm_settings:
context_window_fallbacks: [{"gpt-3.5-turbo-small": ["gpt-3.5-turbo-large", "claude-opus"]}] # fallbacks for ContextWindowExceededErrors
# MCP Aliases - Map aliases to MCP server names for easier tool access
- mcp_aliases: { "github": "github_mcp_server", "zapier": "zapier_mcp_server", "deepwiki": "deepwiki_mcp_server" } # Maps friendly aliases to MCP server names. Only the first alias for each server is used.
-
+ mcp_aliases: { "github": "github_mcp_server", "zapier": "zapier_mcp_server", "deepwiki": "deepwiki_mcp_server" } # Maps friendly aliases to MCP server names. Only the first alias for each server is used
# Caching settings
cache: true
diff --git a/litellm/proxy/management_endpoints/model_management_endpoints.py b/litellm/proxy/management_endpoints/model_management_endpoints.py
index 411ca10aa96..b8762899f1e 100644
--- a/litellm/proxy/management_endpoints/model_management_endpoints.py
+++ b/litellm/proxy/management_endpoints/model_management_endpoints.py
@@ -233,7 +233,7 @@ async def patch_model(
data=update_data,
)
- # Clear cache and reload models
+ # Clear cache and reload models (uses config setting or defaults to preserving config models for DB updates)
await clear_cache()
## CREATE AUDIT LOG ##
@@ -1144,13 +1144,38 @@ async def clear_cache():
)
return
+
try:
- llm_router.model_list.clear()
+ # Only clear DB models, preserve config models
+ verbose_proxy_logger.debug("Clearing only DB models, preserving config models")
+
+ # Get current models and filter out DB models
+ current_models = llm_router.model_list.copy()
+ config_models = []
+ db_model_ids = []
+
+ for model in current_models:
+ model_info = model.get("model_info", {})
+ if model_info.get("db_model", False):
+ # This is a DB model, mark for deletion
+ db_model_ids.append(model_info.get("id"))
+ else:
+ # This is a config model, preserve it
+ config_models.append(model)
+
+ # Clear only DB models
+ for model_id in db_model_ids:
+ llm_router.delete_deployment(id=model_id)
+
+ # Clear auto routers
llm_router.auto_routers.clear()
-
+
+ # Reload only DB models
await proxy_config.add_deployment(
prisma_client=prisma_client, proxy_logging_obj=proxy_logging_obj
)
+
+ verbose_proxy_logger.debug(f"Cleared {len(db_model_ids)} DB models, preserved {len(config_models)} config models")
except Exception as e:
verbose_proxy_logger.exception(
f"Failed to clear cache and reload models. Due to error - {str(e)}"
diff --git a/tests/test_litellm/proxy/management_endpoints/test_model_management_endpoints.py b/tests/test_litellm/proxy/management_endpoints/test_model_management_endpoints.py
index e9bb69abd0e..c5fdbf4925a 100644
--- a/tests/test_litellm/proxy/management_endpoints/test_model_management_endpoints.py
+++ b/tests/test_litellm/proxy/management_endpoints/test_model_management_endpoints.py
@@ -390,8 +390,64 @@ class TestClearCache:
):
await clear_cache()
- assert len(mock_router.model_list) == 0
+ assert len(mock_router.model_list) == 2
+ assert len(mock_router.auto_routers) == 0
+
+ @pytest.mark.asyncio
+ async def test_clear_cache_preserve_config_models(self):
+ """
+ Test that clear_cache clears DB models and preserves config models.
+ """
+ from litellm.proxy.management_endpoints.model_management_endpoints import clear_cache
+
+ # Create mock router with mixed DB and config models
+ mock_router = MagicMock()
+ mock_router.model_list = [
+ {
+ "model_name": "gpt-4",
+ "model_info": {"id": "db-model-1", "db_model": True},
+ "litellm_params": {"model": "gpt-4"}
+ },
+ {
+ "model_name": "gpt-3.5-turbo",
+ "model_info": {"id": "config-model-1", "db_model": False},
+ "litellm_params": {"model": "gpt-3.5-turbo"}
+ },
+ {
+ "model_name": "claude-3",
+ "model_info": {"id": "db-model-2", "db_model": True},
+ "litellm_params": {"model": "claude-3"}
+ }
+ ]
+ mock_router.delete_deployment = MagicMock(return_value=True)
+ mock_router.auto_routers = MagicMock()
+ mock_router.auto_routers.clear = MagicMock()
+
+ mock_config = MagicMock()
+ mock_config.add_deployment = AsyncMock(return_value=True)
+
+ mock_prisma = MagicMock()
+ mock_logging = MagicMock()
+
+ with patch("litellm.proxy.proxy_server.llm_router", mock_router), patch(
+ "litellm.proxy.proxy_server.proxy_config", mock_config
+ ), patch("litellm.proxy.proxy_server.prisma_client", mock_prisma), patch(
+ "litellm.proxy.proxy_server.proxy_logging_obj", mock_logging
+ ), patch(
+ "litellm.proxy.proxy_server.verbose_proxy_logger"
+ ):
+ await clear_cache()
+
+ # Should have called delete_deployment for both DB models
+ assert mock_router.delete_deployment.call_count == 2
+ mock_router.delete_deployment.assert_any_call(id="db-model-1")
+ mock_router.delete_deployment.assert_any_call(id="db-model-2")
+
+ # Should have cleared auto routers
+ mock_router.auto_routers.clear.assert_called_once()
+
+ # Should have called add_deployment to reload DB models
mock_config.add_deployment.assert_called_once_with(
prisma_client=mock_prisma, proxy_logging_obj=mock_logging
)