The fastest, litest AI Gateway. Rust core with Python SDK. Call 100+ LLM APIs in OpenAI (or native) format with cost tracking, guardrails, load balancing, and logging [Bedrock, Azure, OpenAI, Anthropic, OpenAI, VertexAI, vLLM, Nvidia NIM]
Python ai-gatewayanthropicazure-openaibedrockgatewaylangchainlitellmllmllm-gatewayllmopsmcp-gatewayopenaiopenai-proxyrustrust-aivertex-ai
fix(proxy): end failed responses streams with [DONE]
mateo-berrieb96d885ceff
diff --git a/litellm/proxy/_lazy_openapi_snapshot.json b/litellm/proxy/_lazy_openapi_snapshot.jsonindex 2aa2cf15ac1..8a8d08c6887 100644--- a/litellm/proxy/_lazy_openapi_snapshot.json+++ b/litellm/proxy/_lazy_openapi_snapshot.json@@ -19394,7 +19394,7 @@}}},- "description": "\nUnified rate-limit error.\n\nEvery rate-limit condition surfaced by litellm \u2014 whether it originated from\nan upstream LLM provider, a vendor batch endpoint, or one of litellm's own\nproxy-side limiters (parallel-requests, dynamic-rate, batch-rate, budget,\nmax-iterations, etc.) \u2014 is raised as an instance of this class.\n\nThe :attr:`category` attribute lets callers distinguish the source. See\n:class:`RateLimitErrorCategory` for the available values.\n"+ "description": "\n Unified rate-limit error.\n\n Every rate-limit condition surfaced by litellm \u2014 whether it originated from\n an upstream LLM provider, a vendor batch endpoint, or one of litellm's own\n proxy-side limiters (parallel-requests, dynamic-rate, batch-rate, budget,\n max-iterations, etc.) \u2014 is raised as an instance of this class.\n\n The :attr:`category` attribute lets callers distinguish the source. See\n :class:`RateLimitErrorCategory` for the available values.\n "},"500": {"content": {diff --git a/litellm/proxy/proxy_server.py b/litellm/proxy/proxy_server.pyindex 9cd233e5ad5..38a883a3b1b 100644--- a/litellm/proxy/proxy_server.py+++ b/litellm/proxy/proxy_server.py@@ -8865,6 +8865,7 @@ def _format_streaming_sse_chunk(chunk: str | bytes) -> str | bytes:_SSE_FRAME_DELIMITERS: Final = ("\r\n\r\n", "\n\n", "\r\r")+_OPENAI_STREAM_DONE_FRAME: Final = "data: [DONE]\n\n"_MAX_RAW_SSE_BUFFER_CHARS: Final = 8 * 1024 * 1024@@ -9272,8 +9273,7 @@ async def async_data_generator(yield error_message# OpenAI-compatible streams terminate with data: [DONE]; Google GenAI (?alt=sse) does not.if not request_data.get("_litellm_skip_openai_stream_done"):- done_message: Final = "[DONE]"- yield f"data: {done_message}\n\n"+ yield _OPENAI_STREAM_DONE_FRAMEexcept (asyncio.CancelledError, GeneratorExit):# Client disconnected mid-stream. CancelledError / GeneratorExit are# BaseException, so they bypass the success/failure logging callbacks@@ -9303,6 +9303,8 @@ async def async_data_generator(error_frame: Final = error_state.format_failure(e)if error_frame is not None:yield error_frame+ if not request_data.get("_litellm_skip_openai_stream_done"):+ yield _OPENAI_STREAM_DONE_FRAMEreturnif isinstance(e, HTTPException):raise ediff --git a/tests/test_litellm/proxy/proxy_server/test_streaming_helpers.py b/tests/test_litellm/proxy/proxy_server/test_streaming_helpers.pyindex 9adc2b80741..86dd356e5f5 100644--- a/tests/test_litellm/proxy/proxy_server/test_streaming_helpers.py+++ b/tests/test_litellm/proxy/proxy_server/test_streaming_helpers.py@@ -995,6 +995,8 @@ async def test_responses_stream_keeps_tool_deltas_and_only_emits_a_valid_terminafor frame in event_frames)+ assert decoded[-1] == "data: [DONE]\n\n"+ assert len(decoded) == len(event_frames) + 1assert payloads[0]["response"]["id"] == "resp_visible"assert payloads[1] == tool_delta.model_dump()assert len(payloads) == 3diff --git a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.pyindex 1fb3fef8fb3..63faef2366f 100644--- a/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py+++ b/tests/test_litellm/proxy/response_api_endpoints/test_endpoints.py@@ -95,7 +95,8 @@ async def test_streaming_upstream_errors_keep_the_client_protocol(assert result.status_code == 200, result.textassert message in result.textif path == "/v1/responses":- assert frames[-1].startswith("event: response.failed\n"), result.text+ assert frames[-1] == "data: [DONE]", result.text+ assert frames[-2].startswith("event: response.failed\n"), result.textif partial:assert [event["type"] for event in events] == ["response.created", "response.output_item.added",