{"record":{"id":"fd7f87700637570b","repo":"continuedev/continue","slug":"malformed-json-sent-from-server-line","errorCode":null,"errorMessage":"Malformed JSON sent from server: ${line}","messagePattern":"Malformed JSON sent from server: (.+?)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/fetch/src/stream.ts","lineNumber":164,"sourceCode":"    if (!done && data) {\n      yield data;\n    }\n  }\n}\n\nexport async function* streamJSON(response: Response): AsyncGenerator<any> {\n  let buffer = \"\";\n  for await (const value of streamResponse(response)) {\n    buffer += value;\n\n    let position;\n    while ((position = buffer.indexOf(\"\\n\")) >= 0) {\n      const line = buffer.slice(0, position);\n      try {\n        const data = JSON.parse(line);\n        yield data;\n      } catch (e) {\n        throw new Error(`Malformed JSON sent from server: ${line}`);\n      }\n      buffer = buffer.slice(position + 1);\n    }\n  }\n}\n","sourceCodeStart":146,"sourceCodeEnd":170,"githubUrl":"https://github.com/continuedev/continue/blob/5522c6f44ca0ac3528b37244818fbfa39b5af470/packages/fetch/src/stream.ts#L146-L170","documentation":"streamJSON parses newline-delimited JSON directly from the byte stream; when a newline-terminated line fails JSON.parse, it throws with the offending line.","triggerScenarios":"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.","commonSituations":"Endpoints that return plain-text errors with 200, chunked responses split incorrectly, or debug logging interleaved into the body.","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"],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":"if (response.headers.get('content-type')?.includes('text/event-stream')) useSse(); else useNdjson();","typeGuard":null,"tryCatchPattern":"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; }","preventionTips":["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"],"tags":["fetch","streaming","ndjson","json","malformed-response"],"backgroundTag":"malformed-stream-json","analyzedSha":"5522c6f44ca0ac3528b37244818fbfa39b5af470","analyzedAt":"2026-08-27T11:28:54.683Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}