From 4690dfaed918b46f7a5be9a9da609d6849b2b7dd Mon Sep 17 00:00:00 2001 From: Sujit Patil Date: Tue, 26 May 2026 11:04:49 +0530 Subject: [PATCH] Fix line splitting in format_sse_event to comply with SSE spec Resolves issue where string.splitlines() incorrectly splits on non-LF/CRLF characters, which broke the Server-Sent Events stream specification. --- fastapi/sse.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fastapi/sse.py b/fastapi/sse.py index 1e2bd86171..bcd0ab5e8d 100644 --- a/fastapi/sse.py +++ b/fastapi/sse.py @@ -206,14 +206,14 @@ def format_sse_event( lines: list[str] = [] if comment is not None: - for line in comment.splitlines(): + for line in comment.replace("\r\n", "\n").replace("\r", "\n").split("\n"): lines.append(f": {line}") if event is not None: lines.append(f"event: {event}") if data_str is not None: - for line in data_str.splitlines(): + for line in data_str.replace("\r\n", "\n").replace("\r", "\n").split("\n"): lines.append(f"data: {line}") if id is not None: