Add native server-sent events support via `client.sse()`
Merged
KL
4mo ago+529+529+012345678901234567890123456789-0-0-0123456789
Adds native server-sent events support, so the client has a first-class method for SSE instead of needing a companion library.
API
with httpx2.Client() as client:
with client.sse("http://localhost:8000/sse") as source:
for event in source:
print(event.event, event.data, event.json())
Client.sse()/AsyncClient.sse()- same name on both clients (likestream),url-first withmethod="GET"default. A context manager yielding anEventSource.EventSource- iterate it directly (for event in source/async for event in source) to getServerSentEventinstances;source.responseexposes the underlyingResponse.ServerSentEvent- frozen dataclass withevent,data,id,retry: int | None, and.json().SSEError(TransportError)- raised when the responseContent-Typeis nottext/event-stream.
The method sets Accept: text/event-stream and Cache-Control: no-store (user-supplied headers win), and is built on top of client.stream().
Notes
- Always-on, no extra and no third-party dependency - the parser is implemented over
response.iter_text(). - Uses a spec-compliant line splitter (
\r\n/\r/\nonly) rather thaniter_lines(), which over-splits on exotic Unicode separators that SSE treats as data. - New
tests/httpx2/test_sse.pycovers parsing, multi-line data, comments,id/retry/null handling, last-event-id persistence, content-type validation, header defaults/override, POST, and chunk-boundary/CRLF splitting (sync + async)._sse.pyis at 100% coverage.
AI Disclaimer
This PR was developed with the assistance of either Claude or Codex. I've reviewed and verified the changes.