From f4872232e5876f8742be87019255908172be1e3c Mon Sep 17 00:00:00 2001 From: t0ugh-sys <2293086082@qq.com> Date: Wed, 21 Oct 2026 14:39:40 +0800 Subject: [PATCH 1/2] fix(proxy): default invalid streaming error codes to 500 --- litellm/proxy/common_request_processing.py | 4 ++-- tests/unit/proxy/test_common_request_processing.py | 8 ++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/litellm/proxy/common_request_processing.py b/litellm/proxy/common_request_processing.py index 6fda5780f63..e66bd97271b 100644 --- a/litellm/proxy/common_request_processing.py +++ b/litellm/proxy/common_request_processing.py @@ -910,8 +910,8 @@ async def _parse_event_data_for_error(event_line: str | bytes) -> int | None: # Ensure error_code is a valid HTTP status code if error_code is not None and 100 <= error_code <= 599: return error_code - elif error_code_raw is not None: # Log if original code was present but not valid - verbose_proxy_logger.warning("Error has invalid or non-convertible code: %s", error_code_raw) + verbose_proxy_logger.warning("Error has missing or invalid HTTP status code: %s", error_code_raw) + return status.HTTP_500_INTERNAL_SERVER_ERROR except (orjson.JSONDecodeError, json.JSONDecodeError): # not a known error chunk pass diff --git a/tests/unit/proxy/test_common_request_processing.py b/tests/unit/proxy/test_common_request_processing.py index a226ce8a4a9..ec34f32dbdb 100644 --- a/tests/unit/proxy/test_common_request_processing.py +++ b/tests/unit/proxy/test_common_request_processing.py @@ -98,6 +98,14 @@ def test_attach_guardrail_information_copies_recorded_entries_onto_model_respons assert "guardrail_information" not in response.model_dump() +@pytest.mark.asyncio +@pytest.mark.parametrize("error", [{"code": None}, {}, {"code": "not-a-status"}]) +async def test_parse_event_data_for_error_defaults_invalid_code_to_internal_error(error): + event = f"data: {json.dumps({'error': error})}\n\n" + + assert await _parse_event_data_for_error(event) == status.HTTP_500_INTERNAL_SERVER_ERROR + + def test_attach_guardrail_information_reports_empty_list_when_nothing_ran(): response = litellm.ModelResponse() From 4c94a03ff71afecc2f0d83b38ff0c9c090806985 Mon Sep 17 00:00:00 2001 From: t0ugh-sys <2293086082@qq.com> Date: Wed, 21 Oct 2026 14:58:48 +0800 Subject: [PATCH 2/2] test(proxy): update invalid streaming error expectations --- tests/unit/proxy/test_common_request_processing.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/unit/proxy/test_common_request_processing.py b/tests/unit/proxy/test_common_request_processing.py index ec34f32dbdb..2745fbfacdb 100644 --- a/tests/unit/proxy/test_common_request_processing.py +++ b/tests/unit/proxy/test_common_request_processing.py @@ -2111,15 +2111,15 @@ class TestCommonRequestProcessingHelpers: ), # Valid string-integer code ( 'data: {"error": {"code": "invalid_code", "message": "error"}}', - None, + 500, ), # Invalid string code ( 'data: {"error": {"code": 99, "message": "too low"}}', - None, + 500, ), # Integer code too low ( 'data: {"error": {"code": 600, "message": "too high"}}', - None, + 500, ), # Integer code too high ( 'data: {"id": "123", "content": "hello"}', @@ -2134,11 +2134,11 @@ class TestCommonRequestProcessingHelpers: ("id: 123", None), # Non-SSE event line ( 'data: {"error": {"message": "some error"}}', - None, + 500, ), # Error event without 'code' field ( 'data: {"error": {"code": null, "message": "code is null"}}', - None, + 500, ), # Error with null code ], )