ReqLLM.Streaming.SSE (ReqLLM v1.13.0)

View Source

Provider-agnostic Server-Sent Events (SSE) parsing utilities.

The streaming HTTP path feeds Finch chunks into accumulate_and_parse/2, which returns parsed SSE events and an opaque parser state for the next chunk. Complete enumerable bodies can use parse_sse_stream/1 or parse_sse_binary/1.

Summary

Functions

Parse a binary chunk and return complete SSE events with parser state.

Flush a parser state by supplying a terminating blank line.

Parse SSE events from a complete binary string.

Parse SSE events from a stream of binary chunks with boundary handling.

Process a raw SSE event, attempting JSON decode of its data field.

Types

parser_state()

@type parser_state() ::
  %ServerSentEvents.Parser{
    event: term(),
    key: term(),
    phase: term(),
    value: term()
  }
  | nil

Functions

accumulate_and_parse(chunk, state)

@spec accumulate_and_parse(binary(), parser_state() | binary()) ::
  {[map()], parser_state()}

Parse a binary chunk and return complete SSE events with parser state.

flush(state)

@spec flush(parser_state() | term()) :: {[map()], parser_state() | term()}

Flush a parser state by supplying a terminating blank line.

parse_sse_binary(binary)

@spec parse_sse_binary(binary()) :: [map()]

Parse SSE events from a complete binary string.

parse_sse_stream(stream)

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

Parse SSE events from a stream of binary chunks with boundary handling.

process_sse_event(event)

@spec process_sse_event(map()) :: map() | nil

Process a raw SSE event, attempting JSON decode of its data field.