sse-llm-parser
v1.0.0
Published
Zero-dependency parser for streaming LLM responses (Server-Sent Events / data: ... [DONE]) — robust to chunk boundaries, with text-delta helpers for OpenAI/Anthropic-style APIs.
Maintainers
Readme
sse-llm-parser
Zero-dependency parser for streaming LLM responses — the Server-Sent Events
format (data: {…}\n\n … data: [DONE]) used by OpenAI- and Anthropic-style
APIs. Robust to arbitrary chunk boundaries (events split mid-stream are buffered
and reassembled), with helpers to pull the incremental text out of each event.
Install
npm install sse-llm-parserUse
const { LlmSseParser, textDelta } = require('sse-llm-parser');
const parser = new LlmSseParser();
for await (const chunk of responseStream) { // chunk: string
for (const event of parser.push(chunk)) {
if (event.done) break; // data: [DONE]
process.stdout.write(textDelta(event)); // incremental text
}
}
for (const event of parser.flush()) { /* trailing event, if any */ }parser.push(chunk)→ array of completed{ data, raw, done }events.parser.flush()→ any trailing buffered event at end of stream.textDelta(event)→ incremental text for OpenAI (choices[0].delta.content) and Anthropic (content_block_delta.delta.text) shapes;''otherwise.
Handles multi-line data: fields, \r\n, non-JSON payloads (returned as-is) and the [DONE] sentinel.
More AI-coding & privacy guides
alexi.sh — independent guides on AI coding tools, LLMs and developer privacy.
License
MIT
