ARTIFACT // SSE-STREAM-GUARD.MJS (JAVASCRIPT)
Resilient LLM SSE Stream Reader (Handles Post-HTTP-200 Mid-Stream Errors) (sse-stream-guard.mjs)
Detects mid-stream error frames after HTTP 200 OK, idle socket stalls, and premature finish_reason truncation.
[KEY VERIFIED TAKEAWAY / TL;DR]This reproducible configuration artifact "Resilient LLM SSE Stream Reader (Handles Post-HTTP-200 Mid-Stream Errors)" (sse-stream-guard.mjs, JAVASCRIPT) is paired with Tinker AI Lab's hardware-verified guide "sse-http200-stream-errors". It exposes 2 customizable browser variables (STREAM_API_URL=http://127.0.0.1:11434/v1/chat/completions, IDLE_TIMEOUT_MS=15000) with loopback-first security defaults.
04 // DIAGNOSISAPI & Deployment Troubleshooting· Verified: 2026-10-23
Browse All 42 Artifacts →sse-stream-guard.mjs
View Paired Guide →Resilient LLM SSE Stream Reader (Handles Post-HTTP-200 Mid-Stream Errors)
Detects mid-stream error frames after HTTP 200 OK, idle socket stalls, and premature finish_reason truncation.
Customizable Variables (Saved in Browser)
UTF-8 Encoded · Live Client-Side Compilation
// Tinker AI — Resilient SSE Stream Reader with Mid-Stream Error Detection
const API_URL = "http://127.0.0.1:11434/v1/chat/completions";
const IDLE_TIMEOUT_MS = Number("15000");
export async function consumeLlmStream(payload) {
const res = await fetch(API_URL, {
method: "POST",
headers: { "Content-Type": "application/json" },
body: JSON.stringify({ ...payload, stream: true }),
});
if (!res.ok || !res.body) {
throw new Error(`Pre-stream HTTP failure: ${res.status}`);
}
const reader = res.body.getReader();
const decoder = new TextDecoder();
let buffer = "";
let output = "";
while (true) {
const timer = setTimeout(() => reader.cancel("SSE chunk idle timeout"), IDLE_TIMEOUT_MS);
const { value, done } = await reader.read().finally(() => clearTimeout(timer));
if (done) break;
buffer += decoder.decode(value, { stream: true });
const lines = buffer.split("\n");
buffer = lines.pop() ?? "";
for (const raw of lines) {
const line = raw.trim();
if (!line.startsWith("data:")) continue;
const data = line.slice(5).trim();
if (data === "[DONE]") return { output, completed: true };
const event = JSON.parse(data);
// Catch mid-stream error frame sent AFTER HTTP 200 OK
if (event.error || event.type === "error") {
throw new Error(`Mid-stream SSE error after HTTP 200: ${ JSON.stringify(event.error || event) }`);
}
const delta = event.choices?.[0]?.delta?.content ?? "";
output += delta;
}
}
return { output, completed: false };
}
PARAMETER SPECIFICATION // 参数化变量规范说明表
Customizable Parameter Reference for sse-stream-guard.mjs
This JAVASCRIPT artifact exposes 2 template variables compiled client-side in your browser.
| Template Token | Parameter Description | Verified Default | Example Format |
|---|---|---|---|
{{STREAM_API_URL}} | Streaming Chat Endpoint | http://127.0.0.1:11434/v1/chat/completions | http://127.0.0.1:11434/v1/chat/completions |
{{IDLE_TIMEOUT_MS}} | Chunk Idle Timeout (ms) | 15000 | 15000 |
PAIRED HARDWARE MANIFEST // 配套实测环境与边界记录
Verified Environment for "Post-HTTP-200 SSE Stream Errors: Failure Conditions & Recovery Boundaries"
Tested Hardware & OS
Node.js 22.13+ SSE Client · Applicable to Claude API, OpenAI API, and Local Ollama / vLLM
Pinned Stack & Model Versions
Node.js 22.13+Anthropic Messages SSEOpenAI Chat Completions SSE
Offline Boundary (Applies to Both Local & Cloud Streaming APIs)
Includes local zero-cost SSE guard client to test your parser against mid-stream error events.
Known Operational Limits & Failure Modes
- As documented by Claude & OpenAI, once HTTP 200 OK headers are flushed to open an SSE stream, an error occurring mid-generation CANNOT change the HTTP status code.
- Retrying a partially streamed response that already triggered external side effects without an idempotency key causes duplicate writes.
DEPLOYMENT CHECKLIST // 配置落地核验步骤
3-Step Verification Checklist After Deploying sse-stream-guard.mjs
- Verified SSE client inspects every parsed chunk for `event.error` before appending output.
- Verified client requires explicit `[DONE]` and `finish_reason === "stop"` before saving structured output.
- Configured per-chunk idle timeout (`reader.cancel`) to recover from stalled proxy connections.