ReqAnthropic.SSE (ReqAnthropic v0.2.1)

Copy Markdown View Source

Minimal Server-Sent Events parser used by streaming endpoints.

The Anthropic streaming protocol emits events of the form:

event: <name>
data: <json>

separated by blank lines. parse_stream/1 takes any enumerable of binary chunks (most commonly a Req.Response.Async) and returns a Stream that yields parsed event maps. The :type key is taken from the JSON data payload (Anthropic always includes it). Cross-chunk event boundaries are handled by buffering between elements.

Lines of any other shape (id:, retry:, : comments) are ignored. Multi-line data: payloads are concatenated with \n per the SSE spec.

Summary

Functions

Parse a stream of binary chunks into a stream of decoded SSE events.

Functions

parse_stream(chunks)

@spec parse_stream(Enumerable.t()) :: Enumerable.t()

Parse a stream of binary chunks into a stream of decoded SSE events.

Each yielded element is a map decoded from the event's data: field, or {:error, %ReqAnthropic.Error{}} if a data: payload fails to decode. Events without a data: line (e.g. bare event: ping keep-alives) are yielded as %{"type" => name} so callers can still see them.