Mintplex-Labs/anything-llm · error · Error

Stream returned undefined chunk. Aborting reply - check mode

Error message

Stream returned undefined chunk. Aborting reply - check model provider logs.

What it means

Defensive guard inside the streaming 'for await' loop in handleStream. If the Ollama async iterator yields a chunk that is strictly undefined, the loop aborts with this message. This is not a normal Ollama response shape (valid chunks are objects with .message/.done); an undefined yield indicates the stream broke internally without throwing.

Source

Thrown at server/utils/AiProviders/ollama/index.js:376

      let usage = {
        prompt_tokens: 0,
        completion_tokens: 0,
      };

      // Establish listener to early-abort a streaming response
      // in case things go sideways or the user does not like the response.
      // We preserve the generated text but continue as if chat was completed
      // to preserve previously generated content.
      const handleAbort = () => {
        stream?.endMeasurement(usage);
        clientAbortedHandler(resolve, fullText);
      };
      response.on("close", handleAbort);

      try {
        for await (const chunk of stream) {
          if (chunk === undefined)
            throw new Error(
              "Stream returned undefined chunk. Aborting reply - check model provider logs."
            );

          if (chunk.done) {
            usage.prompt_tokens = chunk.prompt_eval_count;
            usage.completion_tokens = chunk.eval_count;
            usage.duration = chunk.eval_duration / 1e9;
            writeResponseChunk(response, {
              uuid,
              sources,
              type: "textResponseChunk",
              textResponse: "",
              close: true,
              error: false,
            });
            response.removeListener("close", handleAbort);
            stream?.endMeasurement(usage);
            resolve(fullText);

View on GitHub (pinned to 526360e320)

Solutions

  1. Check Ollama server logs around the timestamp for crashes/OOM.
  2. Increase OLLAMA_RESPONSE_TIMEOUT so slow generations are not cut off.
  3. Update the 'ollama' npm package to match the server version.
  4. If behind a proxy, raise its proxy_read_timeout / idle timeout for the Ollama route.

Example fix

// before
for await (const chunk of stream) {
  if (chunk === undefined)
    throw new Error('Stream returned undefined chunk. Aborting reply - check model provider logs.');

// after - tolerate a stray undefined and continue, only abort on repeated failures
for await (const chunk of stream) {
  if (chunk === undefined) { undefinedStreak++; if (undefinedStreak > 3) throw new Error('Ollama stream yielded repeated undefined chunks.'); continue; }
  undefinedStreak = 0;
Defensive patterns

Strategy: try-catch

Type guard

const isDefinedChunk = (c) => c !== undefined && c !== null;

Try / catch

try {
  await llm.handleStream(response, stream, responseProps);
} catch (e) {
  if (e.message.includes('undefined chunk')) {
    // mid-stream drop — surface partial text and retry the turn
    writeResponseChunk(response, { type: 'textResponseChunk', textResponse: '', close: true, error: 'stream interrupted' });
  }
  throw e;
}

Prevention

When it happens

Trigger: Network connection drops mid-stream and the SDK resolves an iteration as undefined instead of rejecting; Ollama server crash during generation; SDK (ollama npm package) version that yields undefined on certain error states; proxy/load-balancer killing the SSE connection mid-flight.

Common situations: Long generation over an unstable link; Ollama restarted or OOM-killed mid-chat; reverse proxy (nginx/cloudflare) idle-timeout closing the stream; version mismatch between Ollama server and the ollama npm client.

Related errors


AI-assisted analysis of Mintplex-Labs/anything-llm@526360e320 (2026-08-13). Data as JSON: /api/errors/aa441e38ee63b7d5. Report an issue: GitHub.