continuedev/continue · error · Error

The response was cancelled mid-stream. Try again. (Premature

Error message

The response was cancelled mid-stream. Try again. (Premature Close).

What it means

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.

Source

Thrown at packages/fetch/src/stream.ts:65

      }
    }
  } catch (e) {
    if (e instanceof Error) {
      if (e.name.startsWith("AbortError")) {
        return; // In case of client-side cancellation, just return
      }
      if (e.message.toLowerCase().includes("premature close")) {
        // Premature close can happen for various reasons, including:
        // - Malformed chunks of data received from the server
        // - The server closed the connection before sending the complete response
        // - Long delays from the server during streaming
        // - 'Keep alive' header being used in combination with an http agent and a set, low number of maxSockets
        if (chunks === 0) {
          throw new Error(
            "Stream was closed before any data was received. Try again. (Premature Close)",
          );
        } else {
          throw new Error(
            "The response was cancelled mid-stream. Try again. (Premature Close).",
          );
        }
      }
    }
    throw e;
  }
}

// Export for testing purposes
export function parseDataLine(line: string): any {
  const json = line.startsWith("data: ")
    ? line.slice("data: ".length)
    : line.slice("data:".length);

  try {
    const data = JSON.parse(json);
    if (data.error) {

View on GitHub (pinned to 5522c6f44c)

Solutions

  1. Retry the full request with backoff; discard partial output
  2. Enable smaller/periodic keep-alive chunks or reduce max_tokens so the response finishes sooner
  3. Increase upstream idle timeouts
  4. If partial results are acceptable, collect yielded chunks and catch this error to use them

Example fix

// before
const out = []; for await (const c of streamSse(response)) out.push(c);

// after
const out = [];
try { for await (const c of streamSse(response)) out.push(c); }
catch (e) { if (!/mid-stream/.test(e.message)) throw e; /* use partial out[] or retry */ }
Defensive patterns

Strategy: retry

Try / catch

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; }

Prevention

When it happens

Trigger: Server/proxy drops the connection partway through SSE/JSON streaming: timeouts, crashes mid-generation, or connection resets.

Common situations: Long LLM generations cut off by LB idle timeouts (no bytes while model thinks), flaky networks, or servers crashing mid-response.

Related errors


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