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(proxy_server.py): fixes for handling team only models via `/v2/mo… (#12632)
Krish Dholakia955b504f7b76
diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex a427253a22e..a60e31d6868 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -734,11 +734,12 @@ def custom_openapi():if route in openapi_schema["paths"]:paths_to_include[route] = openapi_schema["paths"][route]openapi_schema["paths"] = paths_to_include-+# Add LLM API request schema bodies for documentationfrom litellm.proxy.common_utils.custom_openapi_spec import CustomOpenAPISpec+openapi_schema = CustomOpenAPISpec.add_llm_api_request_schema_body(openapi_schema)-+app.openapi_schema = openapi_schemareturn app.openapi_schema@@ -5779,7 +5780,9 @@ async def get_all_team_and_direct_access_models(direct_access_models: List[str] = []if user_api_key_dict.user_role == LitellmUserRoles.PROXY_ADMIN:user_teams = "*"- direct_access_models = llm_router.get_model_ids() # has access to all models+ direct_access_models = llm_router.get_model_ids(+ exclude_team_models=True+ ) # has access to all modelselif user_api_key_dict.user_id is not None:user_db_object = await prisma_client.db.litellm_usertable.find_unique(where={"user_id": user_api_key_dict.user_id}@@ -5798,14 +5801,15 @@ async def get_all_team_and_direct_access_models(prisma_client=prisma_client,llm_router=llm_router,)-for _model in all_models:model_id = _model.get("model_info", {}).get("id", None)team_only_model_id = _model.get("model_info", {}).get("team_id", None)if model_id is not None:can_use_model = Falseif team_only_model_id is not None:- pass+ team_ids = team_models.get(model_id, [])+ if team_ids and team_only_model_id in team_ids:+ can_use_model = Trueelse:can_use_model = Trueif can_use_model:@@ -8654,16 +8658,19 @@ async def get_routes():Get a list of available routes in the FastAPI application."""from litellm.proxy.common_utils.get_routes import GetRoutes+routes = []for route in app.routes:endpoint_route = getattr(route, "endpoint", None)if endpoint_route is not None:- routes.extend(GetRoutes.get_app_routes(- route=route,- endpoint_route=endpoint_route,- ))+ routes.extend(+ GetRoutes.get_app_routes(+ route=route,+ endpoint_route=endpoint_route,+ )+ )# Handle mounted sub-applications (like MCP app)- elif hasattr(route, 'app') and hasattr(route, 'path'):+ elif hasattr(route, "app") and hasattr(route, "path"):routes.extend(GetRoutes.get_routes_for_mounted_app(route=route))return {"routes": routes}diff --git a/litellm/router.py b/litellm/router.pyindex 4eddbf6da1f..6ae9867ddb8 100644--- a/litellm/router.py+++ b/litellm/router.py@@ -5567,7 +5567,9 @@ class Router:additional_headers[header] = valuereturn response- def get_model_ids(self, model_name: Optional[str] = None) -> List[str]:+ def get_model_ids(+ self, model_name: Optional[str] = None, exclude_team_models: bool = False+ ) -> List[str]:"""if 'model_name' is none, returns all.@@ -5577,6 +5579,8 @@ class Router:for model in self.model_list:if "model_info" in model and "id" in model["model_info"]:id = model["model_info"]["id"]+ if exclude_team_models and model["model_info"].get("team_id"):+ continueif model_name is not None and model["model_name"] == model_name:ids.append(id)elif model_name is None:diff --git a/tests/llm_translation/test_anthropic_completion.py b/tests/llm_translation/test_anthropic_completion.pyindex bd49757088b..599aff3bbe5 100644--- a/tests/llm_translation/test_anthropic_completion.py+++ b/tests/llm_translation/test_anthropic_completion.py@@ -1278,7 +1278,7 @@ def test_anthropic_mcp_server_tool_use(spec: str):"type": "url","url": "https://mcp.zapier.com/api/mcp/mcp","name": "zapier-mcp",- "authorization_token": os.getenv('ZAPIER_CI_CD_MCP_TOKEN')+ "authorization_token": os.getenv("ZAPIER_CI_CD_MCP_TOKEN"),}]elif spec == "openai":@@ -1302,10 +1302,9 @@ def test_anthropic_mcp_server_tool_use(spec: str):try:response = litellm.completion(**params)+ assert response is not Noneexcept litellm.InternalServerError as e:- print(e)-- assert response is not None+ pytest.skip(f"Skipping test due to internal server error: {e}")@pytest.mark.parametrize(@@ -1313,6 +1312,7 @@ def test_anthropic_mcp_server_tool_use(spec: str):)def test_anthropic_mcp_server_responses_api(model: str):from litellm import responses+litellm._turn_on_debug()tools = [{@@ -1322,7 +1322,7 @@ def test_anthropic_mcp_server_responses_api(model: str):"require_approval": "never","headers": {"Authorization": f"Bearer {os.getenv('ZAPIER_CI_CD_MCP_TOKEN')}"- }+ },},]