From d707105053642d6ae005c68b616bc1a5581c2d84 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sebasti=C3=A1n=20Ram=C3=ADrez?= Date: Sun, 1 Mar 2026 09:16:15 +0100 Subject: [PATCH] =?UTF-8?q?=E2=99=BB=EF=B8=8F=20Refactor=20ServerSentEvent?= =?UTF-8?q?=20to=20use=20id:=20str?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- fastapi/sse.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/fastapi/sse.py b/fastapi/sse.py index bd14713617..901d824964 100644 --- a/fastapi/sse.py +++ b/fastapi/sse.py @@ -33,8 +33,8 @@ class EventSourceResponse(StreamingResponse): media_type = "text/event-stream" -def _check_id_no_null(v: int | str | None) -> int | str | None: - if v is not None and "\0" in str(v): +def _check_id_no_null(v: str | None) -> str | None: + if v is not None and "\0" in v: raise ValueError("SSE 'id' must not contain null characters") return v @@ -96,7 +96,7 @@ class ServerSentEvent(BaseModel): ), ] = None id: Annotated[ - int | str | None, + str | None, AfterValidator(_check_id_no_null), Doc( """ @@ -162,7 +162,7 @@ def format_sse_event( ), ] = None, id: Annotated[ - int | str | None, + str | None, Doc( """ Optional event ID (`id:` field).