stream_struct library
Turn a language model's token stream into a stream of the structured object as it fills in.
parsePartialJson decodes a truncated JSON buffer into the value it holds so far. streamPartialJson and streamPartialJsonFrom turn a delta stream into a stream of growing values, and streamPartial maps those into a typed object. openAiDelta, anthropicDelta, and geminiDelta pull the text fragment out of each provider's streamed chunk.
sseJson decodes the Server-Sent Events body those providers actually send, so an HTTP response can go straight through without hand-written line handling; sseData gives the raw payloads if you want to decode them yourself.
Constants
- sseDoneSentinel → const String
- Decoding for Server-Sent Events, the wire format every streaming LLM API uses.
Functions
-
anthropicDelta(
Map< String, dynamic> chunk) → String? -
Anthropic Messages stream for tool-based structured output: the JSON of
a forced tool call, which arrives as
delta.partial_jsonon the tool block'scontent_block_deltaevents. -
anthropicTextDelta(
Map< String, dynamic> chunk) → String? -
Anthropic Messages stream for plain-text output: the model's text block,
which arrives as
delta.textoncontent_block_deltaevents. -
geminiDelta(
Map< String, dynamic> chunk) → String? -
Gemini
generateContentstream: the answer text fromcandidates[0].content.parts. -
openAiDelta(
Map< String, dynamic> chunk) → String? -
OpenAI chat completions stream:
choices[0].delta.content. -
parsePartialJson(
String buffer) → Object? -
Decodes a possibly-truncated JSON
bufferinto the value it represents so far. -
sseData(
Stream< List< bytes) → Stream<int> >String> -
Decodes an SSE response body into the
datapayload of each event. -
sseDataFromLines(
Stream< String> lines) → Stream<String> - Like sseData, but for a stream that is already split into lines.
-
sseJson(
Stream< List< bytes) → Stream<int> >Map< String, dynamic> > - Decodes an SSE response body into one JSON object per event, which is what streamPartialJson's callers actually want.
-
sseJsonFromData(
Stream< String> payloads) → Stream<Map< String, dynamic> > - Like sseJson, but starting from payloads you already decoded with sseData or sseDataFromLines.
-
streamPartial<
T> (Stream< String> deltas, T build(Map<String, dynamic> partial)) → Stream<T> -
Maps each partial JSON object through
buildto yield a typed value as the object fills in. -
streamPartialFrom<
T> (Stream< Map< chunks, DeltaExtractor extractor, T build(Map<String, dynamic> >String, dynamic> partial)) → Stream<T> -
streamPartial over a provider's decoded chunks: pulls each text fragment
out with
extractor, then maps the growing object throughbuild. -
streamPartialJson(
Stream< String> deltas) → Stream<Object?> -
Accumulates a stream of text
deltasand, after each one, emits the JSON value parsed so far. -
streamPartialJsonFrom(
Stream< Map< chunks, DeltaExtractor extractor) → Stream<String, dynamic> >Object?> -
Like streamPartialJson but takes provider
chunksand a DeltaExtractor, so you can pipe a decoded SSE stream straight in:
Typedefs
-
DeltaExtractor
= String? Function(Map<
String, dynamic> chunk) - Pulls the incremental text out of one streamed chunk from a provider.