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(passthrough): recognize CR-only SSE frame delimiters when minting streamed managed ids
mateo-berri6a9662a5a886
diff --git a/litellm/proxy/common_utils/sse_keepalive.py b/litellm/proxy/common_utils/sse_keepalive.pyindex e3cebf9f6c6..26fccf8ee82 100644--- a/litellm/proxy/common_utils/sse_keepalive.py+++ b/litellm/proxy/common_utils/sse_keepalive.py@@ -93,10 +93,9 @@ def is_sse_content_type(content_type: str | None) -> bool:def split_complete_sse_frames(pending: bytes) -> tuple[bytes, bytes]:"""Split buffered SSE bytes into ``(complete_frames, unterminated_tail)``."""- lf_boundary_end: Final = pending.rfind(b"\n\n") + 2- crlf_boundary_end: Final = pending.rfind(b"\r\n\r\n") + 4boundary_end: Final = max(- lf_boundary_end if lf_boundary_end >= 2 else 0, crlf_boundary_end if crlf_boundary_end >= 4 else 0+ (pending.rfind(delimiter) + len(delimiter) for delimiter in _SSE_FRAME_DELIMITERS if delimiter in pending),+ default=0,)if boundary_end == 0:return b"", pendingdiff --git a/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py b/litellm/proxy/pass_through_endpoints/managed_id_rewriter.pyindex bdc4c515803..23cfef6576c 100644--- a/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py+++ b/litellm/proxy/pass_through_endpoints/managed_id_rewriter.py@@ -821,10 +821,6 @@ async def rewrite_response_ids(return mutated if changed else body-# ----------------------------------------------------------------------------# OUTPUT path — streamed Responses API bodies-# ----------------------------------------------------------------------------_RESPONSE_ID_PREFIX: Final = "resp_"_STREAMED_RESPONSE_ID_SPEC: Final[_FieldSpec] = ("id", _RESPONSE_ID_PREFIX)_SSE_DATA_PREFIX: Final = "data:"diff --git a/tests/test_litellm/proxy/common_utils/test_sse_keepalive.py b/tests/test_litellm/proxy/common_utils/test_sse_keepalive.pyindex 89ae74920fe..69b92f5e4d7 100644--- a/tests/test_litellm/proxy/common_utils/test_sse_keepalive.py+++ b/tests/test_litellm/proxy/common_utils/test_sse_keepalive.py@@ -10,6 +10,7 @@ from litellm.proxy.common_utils.sse_keepalive import (ANTHROPIC_PING_SSE_CHUNK,SSE_COMMENT_PING_BYTES,resolve_ttft_keepalive_interval,+ split_complete_sse_frames,wrap_passthrough_sse_bytes_with_keepalive_pings,wrap_sse_stream_with_keepalive_pings,)@@ -18,6 +19,19 @@ MESSAGE_START_CHUNK: Final = 'data: {"type": "message_start"}\n\n'TEXT_DELTA_CHUNK: Final = 'data: {"type": "content_block_delta"}\n\n'[email protected]("delimiter", [b"\n\n", b"\r\n\r\n", b"\r\r"])+def test_split_complete_sse_frames_recognizes_every_sse_frame_delimiter(delimiter: bytes):+ newline: Final = delimiter[: len(delimiter) // 2]+ frame: Final = b"event: response.created" + newline + b"data: {}" + delimiter+ tail: Final = b"data: partial"++ assert split_complete_sse_frames(frame + tail) == (frame, tail)+++def test_split_complete_sse_frames_holds_bytes_with_no_complete_frame():+ assert split_complete_sse_frames(b"data: unterminated") == (b"", b"data: unterminated")++@pytest.mark.asyncioasync def test_pings_fill_mid_stream_silence_and_preserve_chunk_order():async def gappy_stream() -> AsyncGenerator[str, None]:diff --git a/tests/test_litellm/proxy/pass_through_endpoints/test_managed_id_rewriter.py b/tests/test_litellm/proxy/pass_through_endpoints/test_managed_id_rewriter.pyindex da4ccff7008..dc8c49b93d8 100644--- a/tests/test_litellm/proxy/pass_through_endpoints/test_managed_id_rewriter.py+++ b/tests/test_litellm/proxy/pass_through_endpoints/test_managed_id_rewriter.py@@ -211,6 +211,29 @@ async def test_streamed_response_is_owned_and_rewritten_across_chunk_boundaries(assert output == _response_stream_bytes(managed_id)+async def test_streamed_response_with_cr_only_frame_delimiters_is_still_owned_and_rewritten():+ """SSE also terminates lines with a lone CR; those frames must mint and rewrite too."""+ pc = _prisma_client()+ payload = _response_stream_bytes().replace(b"\n", b"\r")++ output = await _collect(+ rewrite_streamed_response_ids(+ stream=_chunks(payload, 7),+ provider="openai",+ method="POST",+ route="/openai_passthrough/v1/responses",+ user_api_key_dict=_user(),+ prisma_client=pc,+ )+ )++ pc.db.litellm_managedobjecttable.upsert.assert_awaited_once()+ managed_id = pc.db.litellm_managedobjecttable.upsert.await_args.kwargs["data"]["create"]["unified_object_id"]+ assert RAW_RESPONSE_ID.encode() not in output+ assert output == _response_stream_bytes(managed_id).replace(b"\n", b"\r")++@pytest.mark.asyncioasync def test_streamed_bytes_untouched_on_routes_without_a_response_id():pc = _prisma_client()