{"record":{"id":"9378bbedc16c9ce6","repo":"continuedev/continue","slug":"the-response-was-cancelled-mid-stream-try-again","errorCode":null,"errorMessage":"The response was cancelled mid-stream. Try again. (Premature Close).","messagePattern":"The response was cancelled mid-stream\\. Try again\\. \\(Premature Close\\)\\.","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/fetch/src/stream.ts","lineNumber":65,"sourceCode":"      }\n    }\n  } catch (e) {\n    if (e instanceof Error) {\n      if (e.name.startsWith(\"AbortError\")) {\n        return; // In case of client-side cancellation, just return\n      }\n      if (e.message.toLowerCase().includes(\"premature close\")) {\n        // Premature close can happen for various reasons, including:\n        // - Malformed chunks of data received from the server\n        // - The server closed the connection before sending the complete response\n        // - Long delays from the server during streaming\n        // - 'Keep alive' header being used in combination with an http agent and a set, low number of maxSockets\n        if (chunks === 0) {\n          throw new Error(\n            \"Stream was closed before any data was received. Try again. (Premature Close)\",\n          );\n        } else {\n          throw new Error(\n            \"The response was cancelled mid-stream. Try again. (Premature Close).\",\n          );\n        }\n      }\n    }\n    throw e;\n  }\n}\n\n// Export for testing purposes\nexport function parseDataLine(line: string): any {\n  const json = line.startsWith(\"data: \")\n    ? line.slice(\"data: \".length)\n    : line.slice(\"data:\".length);\n\n  try {\n    const data = JSON.parse(json);\n    if (data.error) {","sourceCodeStart":47,"sourceCodeEnd":83,"githubUrl":"https://github.com/continuedev/continue/blob/5522c6f44ca0ac3528b37244818fbfa39b5af470/packages/fetch/src/stream.ts#L47-L83","documentation":"The stream aborted after at least one chunk (chunks > 0), i.e. the response was cancelled mid-stream. Partial data was received but the response is incomplete, so the error tells you to retry the whole request.","triggerScenarios":"Server/proxy drops the connection partway through SSE/JSON streaming: timeouts, crashes mid-generation, or connection resets.","commonSituations":"Long LLM generations cut off by LB idle timeouts (no bytes while model thinks), flaky networks, or servers crashing mid-response.","solutions":["Retry the full request with backoff; discard partial output","Enable smaller/periodic keep-alive chunks or reduce max_tokens so the response finishes sooner","Increase upstream idle timeouts","If partial results are acceptable, collect yielded chunks and catch this error to use them"],"exampleFix":"// before\nconst out = []; for await (const c of streamSse(response)) out.push(c);\n\n// after\nconst out = [];\ntry { for await (const c of streamSse(response)) out.push(c); }\ncatch (e) { if (!/mid-stream/.test(e.message)) throw e; /* use partial out[] or retry */ }","handlingStrategy":"retry","validationCode":null,"typeGuard":null,"tryCatchPattern":"try { for await (const c of streamSse(response)) out.push(c); } catch (e) { if (/mid-stream/.test(e.message)) { if (out.length && acceptPartial) return out; else return retry(); } throw e; }","preventionTips":["Retry the full request on mid-stream cancellation; never reuse partial output as complete","Reduce max_tokens to finish before idle timeouts","Collect chunks incrementally so partial data is salvageable"],"tags":["fetch","streaming","network","connection-reset","retry"],"backgroundTag":"premature-stream-close","analyzedSha":"5522c6f44ca0ac3528b37244818fbfa39b5af470","analyzedAt":"2026-08-27T11:28:54.683Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}