Coverage for .venv/lib/python3.13/site-packages/litellm/proxy/common_utils/swagger_utils.py: 93%

19 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 12:01 +0000

1import inspect 

2from typing import Any, Final 

3 

4from pydantic import BaseModel, Field 

5 

6from litellm.exceptions import LITELLM_EXCEPTION_TYPES 

7 

8 

9class ErrorResponse(BaseModel): 

10 detail: dict[str, Any] = Field( 

11 ..., 

12 example={ 

13 "error": { 

14 "message": "Error message", 

15 "type": "error_type", 

16 "param": "error_param", 

17 "code": "error_code", 

18 } 

19 }, 

20 ) 

21 

22 

23# Define a function to get the status code 

24def get_status_code(exception): 

25 if hasattr(exception, "status_code"): 

26 return exception.status_code 

27 # Default status codes for exceptions without a status_code attribute 

28 if exception.__name__ == "Timeout": 

29 return 408 # Request Timeout 

30 if exception.__name__ == "APIConnectionError": 

31 return 503 # Service Unavailable 

32 return 500 # Internal Server Error as default 

33 

34 

35def _error_description(exception: type[Exception]) -> str: 

36 return inspect.cleandoc(exception.__doc__) if exception.__doc__ else exception.__name__ 

37 

38 

39# Create error responses 

40ERROR_RESPONSES: Final = { 

41 get_status_code(exception): { 

42 "model": ErrorResponse, 

43 "description": _error_description(exception), 

44 } 

45 for exception in LITELLM_EXCEPTION_TYPES 

46} 

47 

48# Ensure we have a 500 error response 

49if 500 not in ERROR_RESPONSES: 49 ↛ 50line 49 didn't jump to line 50 because the condition on line 49 was never true

50 ERROR_RESPONSES[500] = { 

51 "model": ErrorResponse, 

52 "description": "Internal Server Error", 

53 }