Nous.Providers.HTTP.JSONArrayParser (nous v0.13.3)

View Source

Stream parser for JSON array responses.

Parses streaming HTTP responses where the body is a JSON array of objects:

[{"candidates":[...]},{"candidates":[...]},...]

Used by providers (like Gemini) that stream responses as a JSON array rather than Server-Sent Events. Has the same interface as Nous.Providers.HTTP.parse_sse_buffer/1 so it can be used as a drop-in :stream_parser for HTTP.stream/4.

How it works

Chunks arrive at arbitrary byte boundaries. The parser accumulates them in a buffer, skips array-level syntax ([, ], ,, whitespace), and extracts complete top-level JSON objects by tracking {} nesting depth while respecting string literals and escape sequences.

Summary

Functions

Parse a buffer containing chunks of a JSON array into individual events.

Functions

parse_buffer(buffer)

@spec parse_buffer(String.t()) :: {list(), String.t()}

Parse a buffer containing chunks of a JSON array into individual events.

Returns {events, remaining_buffer} where events is a list of parsed JSON maps (same contract as HTTP.parse_sse_buffer/1).

Examples

iex> parse_buffer(~s|[{"text":"hi"},{"text":"there"}]|)
{[%{"text" => "hi"}, %{"text" => "there"}], ""}

iex> parse_buffer(~s|[{"text":"hi"},{"tex|)
{[%{"text" => "hi"}], ~s|{"tex|}

iex> parse_buffer("")
{[], ""}