mirror of
https://github.com/BerriAI/litellm.git
synced 2026-10-10 03:28:53 +00:00
Merge 04e965a085 into 461a58c40a
This commit is contained in:
commit
d5ee756353
2 changed files with 64 additions and 14 deletions
|
|
@ -4,7 +4,7 @@ import re
|
|||
import traceback
|
||||
from collections.abc import Mapping
|
||||
from types import MappingProxyType
|
||||
from typing import Final, Protocol, cast
|
||||
from typing import Final, Optional, Protocol, cast
|
||||
|
||||
import httpx
|
||||
|
||||
|
|
@ -419,8 +419,25 @@ def _map_openai_exception(
|
|||
request=_request,
|
||||
litellm_debug_info=extra_information,
|
||||
)
|
||||
elif hasattr(original_exception, "status_code"):
|
||||
if original_exception.status_code == 400:
|
||||
def _extract_status_code() -> Optional[int]:
|
||||
try:
|
||||
if hasattr(original_exception, "status_code") and getattr(original_exception, "status_code", None) is not None:
|
||||
return int(original_exception.status_code)
|
||||
elif (
|
||||
hasattr(original_exception, "response")
|
||||
and hasattr(original_exception.response, "status_code")
|
||||
and getattr(original_exception.response, "status_code", None) is not None
|
||||
):
|
||||
return int(original_exception.response.status_code)
|
||||
except (ValueError, TypeError, AttributeError):
|
||||
# Ignore parsing errors for malformed status codes and fall back to None
|
||||
pass
|
||||
return None
|
||||
|
||||
_status_code: Final = _extract_status_code()
|
||||
|
||||
if _status_code is not None:
|
||||
if _status_code == 400:
|
||||
raise BadRequestError(
|
||||
message=f"{exception_provider} - {message}",
|
||||
llm_provider=custom_llm_provider,
|
||||
|
|
@ -429,7 +446,7 @@ def _map_openai_exception(
|
|||
litellm_debug_info=extra_information,
|
||||
body=getattr(original_exception, "body", None),
|
||||
)
|
||||
elif original_exception.status_code == 401:
|
||||
elif _status_code == 401:
|
||||
raise AuthenticationError(
|
||||
message=f"AuthenticationError: {exception_provider} - {message}",
|
||||
llm_provider=custom_llm_provider,
|
||||
|
|
@ -437,7 +454,7 @@ def _map_openai_exception(
|
|||
response=response,
|
||||
litellm_debug_info=extra_information,
|
||||
)
|
||||
elif original_exception.status_code == 404:
|
||||
elif _status_code == 404:
|
||||
raise NotFoundError(
|
||||
message=f"NotFoundError: {exception_provider} - {message}",
|
||||
model=model,
|
||||
|
|
@ -445,14 +462,14 @@ def _map_openai_exception(
|
|||
response=response,
|
||||
litellm_debug_info=extra_information,
|
||||
)
|
||||
elif original_exception.status_code == 408:
|
||||
elif _status_code == 408:
|
||||
raise Timeout(
|
||||
message=f"Timeout Error: {exception_provider} - {message}",
|
||||
model=model,
|
||||
llm_provider=custom_llm_provider,
|
||||
litellm_debug_info=extra_information,
|
||||
)
|
||||
elif original_exception.status_code == 422:
|
||||
elif _status_code == 422:
|
||||
raise BadRequestError(
|
||||
message=f"{exception_provider} - {message}",
|
||||
model=model,
|
||||
|
|
@ -461,7 +478,7 @@ def _map_openai_exception(
|
|||
litellm_debug_info=extra_information,
|
||||
body=getattr(original_exception, "body", None),
|
||||
)
|
||||
elif original_exception.status_code == 429:
|
||||
elif _status_code == 429:
|
||||
raise RateLimitError(
|
||||
message=f"RateLimitError: {exception_provider} - {message}",
|
||||
model=model,
|
||||
|
|
@ -470,7 +487,7 @@ def _map_openai_exception(
|
|||
litellm_debug_info=extra_information,
|
||||
body=getattr(original_exception, "body", None),
|
||||
)
|
||||
elif original_exception.status_code == 500:
|
||||
elif _status_code == 500:
|
||||
raise InternalServerError(
|
||||
message=f"InternalServerError: {exception_provider} - {message}",
|
||||
model=model,
|
||||
|
|
@ -479,7 +496,7 @@ def _map_openai_exception(
|
|||
litellm_debug_info=extra_information,
|
||||
body=getattr(original_exception, "body", None),
|
||||
)
|
||||
elif original_exception.status_code == 502:
|
||||
elif _status_code == 502:
|
||||
raise BadGatewayError(
|
||||
message=f"BadGatewayError: {exception_provider} - {message}",
|
||||
model=model,
|
||||
|
|
@ -487,7 +504,7 @@ def _map_openai_exception(
|
|||
response=response,
|
||||
litellm_debug_info=extra_information,
|
||||
)
|
||||
elif original_exception.status_code == 503:
|
||||
elif _status_code == 503:
|
||||
raise ServiceUnavailableError(
|
||||
message=f"ServiceUnavailableError: {exception_provider} - {message}",
|
||||
model=model,
|
||||
|
|
@ -495,17 +512,17 @@ def _map_openai_exception(
|
|||
response=response,
|
||||
litellm_debug_info=extra_information,
|
||||
)
|
||||
elif original_exception.status_code == 504: # gateway timeout error
|
||||
elif _status_code == 504: # gateway timeout error
|
||||
raise Timeout(
|
||||
message=f"Timeout Error: {exception_provider} - {message}",
|
||||
model=model,
|
||||
llm_provider=custom_llm_provider,
|
||||
litellm_debug_info=extra_information,
|
||||
exception_status_code=original_exception.status_code,
|
||||
exception_status_code=_status_code,
|
||||
)
|
||||
else:
|
||||
raise APIError(
|
||||
status_code=original_exception.status_code,
|
||||
status_code=_status_code,
|
||||
message=f"APIError: {exception_provider} - {message}",
|
||||
llm_provider=custom_llm_provider,
|
||||
model=model,
|
||||
|
|
|
|||
|
|
@ -1573,3 +1573,36 @@ def test_guardrail_provider_failure_status_is_still_mapped():
|
|||
)
|
||||
|
||||
assert exc_info.value is not upstream_failure
|
||||
|
||||
|
||||
def test_exception_mapping_vllm_nested_status_code():
|
||||
class MockResponse:
|
||||
def __init__(self, code):
|
||||
self.status_code = code
|
||||
|
||||
class RawHTTPError(Exception):
|
||||
def __init__(self, response):
|
||||
self.response = response
|
||||
super().__init__("Mock HTTP Error")
|
||||
|
||||
crash_error = RawHTTPError(MockResponse(500))
|
||||
with pytest.raises(litellm.InternalServerError) as exc_info:
|
||||
exception_type(
|
||||
model="test-model",
|
||||
original_exception=crash_error,
|
||||
custom_llm_provider="hosted_vllm",
|
||||
completion_kwargs={},
|
||||
extra_kwargs={},
|
||||
)
|
||||
assert exc_info.value.status_code == 500
|
||||
|
||||
weird_error = RawHTTPError(MockResponse("500"))
|
||||
with pytest.raises(litellm.InternalServerError) as exc_info:
|
||||
exception_type(
|
||||
model="test-model",
|
||||
original_exception=weird_error,
|
||||
custom_llm_provider="hosted_vllm",
|
||||
completion_kwargs={},
|
||||
extra_kwargs={},
|
||||
)
|
||||
assert exc_info.value.status_code == 500
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue