continuedev/continue · error · Error
Malformed JSON sent from server: ${line}
Error message
Malformed JSON sent from server: ${line} What it means
streamJSON parses newline-delimited JSON directly from the byte stream; when a newline-terminated line fails JSON.parse, it throws with the offending line.
Source
Thrown at packages/fetch/src/stream.ts:164
if (!done && data) {
yield data;
}
}
}
export async function* streamJSON(response: Response): AsyncGenerator<any> {
let buffer = "";
for await (const value of streamResponse(response)) {
buffer += value;
let position;
while ((position = buffer.indexOf("\n")) >= 0) {
const line = buffer.slice(0, position);
try {
const data = JSON.parse(line);
yield data;
} catch (e) {
throw new Error(`Malformed JSON sent from server: ${line}`);
}
buffer = buffer.slice(position + 1);
}
}
}
View on GitHub (pinned to 5522c6f44c)
Solutions
- Log the line and switch to a raw text read of the same request to see the true body shape
- Verify the endpoint actually returns NDJSON and each line is complete
- If lines can be long, confirm no proxy truncation; retry transient failures
- Use streamSse instead if the endpoint is SSE rather than NDJSON
Defensive patterns
Strategy: try-catch
Validate before calling
if (response.headers.get('content-type')?.includes('text/event-stream')) useSse(); else useNdjson(); Try / catch
try { for await (const d of streamJSON(response)) {} } catch (e) { if (/^Malformed JSON/.test(e.message)) { const raw = await response.text(); /* inspect actual body */ } throw e; } Prevention
- Confirm the endpoint returns newline-delimited JSON, not SSE or text
- Read the raw body once on failure to diagnose the true format
- Prefer streamSse for event-stream endpoints
When it happens
Trigger: Using streamJSON (e.g. chatCompletionStream in NDJSON mode) where a line in the response body isn't valid JSON — error text, HTML, or truncated output.
Common situations: Endpoints that return plain-text errors with 200, chunked responses split incorrectly, or debug logging interleaved into the body.
Understand the failure class
- Parsing and encoding errors: unexpected token, malformed input — why parsers reject input and how to find the real culprit.
Related errors
- Malformed JSON sent from server: ${json}
- Malformed JSON received from Bedrock: ${decoded}
- non-200 response body: ${await response.text()}
- No response body returned.
- Stream was closed before any data was received. Try again. (
AI-assisted analysis of continuedev/continue@5522c6f44c (2026-08-27).
Data as JSON: /api/errors/fd7f87700637570b.
Report an issue: GitHub.