NDJSON (Newline-Delimited JSON) stream parser.
Purpose
-------
Reusable utility for parsing chunked NDJSON HTTP responses into a stream
of dicts. Used by :class:`providers.ollama.client.OllamaClient` and
reusable by future providers (LM Studio, vLLM).
Responsibilities
----------------
- :func:`parse_ndjson_stream` — consume an arbitrary-chunk-size byte
iterator, accumulate a line buffer, and yield one dict per complete
JSON line. Handles: chunks split across JSON boundaries, blank lines,
malformed JSON lines, and final un-terminated lines.
Diagnostics
-----------
Domain: OC_CHAT.CLIENT
Levels:
L2 — malformed JSON line logged and skipped
L3 — semantic details
L4 — deep tracing
Contracts
---------
- Input: any ``Iterator[bytes]`` — HTTP response body iterated in
arbitrary chunk sizes.
- Output: ``Iterator[dict]`` — one dict per valid JSON line.
- A ``json.JSONDecodeError`` on any single line is logged at L2 then
skipped; the remaining stream continues normally.
- An empty iterator yields nothing.
- A final chunk without a trailing ``\\n`` is flushed after the iterator
is exhausted.
Dependencies
------------
- oc_diagnostics (structured debug logging via _dbg)
| Iterator[dict] providers.ollama.ndjson.parse_ndjson_stream |
( |
Iterator[bytes] | byte_iter | ) |
|
Parse a chunked NDJSON HTTP response body into a stream of dicts.
Each element of *byte_iter* may contain zero, one, or multiple
``\\n``-terminated JSON lines, or a partial line that continues in
the next element.
Args:
byte_iter: Raw HTTP body chunk iterator (e.g. an iterated
``http.client.HTTPResponse``).
Yields:
Parsed dicts, one per complete, valid JSON line.
Definition at line 52 of file ndjson.py.