{"record":{"id":"40b757855cd6b74b","repo":"continuedev/continue","slug":"stream-was-closed-before-any-data-was-received-tr","errorCode":null,"errorMessage":"Stream was closed before any data was received. Try again. (Premature Close)","messagePattern":"Stream was closed before any data was received\\. Try again\\. \\(Premature Close\\)","errorType":"error_code","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"packages/fetch/src/stream.ts","lineNumber":61,"sourceCode":"      const nodeStream = response.body as unknown as NodeJS.ReadableStream;\n      for await (const chunk of toAsyncIterable(nodeStream)) {\n        yield decoder.decode(chunk, { stream: true });\n        chunks++;\n      }\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);","sourceCodeStart":43,"sourceCodeEnd":79,"githubUrl":"https://github.com/continuedev/continue/blob/5522c6f44ca0ac3528b37244818fbfa39b5af470/packages/fetch/src/stream.ts#L43-L79","documentation":"The underlying stream emitted a premature close event before any chunk arrived (chunks === 0), so the generator throws with a 'Try again' hint — transient connection issues are the expected cause.","triggerScenarios":"Server or proxy closes the connection before sending data: malformed early chunks, idle timeouts, keep-alive + low maxSockets on the http agent, or abrupt server shutdown.","commonSituations":"LLM endpoints dropping long-pending streaming requests, corporate proxies/LBs with short timeouts, keep-alive agent misconfiguration under concurrency.","solutions":["Retry the request — the message itself recommends it; use 2-3 attempts with backoff","Increase agent maxSockets or disable keep-alive for streaming requests","Raise proxy/LB idle timeouts if you control them","Reduce request latency (shorter prompts, streaming sooner) to avoid idle cutoffs"],"exampleFix":"// before\nconst data = await collectStream(streamSse(response));\n\n// after\nasync function withRetry(fn, n = 3) { for (let i = 0; ; i++) { try { return await fn(); } catch (e) { if (i === n - 1 || !/Premature Close/.test(e.message)) throw e; } } }\nconst data = await withRetry(() => collectStream(streamSse(response)));","handlingStrategy":"retry","validationCode":"if (!response.ok || !response.body) { /* avoid entering streamResponse */ }","typeGuard":null,"tryCatchPattern":"const run = async () => { for await (const c of streamSse(response)) out.push(c); }; for (let i = 0; i < 3; i++) { out = []; try { await run(); break; } catch (e) { if (!/Premature Close|Try again/.test(e.message) || i === 2) throw e; } }","preventionTips":["Retry transient premature closes with backoff","Increase http agent maxSockets / disable keep-alive for streams","Raise proxy idle timeouts for slow first tokens"],"tags":["fetch","streaming","network","premature-close","retry"],"backgroundTag":"premature-stream-close","analyzedSha":"5522c6f44ca0ac3528b37244818fbfa39b5af470","analyzedAt":"2026-08-27T11:28:54.683Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}