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
This commit is contained in:
Justin Norman 2026-03-06 18:44:48 -07:00
parent 3a2cba43dc
commit 6a3a4beca8
2 changed files with 30 additions and 0 deletions

View file

@ -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]]] = {

View file

@ -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"