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): stop shipping the literal string "None" as error type and param
mateo-berribede8b5ea46c
diff --git a/litellm/proxy/anthropic_endpoints/endpoints.py b/litellm/proxy/anthropic_endpoints/endpoints.pyindex 7f0045c1d93..f26cb4d41f7 100644--- a/litellm/proxy/anthropic_endpoints/endpoints.py+++ b/litellm/proxy/anthropic_endpoints/endpoints.py@@ -25,6 +25,11 @@ from litellm.proxy.common_request_processing import (proxy_exception_from_http_exception,)from litellm.proxy.common_utils.http_parsing_utils import _read_request_body+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)from litellm.types.utils import TokenCountResponserouter: Final = APIRouter()@@ -221,9 +226,9 @@ async def anthropic_response(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),headers=headers,)diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.pyindex 6542842f5e4..99028c3645a 100644--- a/litellm/proxy/common_request_processing.py+++ b/litellm/proxy/common_request_processing.py@@ -54,6 +54,12 @@ from litellm.proxy.common_utils.callback_utils import (get_logging_caching_headers,get_remaining_tokens_and_requests_from_request_data,)+from litellm.proxy.common_utils.openai_error_payload import (+ attribute_of,+ error_status_code,+ openai_error_param,+ openai_error_type,+)from litellm.proxy.common_utils.sse_keepalive import (SSE_COMMENT_PING_BYTES,coerce_keepalive_interval,@@ -463,46 +469,6 @@ def _stream_usage_tracking_updates(}-def _getattr_object(value: object, name: str, default: object = None) -> object:- return getattr(value, name, default)---_OPENAI_ERROR_TYPE_BY_STATUS: Final[Mapping[int, str]] = MappingProxyType(- {- status.HTTP_401_UNAUTHORIZED: "authentication_error",- status.HTTP_403_FORBIDDEN: "permission_error",- status.HTTP_429_TOO_MANY_REQUESTS: "rate_limit_error",- }-)---def _error_status_code(exc: object, default: int) -> int:- """The HTTP status an exception carries, or ``default`` when it carries none."""- carried: Final = _getattr_object(exc, "status_code")- return carried if isinstance(carried, int) and not isinstance(carried, bool) else default---def _openai_error_type(exc: object, status_code: int) -> str:- """OpenAI types ``error.type`` as a required string, so an exception carrying none- falls back to the type its status code stands for."""- carried: Final = _getattr_object(exc, "type")- if isinstance(carried, str):- return carried- mapped: Final = _OPENAI_ERROR_TYPE_BY_STATUS.get(status_code)- if mapped is not None:- return mapped- if status_code < status.HTTP_500_INTERNAL_SERVER_ERROR:- return "invalid_request_error"- return "internal_server_error"---def _openai_error_param(exc: object) -> str | None:- """OpenAI types ``error.param`` as nullable, so an exception carrying none- serializes as JSON ``null``."""- carried: Final = _getattr_object(exc, "param")- return carried if isinstance(carried, str) else None--class _UpstreamHttpResponse(Protocol):@propertydef status_code(self) -> int: ...@@ -572,15 +538,15 @@ def serialize_http_exception_detail(def proxy_exception_from_http_exception(exc: HTTPException, headers: dict[str, str]) -> ProxyException:- raw_detail: Final = _getattr_object(exc, "detail", str(exc))+ raw_detail: Final = attribute_of(exc, "detail", str(exc))message, structured_fields = serialize_http_exception_detail(raw_detail)existing_fields: Final = getattr(exc, "provider_specific_fields", None) or {}merged_fields: Final = {**existing_fields, **structured_fields} if structured_fields else (existing_fields or None)- error_status: Final = _error_status_code(exc, status.HTTP_400_BAD_REQUEST)+ error_status: Final = error_status_code(exc, status.HTTP_400_BAD_REQUEST)return ProxyException(message=message,- type=_openai_error_type(exc, error_status),- param=_openai_error_param(exc),+ type=openai_error_type(exc, error_status),+ param=openai_error_param(exc),code=error_status,provider_specific_fields=merged_fields,headers=headers,@@ -864,8 +830,8 @@ def sse_error_payload(exc: BaseException) -> tuple[int, Mapping[str, object]]:are byte-identical."""# Preserve status code from HTTPException (e.g. guardrail blocks)- error_status: Final = _error_status_code(exc, status.HTTP_500_INTERNAL_SERVER_ERROR)- raw_detail: Final = _getattr_object(exc, "detail", "Error processing stream start")+ error_status: Final = error_status_code(exc, status.HTTP_500_INTERNAL_SERVER_ERROR)+ raw_detail: Final = attribute_of(exc, "detail", "Error processing stream start")message, structured_fields = serialize_http_exception_detail(raw_detail)existing_fields: Final = getattr(exc, "provider_specific_fields", None) or {}@@ -873,8 +839,8 @@ def sse_error_payload(exc: BaseException) -> tuple[int, Mapping[str, object]]:error_obj: Final = {"message": message,- "type": _openai_error_type(exc, error_status),- "param": _openai_error_param(exc),+ "type": openai_error_type(exc, error_status),+ "param": openai_error_param(exc),"code": str(error_status),}if not merged_fields:@@ -2755,10 +2721,10 @@ class ProxyBaseLLMRequestProcessing:``ResponsesAPIResponse`` directly. Handle both shapes so thecontainer-ownership recording path can walk ``.output`` either way."""- completed: Final = _getattr_object(stream_response, "completed_response")+ completed: Final = attribute_of(stream_response, "completed_response")if completed is None:return None- response_obj: Final = _getattr_object(completed, "response")+ response_obj: Final = attribute_of(completed, "response")if response_obj is not None:return response_objreturn completed@@ -3380,7 +3346,7 @@ class ProxyBaseLLMRequestProcessing:headers = getattr(e, "headers", None) or {}if not headers:# Try to get headers from e.response.headers (httpx.Response)- _response: Final = _getattr_object(e, "response")+ _response: Final = attribute_of(e, "response")if _response is not None:_response_headers: Final = getattr(_response, "headers", None)if _response_headers:@@ -3451,8 +3417,8 @@ class ProxyBaseLLMRequestProcessing:_code = status.HTTP_500_INTERNAL_SERVER_ERRORraise ProxyException(message=redact_internal_details_from_client_message(getattr(e, "message", error_msg)),- type=_openai_error_type(e, _code),- param=_openai_error_param(e),+ type=openai_error_type(e, _code),+ param=openai_error_param(e),openai_code=getattr(e, "code", None),code=_code,provider_specific_fields=getattr(e, "provider_specific_fields", None),@@ -3662,11 +3628,11 @@ class ProxyBaseLLMRequestProcessing:if isinstance(e, HTTPException):raise e- stream_error_status: Final = _error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR)+ stream_error_status: Final = error_status_code(e, status.HTTP_500_INTERNAL_SERVER_ERROR)proxy_exception: Final = ProxyException(message=redact_internal_details_from_client_message(getattr(e, "message", str(e))),- type=_openai_error_type(e, stream_error_status),- param=_openai_error_param(e),+ type=openai_error_type(e, stream_error_status),+ param=openai_error_param(e),code=stream_error_status,)stream_completed = Truediff --git a/litellm/proxy/common_utils/openai_error_payload.py b/litellm/proxy/common_utils/openai_error_payload.pynew file mode 100644index 00000000000..180ec152094--- /dev/null+++ b/litellm/proxy/common_utils/openai_error_payload.py@@ -0,0 +1,48 @@+"""Shapes the ``error`` object the proxy answers with so it matches OpenAI's contract:+``type`` is a required string and ``param`` is nullable, neither of which the literal+string ``"None"`` satisfies."""++from collections.abc import Mapping+from types import MappingProxyType+from typing import Final++from fastapi import status++_OPENAI_ERROR_TYPE_BY_STATUS: Final[Mapping[int, str]] = MappingProxyType(+ {+ status.HTTP_401_UNAUTHORIZED: "authentication_error",+ status.HTTP_403_FORBIDDEN: "permission_error",+ status.HTTP_429_TOO_MANY_REQUESTS: "rate_limit_error",+ }+)+++def attribute_of(value: object, name: str, default: object = None) -> object:+ return getattr(value, name, default)+++def error_status_code(exc: object, default: int) -> int:+ """The HTTP status an exception carries, or ``default`` when it carries none."""+ carried: Final = attribute_of(exc, "status_code")+ return carried if isinstance(carried, int) and not isinstance(carried, bool) else default+++def openai_error_type(exc: object, status_code: int) -> str:+ """OpenAI types ``error.type`` as a required string, so an exception carrying none+ falls back to the type its status code stands for."""+ carried: Final = attribute_of(exc, "type")+ if isinstance(carried, str):+ return carried+ mapped: Final = _OPENAI_ERROR_TYPE_BY_STATUS.get(status_code)+ if mapped is not None:+ return mapped+ if status_code < status.HTTP_500_INTERNAL_SERVER_ERROR:+ return "invalid_request_error"+ return "internal_server_error"+++def openai_error_param(exc: object) -> str | None:+ """OpenAI types ``error.param`` as nullable, so an exception carrying none+ serializes as JSON ``null``."""+ carried: Final = attribute_of(exc, "param")+ return carried if isinstance(carried, str) else Nonediff --git a/litellm/proxy/image_endpoints/endpoints.py b/litellm/proxy/image_endpoints/endpoints.pyindex 83caa92ede5..b83f7e5cd60 100644--- a/litellm/proxy/image_endpoints/endpoints.py+++ b/litellm/proxy/image_endpoints/endpoints.py@@ -16,6 +16,11 @@ from litellm.litellm_core_utils.prompt_templates.common_utils import (from litellm.proxy._types import *from litellm.proxy.auth.user_api_key_auth import UserAPIKeyAuth, user_api_key_authfrom litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)from litellm.proxy.route_llm_request import route_requestfrom litellm.types.llms.openai import ChatCompletionUserMessage@@ -193,18 +198,18 @@ async def image_generation(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", 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),)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),openai_code=getattr(e, "code", None),- code=getattr(e, "status_code", 500),+ code=error_status_code(e, 500),)diff --git a/litellm/proxy/openai_files_endpoints/files_endpoints.py b/litellm/proxy/openai_files_endpoints/files_endpoints.pyindex bf07f4748ef..c315d30b8f3 100644--- a/litellm/proxy/openai_files_endpoints/files_endpoints.py+++ b/litellm/proxy/openai_files_endpoints/files_endpoints.py@@ -45,6 +45,11 @@ from litellm.proxy.common_utils.openai_endpoint_utils import (get_custom_llm_provider_from_request_headers,get_custom_llm_provider_from_request_query,)+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)from litellm.proxy.openai_files_endpoints.batch_file_validation import (check_batch_file_upload,raise_batch_file_validation_failure,@@ -296,22 +301,22 @@ async def route_create_file(if managed_files_obj is None:raise ProxyException(message="Managed files hook not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if llm_router is None:raise ProxyException(message="LLM Router not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if not isinstance(managed_files_obj, BaseFileEndpoints):raise ProxyException(message="Managed files hook is not a BaseFileEndpoints",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)# Managed files internally calls llm_router.acreate_file() which includes loadbalancing@@ -713,17 +718,17 @@ async def create_file(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e.detail)),- 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),)else: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),)finally:for spool in spools:@@ -812,22 +817,22 @@ async def get_file_content(if managed_files_obj is None:raise ProxyException(message="Managed files hook not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if llm_router is None:raise ProxyException(message="LLM Router not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if not isinstance(managed_files_obj, BaseFileEndpoints):raise ProxyException(message="Managed files hook is not a BaseFileEndpoints",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)@@ -1021,17 +1026,17 @@ async def get_file_content(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e.detail)),- 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),)else: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),)@@ -1151,15 +1156,15 @@ async def get_file(if managed_files_obj is None:raise ProxyException(message="Managed files hook not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if not isinstance(managed_files_obj, BaseFileEndpoints):raise ProxyException(message="Managed files hook is not a BaseFileEndpoints",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)response = await managed_files_obj.afile_retrieve(@@ -1215,17 +1220,17 @@ async def get_file(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e.detail)),- 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),)else: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),)@@ -1355,22 +1360,22 @@ async def delete_file(if managed_files_obj is None:raise ProxyException(message="Managed files hook not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if llm_router is None:raise ProxyException(message="LLM Router not found",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)if not isinstance(managed_files_obj, BaseFileEndpoints):raise ProxyException(message="Managed files hook is not a BaseFileEndpoints",- type="None",- param="None",+ type=ProxyErrorTypes.internal_server_error.value,+ param=None,code=500,)@@ -1427,17 +1432,17 @@ async def delete_file(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e.detail)),- 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),)else: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),)@@ -1629,15 +1634,15 @@ async def list_files(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e.detail)),- 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),)else: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),)diff --git a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.pyindex 79d5d0a016f..64ce461990a 100644--- a/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py+++ b/litellm/proxy/pass_through_endpoints/pass_through_endpoints.py@@ -77,6 +77,11 @@ from litellm.proxy.common_utils.http_parsing_utils import (_read_request_body,_safe_get_request_headers,)+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)from litellm.proxy.common_utils.sse_keepalive import (wrap_passthrough_sse_bytes_with_keepalive_pings,)@@ -310,9 +315,9 @@ async def chat_completion_pass_through_endpoint(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),)@@ -1677,18 +1682,18 @@ async def pass_through_request(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(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),headers=custom_headers,)else: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),headers=custom_headers,)diff --git a/litellm/proxy/realtime_endpoints/endpoints.py b/litellm/proxy/realtime_endpoints/endpoints.pyindex 7f9cd251a8a..9996f60098d 100644--- a/litellm/proxy/realtime_endpoints/endpoints.py+++ b/litellm/proxy/realtime_endpoints/endpoints.py@@ -17,6 +17,11 @@ from litellm.proxy.common_utils.encrypt_decrypt_utils import (encrypt_value_helper,)from litellm.proxy.common_utils.http_parsing_utils import _read_request_body+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)from litellm.types.realtime import (RealtimeClientSecretRequest,RealtimeClientSecretResponse,@@ -301,15 +306,15 @@ async def create_realtime_client_secret(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e)),- type=getattr(e, "type", "None"),- param=getattr(e, "param", "None"),- code=getattr(e, "status_code", http_status.HTTP_400_BAD_REQUEST),+ type=openai_error_type(e, error_status_code(e, http_status.HTTP_400_BAD_REQUEST)),+ param=openai_error_param(e),+ code=error_status_code(e, http_status.HTTP_400_BAD_REQUEST),)raise ProxyException(message=getattr(e, "message", str(e)),- 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),)if upstream_resp.status_code != 200:@@ -492,15 +497,15 @@ async def proxy_realtime_calls(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", str(e)),- type=getattr(e, "type", "None"),- param=getattr(e, "param", "None"),- code=getattr(e, "status_code", http_status.HTTP_400_BAD_REQUEST),+ type=openai_error_type(e, error_status_code(e, http_status.HTTP_400_BAD_REQUEST)),+ param=openai_error_param(e),+ code=error_status_code(e, http_status.HTTP_400_BAD_REQUEST),)raise ProxyException(message=getattr(e, "message", str(e)),- 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),)return Response(@@ -605,15 +610,15 @@ async def create_realtime_transcription_session(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "detail", getattr(e, "message", str(e))),- type=getattr(e, "type", "None"),- param=getattr(e, "param", "None"),- code=getattr(e, "status_code", http_status.HTTP_400_BAD_REQUEST),+ type=openai_error_type(e, error_status_code(e, http_status.HTTP_400_BAD_REQUEST)),+ param=openai_error_param(e),+ code=error_status_code(e, http_status.HTTP_400_BAD_REQUEST),)raise ProxyException(message=getattr(e, "message", str(e)),- 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),)if upstream_resp.status_code != 200:diff --git a/litellm/proxy/rerank_endpoints/endpoints.py b/litellm/proxy/rerank_endpoints/endpoints.pyindex dd5803796b7..16cd7368e4a 100644--- a/litellm/proxy/rerank_endpoints/endpoints.py+++ b/litellm/proxy/rerank_endpoints/endpoints.py@@ -11,6 +11,11 @@ from litellm._logging import verbose_proxy_loggerfrom litellm.proxy._types import *from litellm.proxy.auth.user_api_key_auth import user_api_key_authfrom litellm.proxy.common_request_processing import ProxyBaseLLMRequestProcessing+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)router: Final = APIRouter()@@ -112,15 +117,15 @@ async def rerank(if isinstance(e, HTTPException):raise ProxyException(message=getattr(e, "message", 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),)else: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),)diff --git a/litellm/proxy/utils.py b/litellm/proxy/utils.pyindex cab2bd6d9db..5c0fbd64744 100644--- a/litellm/proxy/utils.py+++ b/litellm/proxy/utils.py@@ -37,6 +37,7 @@ from litellm.proxy._types import (SpendLogsMetadata,SpendLogsPayload,)+from litellm.proxy.common_utils.openai_error_payload import openai_error_paramfrom litellm.proxy.spend_tracking.spend_log_error_logger import spend_log_errorfrom litellm.types.guardrails import GuardrailEventHooksfrom litellm.types.proxy.model_listing import ModelInfoResponse@@ -7098,7 +7099,7 @@ def handle_exception_on_proxy(e: Exception) -> ProxyException:return 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):@@ -7107,7 +7108,7 @@ def handle_exception_on_proxy(e: Exception) -> ProxyException:return ProxyException(message=str(e),type=ProxyErrorTypes.internal_server_error,- param=getattr(e, "param", "None"),+ param=openai_error_param(e),code=_status_code,)diff --git a/tests/test_litellm/proxy/common_utils/test_openai_error_payload.py b/tests/test_litellm/proxy/common_utils/test_openai_error_payload.pynew file mode 100644index 00000000000..c165d7ffdb1--- /dev/null+++ b/tests/test_litellm/proxy/common_utils/test_openai_error_payload.py@@ -0,0 +1,117 @@+import json++import pytest+from fastapi import HTTPException++from litellm.proxy._types import ProxyErrorTypes, ProxyException+from litellm.proxy.common_utils.openai_error_payload import (+ error_status_code,+ openai_error_param,+ openai_error_type,+)+++ "status_code, expected_type",+ [+ (400, "invalid_request_error"),+ (401, "authentication_error"),+ (403, "permission_error"),+ (404, "invalid_request_error"),+ (422, "invalid_request_error"),+ (429, "rate_limit_error"),+ (499, "invalid_request_error"),+ (500, "internal_server_error"),+ (502, "internal_server_error"),+ (503, "internal_server_error"),+ ],+)+def test_status_code_decides_the_type_when_the_exception_carries_none(status_code, expected_type):+ """A route that raises a bare HTTPException carries no error type, so the status it+ answered with is the only thing left to name the OpenAI type from."""+ assert openai_error_type(HTTPException(status_code=status_code, detail="boom"), status_code) == expected_type+++def test_a_carried_type_wins_over_the_one_the_status_would_imply():+ """A ProxyException raised mid-request already names its own type, and relabelling a+ 402 budget_exceeded as the status map's guess would lose what the client branches on."""+ carried = ProxyException(+ message="Budget has been exceeded",+ type=ProxyErrorTypes.budget_exceeded.value,+ param=None,+ code=400,+ )++ assert openai_error_type(carried, 400) == ProxyErrorTypes.budget_exceeded.value++[email protected]("carried_type", [None, 400, {"type": "invalid_request_error"}, ["invalid_request_error"]])+def test_a_non_string_carried_type_falls_back_to_the_status(carried_type):+ """OpenAI types error.type as a string, so anything else on the exception is not one and+ must not reach the wire the way the literal "None" used to."""++ class _Carrier(Exception):+ type = carried_type++ assert openai_error_type(_Carrier("boom"), 401) == "authentication_error"+++def test_the_type_is_never_the_string_none_after_a_json_round_trip():+ """The bug this module exists for: json.dumps of a "None" default is indistinguishable+ from a real type to a client's error handler."""+ payload = json.loads(+ json.dumps(+ {+ "type": openai_error_type(HTTPException(status_code=400, detail="boom"), 400),+ "param": openai_error_param(HTTPException(status_code=400, detail="boom")),+ }+ )+ )++ assert payload == {"type": "invalid_request_error", "param": None}+++def test_a_carried_param_names_the_offending_field():+ carried = ProxyException(message="Invalid purpose", type="invalid_request_error", param="purpose", code=400)++ assert openai_error_param(carried) == "purpose"++[email protected]("exc", [HTTPException(status_code=400, detail="boom"), ValueError("boom"), None])+def test_param_is_json_null_when_the_exception_names_no_field(exc):+ assert openai_error_param(exc) is None+++def test_a_non_string_carried_param_is_json_null():+ class _Carrier(Exception):+ param = 42++ assert openai_error_param(_Carrier("boom")) is None+++def test_a_carried_status_code_wins_over_the_default():+ assert error_status_code(HTTPException(status_code=429, detail="slow down"), 400) == 429++[email protected]("default", [400, 500])+def test_the_default_status_stands_when_the_exception_carries_none(default):+ assert error_status_code(ValueError("boom"), default) == default++[email protected]("carried_status", [True, False, "429", None, 429.0])+def test_a_non_int_carried_status_falls_back_to_the_default(carried_status):+ """True is an int in Python but not an HTTP status, and a stringified one would break+ every caller that compares the code numerically."""++ class _Carrier(Exception):+ status_code = carried_status++ assert error_status_code(_Carrier("boom"), 500) == 500+++def test_a_status_carried_by_an_exception_drives_the_type_it_reports():+ """The two helpers compose at every call site: the status the exception carries is what+ names its type, not the default the route would have used."""+ exc = HTTPException(status_code=403, detail="blocked by policy")++ assert openai_error_type(exc, error_status_code(exc, 400)) == "permission_error"diff --git a/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py b/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.pyindex bca97915347..123d54789bf 100644--- a/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py+++ b/tests/test_litellm/proxy/openai_files_endpoint/test_files_endpoint.py@@ -2920,9 +2920,9 @@ def test_unscoped_list_files_accepts_every_documented_purpose(def test_list_files_reports_a_bad_target_model_names_as_a_400(mocker: MockerFixture, monkeypatch, llm_router: Router):- """The exception tail reports an HTTPException with its own status and error- type rather than relabelling it, so a client that branches on either keeps- reading the same thing off a bad request."""+ """The exception tail answers with the OpenAI error object a client can branch on:+ the type its 400 status stands for, and a JSON null param rather than the literal+ string "None" no OpenAI SDK has a case for."""_setup_unscoped_list_files_route(mocker, monkeypatch, llm_router, _permissive_afile_list)response = _get_list_files("/v1/files?target_model_names=gpt-3.5-turbo,gpt-4o")@@ -2931,8 +2931,8 @@ def test_list_files_reports_a_bad_target_model_names_as_a_400(assert response.json() == {"error": {"message": "target_model_names on list files must be a list of one model name. Example: ['gpt-4o']",- "type": "None",- "param": "None",+ "type": "invalid_request_error",+ "param": None,"code": "400",}}diff --git a/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py b/tests/test_litellm/proxy/utils/helpers/test_error_helpers.pyindex e73e3c151e0..dc30798df55 100644--- a/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py+++ b/tests/test_litellm/proxy/utils/helpers/test_error_helpers.py@@ -135,7 +135,7 @@ def test_handle_exception_on_proxy_happy_path_generic_exception_defaults_to_500("message": "kaboom","type": ProxyErrorTypes.internal_server_error.value,"code": "500",- "param": "None",+ "param": None,}