ReqLLM.Response.Stream (ReqLLM v1.0.0)

View Source

Stream processing utilities for ReqLLM responses.

This module contains helper functions for working with streaming responses, particularly for joining stream chunks into complete responses.

Summary

Functions

Join a stream of chunks into a complete response.

Functions

join(stream, response)

@spec join(Enumerable.t(), ReqLLM.Response.t()) ::
  {:ok, ReqLLM.Response.t()} | {:error, term()}

Join a stream of chunks into a complete response.

This function consumes the entire stream, builds the complete message from content chunks, and returns a new response with the stream consumed and message populated.

Implementation Notes

The joining process involves several steps:

  1. Collect all stream chunks by consuming the enumerable
  2. Filter and concatenate content chunks to build the response text
  3. Extract final usage statistics from meta chunks, merging with existing usage
  4. Build a complete assistant message with the concatenated text content
  5. Return an updated response with materialized data and stream cleared

Parameters

  • stream - The stream enumerable containing stream chunks
  • response - The original response to update with materialized data

Returns

  • {:ok, updated_response} on success
  • {:error, %ReqLLM.Error.API.Stream{}} on stream processing failure