Server-sent event streaming with Meridian
Real-time token-by-token delivery over SSE. Meridian handles framing, reconnection, and parsing — you handle the response.
Quick start
Set stream: true on any chat completion call. Meridian returns aReadableStream of parsed chunks.
const stream = await meridian.chat.completions.create({
model: "meridian-4",
messages: [{ role: "user", content: "Explain SSE" }],
stream: true,
});
for await (const chunk of stream) {
process.stdout.write(chunk.choices[0]?.delta?.content ?? "");
}Wire protocol
Meridian streams conform to the text/event-stream MIME type. Each chunk is a data: line containing a JSON fragment. The stream terminates with data: [DONE].
GET /v1/chat/completions
Accept: text/event-stream
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":"SSE"}}]}
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":" is"}}]}
data: {"id":"chatcmpl-...","choices":[{"delta":{"content":" simple"}}]}
data: [DONE]Parsing data: lines
If you consume the raw response, split on newlines and strip the data: prefix. Empty lines are heartbeats — ignore them. The final [DONE] sentinel signals stream end.
const decoder = new TextDecoder();
let buffer = "";
for await (const chunk of response.body) {
buffer += decoder.decode(chunk, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() ?? "";
for (const line of lines) {
const trimmed = line.trim();
if (!trimmed || !trimmed.startsWith("data: ")) continue;
const payload = trimmed.slice(6);
if (payload === "[DONE]") return;
const json = JSON.parse(payload);
console.log(json.choices[0]?.delta?.content);
}
}OpenAI SDK auto-handling
Meridian is a drop-in replacement. The official openai Python and Node SDKs handle SSE parsing, reconnection, and error propagation automatically when stream: true is set. Point the base URL at Meridian and stream as usual.