API reference

StreamingParser Class

Shared utilities for parsing streaming responses from various LLM providers.
Handles SSE (Server-Sent Events) and newline-delimited JSON formats. Maintains internal buffers to handle partial chunks correctly.

Static Methods

ClearBuffers ( streamId )
Clears the buffer for a specific stream.
Call this when a stream is cancelled or encounters an error.
Signature
static void ClearBuffers(int streamId=0)
Parameters
streamId: int  Stream identifier to clear
Returns
void
ParseJson< T > ( json )
Helper to extract a field from JSON using JsonUtility with a typed class.
Signature
static T ParseJson< T >(string json)
Parameters
json: string
Returns
T  The parsed object, or null if parsing fails
ParseNewlineJson ( chunk , extractor , streamId , isFinalChunk )
Parses newline-delimited JSON chunks and extracts text using the provided extractor.
Handles partial chunks by maintaining a buffer per stream context.
Signature
static List< string > ParseNewlineJson(string chunk, Func< string, string > extractor, int streamId=0, bool isFinalChunk=false)
Parameters
chunk: string  Raw chunk data containing newline-separated JSON objects
extractor: Func< string, string >  Function to extract text from a JSON line
streamId: int  Optional stream identifier for buffer isolation (defaults to 0)
isFinalChunk: bool  Set to true on the last chunk to flush remaining buffer
Returns
List< string >  List of extracted text tokens
ParseSse ( chunk , extractor , streamId , isFinalChunk )
Parses Server-Sent Events (SSE) formatted chunks and extracts text deltas.
Handles partial chunks by maintaining a buffer per stream context.
Signature
static List< string > ParseSse(string chunk, Func< string, string > extractor, int streamId=0, bool isFinalChunk=false)
Parameters
chunk: string  Raw chunk data from HTTP stream
extractor: Func< string, string >  Function to extract text from a JSON data line
streamId: int  Optional stream identifier for buffer isolation (defaults to 0)
isFinalChunk: bool  Set to true on the last chunk to flush remaining buffer
Returns
List< string >  List of extracted text tokens
TryParseJson< T > ( json , result )
Tries to extract a field from JSON using JsonUtility with a typed class.
Signature
static bool TryParseJson< T >(string json, out T result)
Parameters
json: string  The JSON string to parse
result: out T  The parsed object if successful, null otherwise
Returns
bool  True if parsing succeeded, false otherwise