diff --git a/litellm/proxy/_types.py b/litellm/proxy/_types.py index 95dabd8bfd0..b6c4b213b2b 100644 --- a/litellm/proxy/_types.py +++ b/litellm/proxy/_types.py @@ -3276,6 +3276,9 @@ class ProxyException(Exception): elif RouterErrors.no_deployments_with_tag_routing.value in self.message: self.code = "401" + def __str__(self) -> str: + return self.message + def to_dict(self) -> dict: """Converts the ProxyException instance to a dictionary.""" error_dict: Dict[str, Optional[Union[str, Dict]]] = { diff --git a/tests/test_litellm/proxy/test_proxy_exception.py b/tests/test_litellm/proxy/test_proxy_exception.py new file mode 100644 index 00000000000..65b17e7ce31 --- /dev/null +++ b/tests/test_litellm/proxy/test_proxy_exception.py @@ -0,0 +1,27 @@ +"""Tests for ProxyException.__str__. + +Related issue: https://github.com/BerriAI/litellm/issues/22644 +""" + +from litellm.proxy._types import ProxyException + + +def test_proxy_exception_str_returns_message(): + exc = ProxyException( + message="This is an error", + type="invalid_request_error", + param="model", + code=400, + ) + assert str(exc) == "This is an error" + + +def test_proxy_exception_str_with_non_string_message(): + exc = ProxyException( + message=42, + type="server_error", + param=None, + code=500, + ) + # __init__ already calls str(message) + assert str(exc) == "42"