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

  1. Log the line and switch to a raw text read of the same request to see the true body shape
  2. Verify the endpoint actually returns NDJSON and each line is complete
  3. If lines can be long, confirm no proxy truncation; retry transient failures
  4. 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

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

Related errors


AI-assisted analysis of continuedev/continue@5522c6f44c (2026-08-27). Data as JSON: /api/errors/fd7f87700637570b. Report an issue: GitHub.