From e6385a6c836f045e295854af21e868d467a587d1 Mon Sep 17 00:00:00 2001 From: Julio Quinteros Pro Date: Thu, 12 Feb 2026 20:19:06 -0300 Subject: [PATCH] fix: make policy_resolve_endpoints importable without FastAPI Adds try/except around FastAPI imports with fallback mock classes. This allows the module to be imported in test environments where proxy dependencies (FastAPI) may not be installed. Fixes NameError when MCP tests try to import from proxy_server which imports from this module: - NameError: name 'APIRouter' is not defined - NameError: name 'Depends' is not defined - NameError: name 'HTTPException' is not defined - NameError: name 'Query' is not defined --- .../policy_engine/policy_resolve_endpoints.py | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/litellm/proxy/policy_engine/policy_resolve_endpoints.py b/litellm/proxy/policy_engine/policy_resolve_endpoints.py index 318e990ff12..428ab2b63b2 100644 --- a/litellm/proxy/policy_engine/policy_resolve_endpoints.py +++ b/litellm/proxy/policy_engine/policy_resolve_endpoints.py @@ -6,8 +6,26 @@ Policy resolve and attachment impact estimation endpoints. """ import json +from typing import TYPE_CHECKING -from fastapi import APIRouter, Depends, HTTPException, Query +# FastAPI imports - may not be available in all environments (e.g. during testing) +try: + from fastapi import APIRouter, Depends, HTTPException, Query +except ImportError: + # Provide stubs for type checking only + if TYPE_CHECKING: + from fastapi import APIRouter, Depends, HTTPException, Query + else: + # Create mock classes that won't be used + class APIRouter: # type: ignore[no-redef] + def post(self, *args, **kwargs): + def decorator(func): + return func + return decorator + + def Depends(func): return func # type: ignore[misc] + HTTPException = Exception # type: ignore[misc,assignment] + def Query(*args, **kwargs): return None # type: ignore[misc] from litellm._logging import verbose_proxy_logger from litellm.constants import MAX_POLICY_ESTIMATE_IMPACT_ROWS