{"record":{"id":"aa441e38ee63b7d5","repo":"Mintplex-Labs/anything-llm","slug":"stream-returned-undefined-chunk-aborting-reply","errorCode":null,"errorMessage":"Stream returned undefined chunk. Aborting reply - check model provider logs.","messagePattern":"Stream returned undefined chunk\\. Aborting reply - check model provider logs\\.","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/AiProviders/ollama/index.js","lineNumber":376,"sourceCode":"      let usage = {\n        prompt_tokens: 0,\n        completion_tokens: 0,\n      };\n\n      // Establish listener to early-abort a streaming response\n      // in case things go sideways or the user does not like the response.\n      // We preserve the generated text but continue as if chat was completed\n      // to preserve previously generated content.\n      const handleAbort = () => {\n        stream?.endMeasurement(usage);\n        clientAbortedHandler(resolve, fullText);\n      };\n      response.on(\"close\", handleAbort);\n\n      try {\n        for await (const chunk of stream) {\n          if (chunk === undefined)\n            throw new Error(\n              \"Stream returned undefined chunk. Aborting reply - check model provider logs.\"\n            );\n\n          if (chunk.done) {\n            usage.prompt_tokens = chunk.prompt_eval_count;\n            usage.completion_tokens = chunk.eval_count;\n            usage.duration = chunk.eval_duration / 1e9;\n            writeResponseChunk(response, {\n              uuid,\n              sources,\n              type: \"textResponseChunk\",\n              textResponse: \"\",\n              close: true,\n              error: false,\n            });\n            response.removeListener(\"close\", handleAbort);\n            stream?.endMeasurement(usage);\n            resolve(fullText);","sourceCodeStart":358,"sourceCodeEnd":394,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/AiProviders/ollama/index.js#L358-L394","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check Ollama server logs around the timestamp for crashes/OOM.","Increase OLLAMA_RESPONSE_TIMEOUT so slow generations are not cut off.","Update the 'ollama' npm package to match the server version.","If behind a proxy, raise its proxy_read_timeout / idle timeout for the Ollama route."],"exampleFix":"// before\nfor await (const chunk of stream) {\n  if (chunk === undefined)\n    throw new Error('Stream returned undefined chunk. Aborting reply - check model provider logs.');\n\n// after - tolerate a stray undefined and continue, only abort on repeated failures\nfor await (const chunk of stream) {\n  if (chunk === undefined) { undefinedStreak++; if (undefinedStreak > 3) throw new Error('Ollama stream yielded repeated undefined chunks.'); continue; }\n  undefinedStreak = 0;","handlingStrategy":"try-catch","validationCode":null,"typeGuard":"const isDefinedChunk = (c) => c !== undefined && c !== null;","tryCatchPattern":"try {\n  await llm.handleStream(response, stream, responseProps);\n} catch (e) {\n  if (e.message.includes('undefined chunk')) {\n    // mid-stream drop — surface partial text and retry the turn\n    writeResponseChunk(response, { type: 'textResponseChunk', textResponse: '', close: true, error: 'stream interrupted' });\n  }\n  throw e;\n}","preventionTips":["Keep the ollama npm client version aligned with the Ollama server version.","Set OLLAMA_RESPONSE_TIMEOUT high enough for long generations to avoid mid-stream cuts.","Raise reverse-proxy idle timeouts for the Ollama route."],"tags":["ollama","streaming","runtime","resilience"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}