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).