GenAI.StreamHandler.Accumulate (GenAI Core v0.3.3)

Copy Markdown

Streaming handler that decodes provider SSE streams, forwards each normalized event live to a sink, and accumulates the final GenAI.ChatCompletion (available as state.completion when the stream ends).

Use via:

thread
|> GenAI.with_stream_handler(%GenAI.StreamHandler.Accumulate{sink: self()})
# ...
{:ok, {acc, _session}} = GenAI.stream(thread, context)

# in the sink process:
#   handle_info({:genai, {:text, chunk}}, s) — one per content delta
#   acc.completion — final %GenAI.ChatCompletion{}

sink may be a pid (events delivered as {:genai, event} messages) or a 1-arity fun (called with each event). Omit the sink to only accumulate.

Summary

Functions

begin_stream(handler, session, context, options)

Callback implementation for GenAI.StreamHandler.Behaviour.begin_stream/4.

handle_event(event, state)

Callback implementation for GenAI.StreamHandler.Behaviour.handle_event/2.