This commit is contained in:
Mateo Wang 2026-10-29 03:12:36 +00:00 • committed by GitHub
commit a4841d8b8f
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
41 changed files with 292 additions and 131 deletions

View file

@ -30,6 +30,7 @@ from litellm.proxy.auth.auth_utils import ( # noqa: F401 # legacy module expor
)
from litellm.proxy.auth.model_access_denied import ModelAccessDeniedHTTPException
from litellm.proxy.auth.resolvers.exceptions import KeyNotFoundError
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.db.exception_handler import PrismaDBExceptionHandler
from litellm.types.services import ServiceTypes
@ -61,14 +62,14 @@ def _as_proxy_exception(e: Exception) -> ProxyException:
message=str(e.detail),
internal_message=e.internal_message,
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=e.status_code,
)
if isinstance(e, HTTPException):
return ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_401_UNAUTHORIZED),
)
if isinstance(e, ProxyException):
@ -78,7 +79,7 @@ def _as_proxy_exception(e: Exception) -> ProxyException:
return ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_401_UNAUTHORIZED,
)

View file

@ -225,7 +225,7 @@ def jwks_unavailable_exception(error: JWKSUnreachableError) -> ProxyException:
f"unreachable, so the JWT signature could not be verified. Please retry shortly. Error: {error}"
),
type=ProxyErrorTypes.auth_provider_unavailable,
param="None",
param=None,
code=status.HTTP_503_SERVICE_UNAVAILABLE,
)

View file

@ -301,7 +301,7 @@ class RouteChecks:
f"keys/users/teams. Route={route}. Your role={user_role}. Your user_id={masked_user_id}"
),
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)

View file

@ -2523,7 +2523,7 @@ async def user_api_key_auth_builder(
raise ProxyException(
message="Authentication Error, No api key passed in.",
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
elif api_key == "":
@ -2531,7 +2531,7 @@ async def user_api_key_auth_builder(
raise ProxyException(
message="Authentication Error, Malformed API Key passed in. Ensure Key has `Bearer ` prefix.",
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
@ -2772,7 +2772,7 @@ async def user_api_key_auth_builder(
raise ProxyException(
message=f"Authentication Error, Tried to access route={route}, which is only for MASTER KEY",
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
@ -2905,7 +2905,7 @@ async def validate_resolved_virtual_key( # noqa: C901 # Preserve ordering of e
raise ProxyException(
message="Authentication Error, Key is blocked. Update via `/key/unblock` if you're an admin.",
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
await enforce_key_and_fallback_model_access(
@ -2956,7 +2956,7 @@ async def validate_resolved_virtual_key( # noqa: C901 # Preserve ordering of e
"Keys owned by this user cannot be used."
),
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
@ -3279,7 +3279,7 @@ async def validate_resolved_virtual_key( # noqa: C901 # Preserve ordering of e
raise ProxyException(
message="Authentication Error, Invalid proxy server token passed",
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
if valid_token_dict is not None:

View file

@ -17,6 +17,7 @@ _OPENAI_ERROR_TYPE_BY_STATUS: Final[Mapping[int, str]] = MappingProxyType(
{
status.HTTP_401_UNAUTHORIZED: "authentication_error",
status.HTTP_403_FORBIDDEN: "permission_error",
status.HTTP_408_REQUEST_TIMEOUT: "timeout_error",
status.HTTP_429_TOO_MANY_REQUESTS: "rate_limit_error",
}
)

View file

@ -435,7 +435,7 @@ class PrismaDBExceptionHandler:
return ProxyException(
message=PrismaDBExceptionHandler.database_unavailable_message(e),
type=ProxyErrorTypes.no_db_connection,
param="None",
param=None,
code=HTTPStatus.SERVICE_UNAVAILABLE.value,
)

View file

@ -47,6 +47,7 @@ from litellm.proxy.auth.auth_utils import (
from litellm.proxy.auth.model_checks import get_key_models
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
from litellm.proxy.common_utils import http_parsing_utils
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.db.db_lookup_gate import db_lookup_stall_tracker
from litellm.proxy.db.exception_handler import PrismaDBExceptionHandler
from litellm.proxy.db.health_check_latest import (
@ -687,7 +688,7 @@ async def health_services_endpoint(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -695,7 +696,7 @@ async def health_services_endpoint(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

View file

@ -46,6 +46,7 @@ from litellm.proxy.auth.password_policy import (
)
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
from litellm.proxy.common_utils.auth_cache_invalidation_pubsub import evict_and_broadcast
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.common_utils.user_api_key_cache import (
AUTH_OBJECTS_TARGET,
object_permission_cache_key,
@ -1733,7 +1734,7 @@ async def user_update(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -1741,7 +1742,7 @@ async def user_update(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)

View file

@ -75,6 +75,7 @@ from litellm.proxy.common_utils.config_sync_pubsub import (
coordination_redis_cache,
publish_config_change,
)
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.common_utils.rbac_utils import check_org_admin_can_generate_keys
from litellm.proxy.common_utils.timezone_utils import get_budget_reset_time
from litellm.proxy.common_utils.user_api_key_cache import AUTH_OBJECTS_TARGET, UserApiKeyCache
@ -4091,7 +4092,7 @@ async def update_key_fn(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -4099,7 +4100,7 @@ async def update_key_fn(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)
@ -4970,7 +4971,7 @@ async def info_key_fn(
raise ProxyException(
message="Internal server error from /key/info. Details are in the proxy server logs.",
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -7248,7 +7249,7 @@ async def list_keys(
raise ProxyException(
message=getattr(e, "detail", f"error({e})"),
type=ProxyErrorTypes.internal_server_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -7256,7 +7257,7 @@ async def list_keys(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.internal_server_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=fastapi.status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -7406,7 +7407,7 @@ async def key_aliases(
raise ProxyException(
message=getattr(e, "detail", f"error({e})"),
type=ProxyErrorTypes.internal_server_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -7414,7 +7415,7 @@ async def key_aliases(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.internal_server_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -8191,7 +8192,7 @@ async def key_health(
raise ProxyException(
message=f"Key health check failed: {e}",
type=ProxyErrorTypes.internal_server_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

View file

@ -77,6 +77,7 @@ from litellm.proxy.common_utils.encrypt_decrypt_utils import (
decrypt_value_helper,
encrypt_value_helper,
)
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
from litellm.proxy.db.routing_prisma_wrapper import WriterPinnedClient
from litellm.proxy.management.teams.authz import TEAM_ADMIN_ONLY, is_team_admin
@ -2849,7 +2850,7 @@ async def delete_model(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -2857,7 +2858,7 @@ async def delete_model(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)
@ -3125,7 +3126,7 @@ async def add_new_model(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -3133,7 +3134,7 @@ async def add_new_model(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)
@ -3368,7 +3369,7 @@ async def update_model(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -3376,7 +3377,7 @@ async def update_model(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)

View file

@ -41,6 +41,7 @@ from litellm.proxy.auth.auth_checks import (
)
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
from litellm.proxy.common_utils.auth_cache_invalidation_pubsub import evict_and_broadcast
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.common_utils.timezone_utils import get_budget_reset_time
from litellm.proxy.common_utils.user_api_key_cache import UserApiKeyCache
from litellm.proxy.management_endpoints.budget_management_endpoints import (
@ -1400,7 +1401,7 @@ async def organization_member_add(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -1408,7 +1409,7 @@ async def organization_member_add(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

View file

@ -46,6 +46,7 @@ from litellm.proxy.common_utils.callback_utils import ( # noqa: F401 # legacy
encrypt_callback_vars,
is_sensitive_callback_key,
)
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.litellm_pre_call_utils import ( # noqa: F401 # legacy module exports
_get_validated_callback_metadata, # pyright: ignore[reportPrivateUsage,reportUnusedImport] # backwards-compatible package export
convert_key_logging_metadata_to_callback,
@ -463,7 +464,7 @@ async def add_team_callbacks(
raise ProxyException(
message="Internal Server Error, " + str(e),
type=ProxyErrorTypes.internal_server_error.value,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -606,7 +607,7 @@ async def delete_team_callback(
raise ProxyException(
message="Internal Server Error, " + str(e),
type=ProxyErrorTypes.internal_server_error.value,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
else:
@ -747,7 +748,7 @@ async def disable_team_logging(
raise ProxyException(
message="Internal Server Error, " + str(e),
type=ProxyErrorTypes.internal_server_error.value,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -838,7 +839,7 @@ async def get_team_callbacks(
raise ProxyException(
message=getattr(e, "detail", f"Internal Server Error({e})"),
type=ProxyErrorTypes.internal_server_error.value,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -846,6 +847,6 @@ async def get_team_callbacks(
raise ProxyException(
message="Internal Server Error, " + str(e),
type=ProxyErrorTypes.internal_server_error.value,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

View file

@ -132,6 +132,7 @@ from litellm.proxy.common_utils.auth_cache_invalidation_pubsub import (
)
from litellm.proxy.common_utils.callback_utils import encrypt_callback_vars
from litellm.proxy.common_utils.json_merge_patch import apply_json_merge_patch
from litellm.proxy.common_utils.openai_error_payload import openai_error_param
from litellm.proxy.common_utils.user_api_key_cache import (
UserApiKeyCache,
team_membership_auth_cache_key,
@ -5530,7 +5531,7 @@ async def team_info(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -5538,7 +5539,7 @@ async def team_info(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)

View file

@ -522,8 +522,11 @@ from litellm.proxy.common_utils.openai_endpoint_utils import (
)
from litellm.proxy.common_utils.openai_error_payload import (
LITELLM_CALL_ID_HEADER,
error_status_code,
headers_with_litellm_call_id,
litellm_call_id_headers,
openai_error_param,
openai_error_type,
with_litellm_call_id,
)
from litellm.proxy.common_utils.periodic_reload_schedule import (
@ -9838,9 +9841,9 @@ async def async_assistants_data_generator(response, user_api_key_dict: UserAPIKe
proxy_exception: Final = ProxyException(
message=getattr(e, "message", error_msg),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
code=getattr(e, "status_code", 500),
type=openai_error_type(e, error_status_code(e, 500)),
param=openai_error_param(e),
code=error_status_code(e, 500),
)
error_returned: Final = json.dumps({"error": proxy_exception.to_dict()})
yield f"data: {error_returned}\n\n"
@ -10675,9 +10678,9 @@ async def async_data_generator(
if isinstance(client_error, HTTPException)
else ProxyException(
message=getattr(client_error, "message", str(client_error)),
type=getattr(client_error, "type", "None"),
param=getattr(client_error, "param", "None"),
code=getattr(client_error, "status_code", 500),
type=openai_error_type(client_error, error_status_code(client_error, 500)),
param=openai_error_param(client_error),
code=error_status_code(client_error, 500),
).to_dict()
},
default=str,
@ -13030,11 +13033,11 @@ async def completion(
error_msg: Final = f"{e}"
raise ProxyException(
message=getattr(e, "message", error_msg),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
type=openai_error_type(e, error_status_code(e, 500)),
param=openai_error_param(e),
headers=litellm_call_id_headers(litellm_call_id),
openai_code=getattr(e, "code", None),
code=getattr(e, "status_code", 500),
code=error_status_code(e, 500),
)
@ -13257,19 +13260,19 @@ async def moderations(
if isinstance(e, HTTPException):
raise ProxyException(
message=getattr(e, "message", str(e)),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
type=openai_error_type(e, error_status_code(e, status.HTTP_400_BAD_REQUEST)),
param=openai_error_param(e),
headers=litellm_call_id_headers(litellm_call_id),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
code=error_status_code(e, status.HTTP_400_BAD_REQUEST),
)
else:
error_msg: Final = f"{e}"
raise ProxyException(
message=getattr(e, "message", error_msg),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
type=openai_error_type(e, error_status_code(e, 500)),
param=openai_error_param(e),
headers=litellm_call_id_headers(litellm_call_id),
code=getattr(e, "status_code", 500),
code=error_status_code(e, 500),
)
@ -13410,11 +13413,11 @@ async def audio_speech(
)
raise ProxyException(
message=getattr(e, "message", f"{e}"),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
type=openai_error_type(e, error_status_code(e, 500)),
param=openai_error_param(e),
headers=litellm_call_id_headers(litellm_call_id),
openai_code=getattr(e, "code", None),
code=getattr(e, "status_code", 500),
code=error_status_code(e, 500),
)
@ -13556,20 +13559,20 @@ async def audio_transcriptions(
if isinstance(e, HTTPException):
raise ProxyException(
message=getattr(e, "message", str(e.detail)),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
type=openai_error_type(e, error_status_code(e, status.HTTP_400_BAD_REQUEST)),
param=openai_error_param(e),
headers=litellm_call_id_headers(litellm_call_id),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
code=error_status_code(e, status.HTTP_400_BAD_REQUEST),
)
else:
error_msg: Final = f"{e}"
raise ProxyException(
message=getattr(e, "message", error_msg),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
type=openai_error_type(e, error_status_code(e, 500)),
param=openai_error_param(e),
headers=litellm_call_id_headers(litellm_call_id),
openai_code=getattr(e, "code", None),
code=getattr(e, "status_code", 500),
code=error_status_code(e, 500),
)
@ -17154,7 +17157,7 @@ async def model_streaming_metrics(
raise ProxyException(
message=CommonProxyErrors.db_not_connected_error.value,
type="internal_error",
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -17288,7 +17291,7 @@ async def model_metrics(
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
startTime = startTime or datetime.now() - timedelta(days=DAYS_IN_A_MONTH)
@ -17403,7 +17406,7 @@ async def model_metrics_slow_responses(
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
if api_key is None or api_key == "undefined":
@ -17492,7 +17495,7 @@ async def model_metrics_exceptions(
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -18526,7 +18529,7 @@ async def async_queue_request(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -18538,7 +18541,7 @@ async def async_queue_request(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)
@ -18713,7 +18716,7 @@ async def login_v2(request: Request):
raise ProxyException(
message=getattr(e, "detail", str(e)),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
else:
@ -18721,7 +18724,7 @@ async def login_v2(request: Request):
raise ProxyException(
message=error_msg,
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -18789,7 +18792,7 @@ async def login_v3(request: Request):
raise ProxyException(
message=getattr(e, "detail", str(e)),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
else:
@ -18797,7 +18800,7 @@ async def login_v3(request: Request):
raise ProxyException(
message=error_msg,
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -18862,7 +18865,7 @@ async def login_v3_exchange(request: Request):
raise ProxyException(
message=str(e),
type=ProxyErrorTypes.auth_error,
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -19823,7 +19826,7 @@ async def update_config(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -19831,7 +19834,7 @@ async def update_config(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)
@ -21020,7 +21023,7 @@ async def get_config(
raise ProxyException(
message=getattr(e, "detail", f"Authentication Error({e})"),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
)
elif isinstance(e, ProxyException):
@ -21028,7 +21031,7 @@ async def get_config(
raise ProxyException(
message="Authentication Error, " + str(e),
type=ProxyErrorTypes.auth_error,
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_400_BAD_REQUEST,
)

View file

@ -50,6 +50,7 @@ from litellm.proxy.auth.authorization_dependencies import (
LogTeamLookupDependency,
)
from litellm.proxy.auth.user_api_key_auth import user_api_key_auth
from litellm.proxy.common_utils.openai_error_payload import error_status_code, openai_error_param, openai_error_type
from litellm.proxy.litellm_pre_call_utils import LiteLLMProxyRequestSetup
from litellm.proxy.spend_tracking.spend_capture_rate import (
ProviderBillingCredentialMissing,
@ -636,7 +637,7 @@ async def view_spend_tags(
raise ProxyException(
message=getattr(e, "detail", f"/spend/tags Error({e})"),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -644,7 +645,7 @@ async def view_spend_tags(
raise ProxyException(
message="/spend/tags Error" + str(e),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -2142,7 +2143,7 @@ async def global_get_all_tag_names():
raise ProxyException(
message=getattr(e, "detail", f"/spend/all_tag_names Error({e})"),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -2150,7 +2151,7 @@ async def global_get_all_tag_names():
raise ProxyException(
message="/spend/all_tag_names Error" + str(e),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -2224,7 +2225,7 @@ async def global_view_spend_tags(
raise ProxyException(
message=getattr(e, "detail", f"/spend/tags Error({error_str})"),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -2232,7 +2233,7 @@ async def global_view_spend_tags(
raise ProxyException(
message="/spend/tags Error" + error_str,
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -2445,9 +2446,9 @@ async def calculate_spend(request: SpendCalculateRequest):
if isinstance(e, HTTPException):
raise ProxyException(
message=getattr(e, "detail", str(e)),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
code=getattr(e, "status_code", status.HTTP_400_BAD_REQUEST),
type=openai_error_type(e, error_status_code(e, status.HTTP_400_BAD_REQUEST)),
param=openai_error_param(e),
code=error_status_code(e, status.HTTP_400_BAD_REQUEST),
)
if isinstance(e, litellm.exceptions.ModelNotMappedError):
raise ProxyException(
@ -2459,9 +2460,9 @@ async def calculate_spend(request: SpendCalculateRequest):
error_msg: Final = f"{e}"
raise ProxyException(
message=getattr(e, "message", error_msg),
type=getattr(e, "type", "None"),
param=getattr(e, "param", "None"),
code=getattr(e, "status_code", 500),
type=openai_error_type(e, error_status_code(e, 500)),
param=openai_error_param(e),
code=error_status_code(e, 500),
)
@ -2631,7 +2632,7 @@ async def ui_view_spend_logs(
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
@ -2698,7 +2699,7 @@ async def ui_view_spend_logs(
raise ProxyException(
message="Start date and end date are required",
type="bad_request",
param="None",
param=None,
code=status.HTTP_400_BAD_REQUEST,
)
formats: Final = ["%Y-%m-%d %H:%M:%S", "%Y-%m-%d"] if is_v2 else ["%Y-%m-%d %H:%M:%S"]
@ -3679,7 +3680,7 @@ async def view_spend_logs(
raise ProxyException(
message=getattr(e, "detail", f"/spend/logs Error({e})"),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -3687,7 +3688,7 @@ async def view_spend_logs(
raise ProxyException(
message="/spend/logs Error" + str(e),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -3714,7 +3715,7 @@ async def global_spend_reset():
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
@ -3745,7 +3746,7 @@ async def global_spend_refresh():
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_401_UNAUTHORIZED,
)
@ -3816,7 +3817,7 @@ async def global_spend_for_internal_user(
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
try:
@ -3877,7 +3878,7 @@ async def global_spend_logs(
raise ProxyException(
message="Prisma Client is not initialized",
type="internal_error",
param="None",
param=None,
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -3921,7 +3922,7 @@ async def global_spend_logs(
raise ProxyException(
message=getattr(e, "detail", f"/global/spend/logs Error({error_str})"),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -3929,7 +3930,7 @@ async def global_spend_logs(
raise ProxyException(
message="/global/spend/logs Error" + error_str,
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)
@ -3969,7 +3970,7 @@ async def global_spend():
raise ProxyException(
message=getattr(e, "detail", f"/global/spend Error({error_str})"),
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=getattr(e, "status_code", status.HTTP_500_INTERNAL_SERVER_ERROR),
)
elif isinstance(e, ProxyException):
@ -3977,7 +3978,7 @@ async def global_spend():
raise ProxyException(
message="/global/spend Error" + error_str,
type="internal_error",
param=getattr(e, "param", "None"),
param=openai_error_param(e),
code=status.HTTP_500_INTERNAL_SERVER_ERROR,
)

View file

@ -310,7 +310,7 @@ def test_non_admin_keys_scoped_to_the_submission_routes_get_403_from_the_admin_c
f"Your user_id={_mask_user_id(user_id)}"
),
type="auth_error",
param="None",
param=None,
code="401",
)
)

View file

@ -114,7 +114,7 @@ class _ChatRequest(BaseModel):
class _ProxyError(BaseModel):
message: str
type: str
param: str
param: str | None
code: str
@ -398,7 +398,7 @@ def _assert_unmapped(response: httpx.Response, client_id: str) -> None:
assert response.status_code == 403, response.text
error: Final = _ErrorResponse.model_validate_json(response.content)
expected: Final = f"JWT Key Mapping: No registered mapping for {_CLIENT_CLAIM}='{client_id}'. Access denied."
assert error.error == _ProxyError(message=expected, type="auth_error", param="None", code="403"), response.text
assert error.error == _ProxyError(message=expected, type="auth_error", param=None, code="403"), response.text
def _mapping_info(gateway: Gateway, identity: str) -> _JWTMappingResponse:

View file

@ -24,7 +24,7 @@ def _admin_only_error(route: str, user_id: str) -> dict[str, JsonValue]:
f"keys/users/teams. Route={route}. Your role=internal_user. Your user_id={masked_user_id}"
),
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}

View file

@ -600,7 +600,7 @@ def _admin_only_route(route: str) -> tuple[int, JsonValue]:
"message": "Authentication Error, Only proxy admin can be used to generate, delete, update info for new "
f"keys/users/teams. Route={route}. Your role=internal_user. Your user_id=<caller>",
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}
@ -746,7 +746,7 @@ def test_internal_user_model_creation_prohibition_refuses_team_admin_but_not_pro
"error": {
"message": "Model creation is disabled for internal users by disable_model_add_for_internal_users.",
"type": "auth_error",
"param": "None",
"param": None,
"code": "403",
}
},

View file

@ -40,7 +40,7 @@ class _ProxyError(BaseModel):
message: str
type: str
param: str
param: str | None
code: str
@ -260,12 +260,12 @@ def _control_error(response: httpx.Response) -> _ProxyError:
def test_requests_without_an_sk_key_keep_their_own_401(gateway: Gateway, peer: Gateway, side: Side) -> None:
client: Final = _side(gateway, peer, side).client
assert _control_error(client.get("/v1/models")) == _ProxyError(
message="Authentication Error, No api key passed in.", type="auth_error", param="None", code="401"
message="Authentication Error, No api key passed in.", type="auth_error", param=None, code="401"
)
assert _control_error(client.get("/v1/models", headers={"Authorization": "Bearer not-a-real-key"})) == _ProxyError(
message="LiteLLM Virtual Key expected. Received=not-****-key, expected to start with 'sk-'.",
type="auth_error",
param="None",
param=None,
code="401",
)
@ -277,7 +277,7 @@ def test_a_blocked_key_keeps_its_own_401_on_both_proxies(gateway: Gateway, peer:
expected: Final = _ProxyError(
message="Authentication Error, Key is blocked. Update via `/key/unblock` if you're an admin.",
type="auth_error",
param="None",
param=None,
code="401",
)
for candidate in (gateway, peer):

View file

@ -38,7 +38,7 @@ def _escalation_error(field: str) -> dict[str, JsonValue]:
"error": {
"message": f"{{'error': \"Non-admin users cannot modify '{field}' on their own record. Contact your proxy admin.\"}}",
"type": "auth_error",
"param": "None",
"param": None,
"code": "403",
}
}
@ -49,7 +49,7 @@ def _role_change_error() -> dict[str, JsonValue]:
"error": {
"message": "Only proxy admins can modify user roles.",
"type": "auth_error",
"param": "None",
"param": None,
"code": "403",
}
}
@ -65,7 +65,7 @@ def _ui_session_route_error(user_id: str) -> dict[str, JsonValue]:
f"Your user_id={masked_user_id}"
),
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}

View file

@ -679,7 +679,7 @@ def _expected_scim_write_denial(route: str, user_id: str, user_role: str) -> dic
f"keys/users/teams. Route={route}. Your role={user_role}. Your user_id={masked_user_id}"
),
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}

View file

@ -57,7 +57,7 @@ def _route_refusal(response: httpx.Response, user_id: str) -> None:
f"keys/users/teams. Route=/v1/workflows/runs. Your role=internal_user. Your user_id={masked_user_id}"
),
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}, response.text

View file

@ -151,7 +151,7 @@ def test_public_readiness_reports_stalled_database_lookups(gateway: Gateway, tmp
"Please retry shortly."
),
"type": "no_db_connection",
"param": "None",
"param": None,
"code": "503",
}
}

View file

@ -133,7 +133,7 @@ def _expected_scim_write_denial(route: str, user_id: str, user_role: str) -> dic
f"keys/users/teams. Route={route}. Your role={user_role}. Your user_id={masked_user_id}"
),
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}

View file

@ -256,7 +256,7 @@ def _team_not_found_body(team_id: str) -> dict[str, JsonValue]:
"error": {
"message": f"{{'message': 'Team not found, passed team id: {team_id}.'}}",
"type": "auth_error",
"param": "None",
"param": None,
"code": "404",
}
}

View file

@ -64,7 +64,7 @@ def _blocked_key_error() -> dict[str, JsonValue]:
"error": {
"message": "Authentication Error, Key is blocked. Update via `/key/unblock` if you're an admin.",
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}

View file

@ -177,7 +177,7 @@ def test_arize_otel_v2_d4_unauthenticated_request_has_no_span(gateway: Gateway,
"error": {
"message": "Authentication Error, No api key passed in.",
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}, body

View file

@ -325,7 +325,7 @@ def test_long_apply_guardrail_alias_accepts_an_llm_api_scoped_key(gateway: Gatew
"error": {
"message": "Authentication Error, No api key passed in.",
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}, unauthenticated.text

View file

@ -100,7 +100,7 @@ def _assert_non_admin_refusal(response: httpx.Response, route: str, user_id: str
f"new keys/users/teams. Route={route}. Your role=internal_user. Your user_id={masked_user_id}"
),
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}, response.text
@ -244,7 +244,7 @@ def test_saved_vault_config_reads_back_masked_and_refuses_non_admins(
"message": "user not allowed to access this route, role= proxy_admin_viewer. "
"Trying to access: /config_overrides/hashicorp_vault",
"type": "auth_error",
"param": "None",
"param": None,
"code": "403",
}
}, viewer_post.text
@ -386,7 +386,7 @@ def test_saved_cyberark_config_reads_back_masked_and_refuses_non_admins(
"message": "user not allowed to access this route, role= proxy_admin_viewer. "
"Trying to access: /config_overrides/cyberark",
"type": "auth_error",
"param": "None",
"param": None,
"code": "403",
}
}, viewer_post.text

View file

@ -104,7 +104,7 @@ def test_global_spend_reset_zeroes_token_and_team_spend_but_keeps_logs(gateway:
"error": {
"message": "Authentication Error, Tried to access route=/global/spend/reset, which is only for MASTER KEY",
"type": "auth_error",
"param": "None",
"param": None,
"code": "401",
}
}, refused.text

View file

@ -1298,3 +1298,17 @@ def test_as_proxy_exception_keeps_jwt_scope_denial_message_shape():
assert converted.to_dict() == plain.to_dict()
assert converted.internal_message == denial.internal_message
@pytest.mark.parametrize(
"auth_failure",
[
pytest.param(HTTPException(status_code=401, detail="Authentication Error, invalid key"), id="http_exception"),
pytest.param(Exception("Invalid proxy server token passed"), id="bare_exception"),
],
)
def test_auth_failure_error_body_carries_a_json_null_param(auth_failure: Exception) -> None:
body = json.loads(json.dumps({"error": _as_proxy_exception(auth_failure).to_dict()}))
assert body["error"]["type"] == ProxyErrorTypes.auth_error.value
assert body["error"]["param"] is None

View file

@ -4368,6 +4368,7 @@ async def test_removed_signing_key_stops_being_trusted_once_the_stale_window_exp
assert exc_info.value.code == "503"
assert exc_info.value.type == ProxyErrorTypes.auth_provider_unavailable
assert exc_info.value.param is None
@pytest.mark.asyncio

View file

@ -20,7 +20,7 @@ from litellm.proxy.common_utils.openai_error_payload import (
(401, "authentication_error"),
(403, "permission_error"),
(404, "invalid_request_error"),
(408, "invalid_request_error"),
(408, "timeout_error"),
(422, "invalid_request_error"),
(429, "rate_limit_error"),
(499, "invalid_request_error"),
@ -161,6 +161,16 @@ def test_an_upstream_5xx_body_does_not_relabel_the_internal_server_error():
assert openai_error_type(carried, error_status_code(carried, 400)) == "internal_server_error"
def test_an_upstream_timeout_is_not_typed_as_the_clients_mistake():
"""A provider timeout carries status 408 and no type, and calling it an
invalid_request_error tells a client branching on type not to retry a request that can succeed."""
from litellm.exceptions import Timeout
timeout = Timeout(message="Request timed out", model="gpt-5.4-mini", llm_provider="openai")
assert openai_error_type(timeout, error_status_code(timeout, 500)) == "timeout_error"
def test_a_stringified_none_type_or_param_is_treated_as_absent():
from litellm.exceptions import BadRequestError

View file

@ -1751,6 +1751,18 @@ async def test_health_services_endpoint_rejects_unknown_service():
await health_services_endpoint(service="totally_unknown_service_xyz")
@pytest.mark.asyncio
async def test_health_services_endpoint_rejection_is_openai_shaped():
"""A caller mistake here must serialize as an OpenAI error object: a real
`type` string and a JSON null `param`, never the literal string "None"."""
with pytest.raises(ProxyException) as exc_info:
await health_services_endpoint(service="totally_unknown_service_xyz")
body = json.loads(json.dumps({"error": exc_info.value.to_dict()}))
assert body["error"]["type"] == "auth_error"
assert body["error"]["param"] is None
@pytest.mark.asyncio
@pytest.mark.parametrize(
"role",

View file

@ -4970,7 +4970,7 @@ _DB_OUTAGE_503_BODY: Final = {
"error": {
"message": "Service Unavailable, the authentication database is temporarily unreachable. Please retry shortly.",
"type": "no_db_connection",
"param": "None",
"param": None,
"code": "503",
}
}

View file

@ -23377,3 +23377,20 @@ def test_key_info_database_not_connected_sanitized_500(
assert "Database not connected" in caplog.text
finally:
proxy_server.app.dependency_overrides.pop(user_api_key_auth, None)
def test_key_health_failure_body_carries_a_json_null_param():
import litellm.proxy.proxy_server as ps
app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth(
api_key="hashed-key", metadata={"logging": ["not-a-callback-object"]}
)
try:
response = client.post("/key/health", headers={"Authorization": "Bearer sk-test"})
finally:
app.dependency_overrides.pop(ps.user_api_key_auth, None)
assert response.status_code == 500
error = response.json()["error"]
assert error["type"] == "internal_server_error"
assert error["param"] is None

View file

@ -19095,7 +19095,7 @@ _DB_OUTAGE_503_BODY: Final = {
"error": {
"message": "Service Unavailable, the authentication database is temporarily unreachable. Please retry shortly.",
"type": "no_db_connection",
"param": "None",
"param": None,
"code": "503",
}
}

View file

@ -438,7 +438,7 @@ _DB_OUTAGE_503_BODY: Final = {
"error": {
"message": "Service Unavailable, the authentication database is temporarily unreachable. Please retry shortly.",
"type": "no_db_connection",
"param": "None",
"param": None,
"code": "503",
}
}

View file

@ -8268,3 +8268,19 @@ async def test_global_spend_refresh_runs_the_refresh_on_the_writer_when_a_reader
assert [statement for statement in journal.statements if statement[1].startswith("REFRESH")] == [
("writer", 'REFRESH MATERIALIZED VIEW "MonthlyGlobalSpend";')
]
def test_spend_calculate_rejection_body_is_openai_shaped(client):
app.dependency_overrides[ps.user_api_key_auth] = lambda: UserAPIKeyAuth(
user_role=LitellmUserRoles.PROXY_ADMIN, user_id="admin_user", api_key="hashed-k"
)
try:
response = client.post("/spend/calculate", json={}, headers={"Authorization": "Bearer sk-test"})
finally:
app.dependency_overrides.pop(ps.user_api_key_auth, None)
assert response.status_code == 400
error = response.json()["error"]
assert error["type"] == "invalid_request_error"
assert error["param"] is None
assert error["code"] == "400"

View file

@ -7253,6 +7253,39 @@ async def test_async_data_generator_closes_the_guardrail_chain_before_returning_
assert cleanup_ran == [True]
@pytest.mark.asyncio
async def test_async_data_generator_midstream_failure_frame_is_openai_shaped_with_a_json_null_param():
from litellm.proxy._types import UserAPIKeyAuth
from litellm.proxy.proxy_server import async_data_generator
async def chain_failing_after_first_chunk(**_kwargs):
yield {"choices": [{"delta": {"content": "Hello"}}]}
raise ValueError("audit hook could not reach its sink")
proxy_logging = _guarded_chain_logging(chain_failing_after_first_chunk)
proxy_logging.post_call_failure_hook = AsyncMock(return_value=None)
with patch("litellm.proxy.proxy_server.proxy_logging_obj", proxy_logging):
frames = [
frame
async for frame in async_data_generator(
MagicMock(), MagicMock(spec=UserAPIKeyAuth), {"model": "gpt-4o-mini"}
)
]
error_bodies = [json.loads(frame.removeprefix("data: ")) for frame in frames if '"error"' in frame]
assert error_bodies == [
{
"error": {
"message": "audit hook could not reach its sink",
"type": "internal_server_error",
"param": None,
"code": "500",
}
}
]
@pytest.mark.asyncio
async def test_async_data_generator_closes_the_guardrail_chain_while_a_keepalive_read_is_pending():
from litellm.proxy._types import UserAPIKeyAuth
@ -14220,6 +14253,51 @@ async def test_moderations_reraises_proxy_exception_unwrapped():
mock_logging.post_call_failure_hook.assert_awaited_once()
@pytest.mark.asyncio
@pytest.mark.parametrize(
("failure", "expected_type", "expected_code"),
[
pytest.param(HTTPException(status_code=400, detail="bad input"), "invalid_request_error", "400", id="http_400"),
pytest.param(HTTPException(status_code=429, detail="slow down"), "rate_limit_error", "429", id="http_429"),
pytest.param(Exception("provider down"), "internal_server_error", "500", id="bare_exception"),
],
)
async def test_moderations_failure_body_is_openai_shaped(failure, expected_type, expected_code):
from litellm.proxy._types import ProxyException
async def passthrough_add_litellm_data(data, **kwargs):
return data
request = MagicMock()
request.body = AsyncMock(return_value=b'{"input": "hi"}')
fake_logging = MagicMock()
fake_logging.pre_call_hook = AsyncMock(side_effect=lambda user_api_key_dict, data, call_type: data)
fake_logging.post_call_failure_hook = AsyncMock()
with (
patch.object(
proxy_server_module, "add_litellm_data_to_request", new=passthrough_add_litellm_data
), # test-quality-ok: the route reads this module global, no injection point
patch.object(
proxy_server_module, "route_request", new=AsyncMock(side_effect=failure)
), # test-quality-ok: fakes the provider failure so the real route's error shaping is observable
patch.object(
proxy_server_module, "proxy_logging_obj", new=fake_logging
), # test-quality-ok: module global, no injection point
pytest.raises(ProxyException) as raised,
):
await proxy_server_module.moderations(
request=request,
fastapi_response=MagicMock(),
user_api_key_dict=UserAPIKeyAuth(api_key="sk-test"),
)
body = json.loads(json.dumps({"error": raised.value.to_dict()}))
assert body["error"]["type"] == expected_type
assert body["error"]["param"] is None
assert body["error"]["code"] == expected_code
@pytest.mark.asyncio
async def test_moderations_response_carries_litellm_call_id_header():
from fastapi import Response