mirror of
https://github.com/BerriAI/litellm.git
synced 2026-09-19 00:01:29 +00:00
fix(exceptions): handle non-integer status_code in MidStreamFallbackError
When a CustomLLM handler raises RateLimitError inside astreaming(), the error propagates through the streaming pipeline and hits MidStreamFallbackError.__init__. In some code paths the original exception's status_code attribute is a non-integer string (e.g. 'litellm_error'), causing int() to raise ValueError and crash with HTTP 500 instead of triggering the fallback chain. Wrap both int() calls with try/except (ValueError, TypeError) and fall back to 503 so the fallback mechanism can engage normally. Adds regression test: test_midstream_fallback_error_non_integer_status_code verifies status_code and response.status_code both resolve to 503. Fixes #26913
This commit is contained in:
parent
934ecdca78
commit
ed1bc3f0d3
2 changed files with 33 additions and 2 deletions
|
|
@ -955,7 +955,12 @@ class MidStreamFallbackError(ServiceUnavailableError): # type: ignore
|
|||
is_pre_first_chunk: bool = False,
|
||||
):
|
||||
original_status = getattr(original_exception, "status_code", None)
|
||||
self.status_code = int(original_status) if original_status is not None else 503
|
||||
try:
|
||||
self.status_code = (
|
||||
int(original_status) if original_status is not None else 503
|
||||
)
|
||||
except (ValueError, TypeError):
|
||||
self.status_code = 503
|
||||
self.message = f"litellm.MidStreamFallbackError: {message}"
|
||||
self.model = model
|
||||
self.llm_provider = llm_provider
|
||||
|
|
@ -997,7 +1002,12 @@ class MidStreamFallbackError(ServiceUnavailableError): # type: ignore
|
|||
)
|
||||
|
||||
# Restore the propagated status and original response/request objects
|
||||
self.status_code = int(original_status) if original_status is not None else 503
|
||||
try:
|
||||
self.status_code = (
|
||||
int(original_status) if original_status is not None else 503
|
||||
)
|
||||
except (ValueError, TypeError):
|
||||
self.status_code = 503
|
||||
self.response = _saved_response
|
||||
self.request = _saved_request
|
||||
self.message = _saved_message
|
||||
|
|
|
|||
|
|
@ -254,6 +254,27 @@ class TestExceptionAttributes:
|
|||
assert midstream_fallback.response.status_code == 503
|
||||
assert str(midstream_fallback.response.request.url) == "https://openai.com/v1/"
|
||||
|
||||
def test_midstream_fallback_error_non_integer_status_code(self):
|
||||
"""
|
||||
MidStreamFallbackError must not crash when original_exception.status_code
|
||||
is a non-integer string (e.g. 'litellm_error'). Before the fix, int()
|
||||
raised ValueError and the error handler returned HTTP 500 instead of
|
||||
engaging the fallback chain.
|
||||
"""
|
||||
|
||||
class _FakeException(Exception):
|
||||
status_code = "litellm_error"
|
||||
|
||||
midstream_error = MidStreamFallbackError(
|
||||
message="stream broke",
|
||||
model="gpt-4o-mini",
|
||||
llm_provider="openai",
|
||||
original_exception=_FakeException("boom"),
|
||||
)
|
||||
|
||||
assert midstream_error.status_code == 503
|
||||
assert midstream_error.response.status_code == 503
|
||||
|
||||
|
||||
class TestProxyHeaderExtraction:
|
||||
"""Test that proxy correctly extracts headers from exceptions."""
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue