From 6a3a4beca8a76e1e42d5f928822e98af9031cdd8 Mon Sep 17 00:00:00 2001 From: Justin Norman Date: Fri, 6 Mar 2026 18:44:48 -0700 Subject: [PATCH] fix: ProxyException.__str__ returns message instead of empty string Adds __str__ to ProxyException so str(exc) returns the message rather than an empty string. Fixes #22644 --- litellm/proxy/_types.py | 3 +++ .../proxy/test_proxy_exception.py | 27 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/test_litellm/proxy/test_proxy_exception.py 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"