{"record":{"id":"c4fefaea93306e88","repo":"Mintplex-Labs/anything-llm","slug":"ollama-returned-empty-embeddings-for-batch","errorCode":null,"errorMessage":"Ollama returned empty embeddings for batch!","messagePattern":"Ollama returned empty embeddings for batch!","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"server/utils/EmbeddingEngines/ollama/index.js","lineNumber":115,"sourceCode":"    for (let i = 0; i < textChunks.length; i += this.maxConcurrentChunks) {\n      const batch = textChunks.slice(i, i + this.maxConcurrentChunks);\n      currentBatch++;\n\n      try {\n        // Use input param instead of prompt param to support batch processing\n        const res = await this.client.embed({\n          model: this.model,\n          input: batch,\n          options: {\n            // Always set the num_ctx to the max chunk length defined by the user in the settings\n            // so that the maximum context window is used and content is not truncated.\n            num_ctx: this.embeddingMaxChunkLength,\n          },\n        });\n\n        const { embeddings } = res;\n        if (!Array.isArray(embeddings) || embeddings.length === 0)\n          throw new Error(\"Ollama returned empty embeddings for batch!\");\n\n        // Using prompt param in embed() would return a single embedding (number[])\n        // but input param returns an array of embeddings (number[][]) for batch processing.\n        // This is why we spread the embeddings array into the data array.\n        data.push(...embeddings);\n        reportEmbeddingProgress(data.length, textChunks.length);\n        this.log(\n          `Batch ${currentBatch}/${totalBatches}: Embedded ${embeddings.length} chunks. Total: ${data.length}/${textChunks.length}`\n        );\n      } catch (err) {\n        this.log(err.message);\n        error = err.message;\n        data = [];\n        break;\n      }\n    }\n\n    if (!!error) throw new Error(`Ollama Failed to embed: ${error}`);","sourceCodeStart":97,"sourceCodeEnd":133,"githubUrl":"https://github.com/Mintplex-Labs/anything-llm/blob/526360e320da9d1b36074be5ed64fe76e5bbfbbd/server/utils/EmbeddingEngines/ollama/index.js#L97-L133","documentation":"Thrown inside the per-batch loop (line 114) when client.embed returns an `embeddings` field that is not a non-empty array. It is a defensive check on a successful RPC: Ollama answered but produced no vectors for the batch, which would leave gaps in the vector data, so the loop's catch captures it and the batch aborts.","triggerScenarios":"client.embed resolves but res.embeddings is undefined/null or []. Causes: the named model is a chat/LLM model with no embedding support; model loaded but input batch was empty after slicing; Ollama build that does not populate embeddings for the given model; num_ctx set so small the input is fully truncated to nothing.","commonSituations":"EMBEDDING_MODEL_PREF pointing at a chat model (e.g. llama3) instead of an embedding model (nomic-embed-text); Ollama version regression; all-whitespace chunks; embeddingMaxChunkLength misconfigured to 0 causing total truncation.","solutions":["Set EMBEDDING_MODEL_PREF to an actual embedding model (e.g. nomic-embed-text) — run `ollama pull nomic-embed-text`","Sanitize batch inputs to drop empty/whitespace-only strings before calling embedChunks","Verify with `ollama run <model>` / a direct embed call that the model returns vectors","Check maximumChunkLength() returns a sane positive value so num_ctx isn't zero"],"exampleFix":"// before\nEMBEDDING_MODEL_PREF=llama3   // chat model, no embeddings\n\n// after\nEMBEDDING_MODEL_PREF=nomic-embed-text","handlingStrategy":"validation","validationCode":"// probe the model returns vectors before the bulk run\nasync function ollamaEmbeds(client, model, sample = 'hello') {\n  const res = await client.embed({ model, input: [sample] });\n  return Array.isArray(res?.embeddings) && res.embeddings.length > 0;\n}","typeGuard":"function isOllamaEmptyEmbeddings(e) {\n  return e instanceof Error && /empty embeddings for batch/.test(e.message);\n}","tryCatchPattern":"try {\n  return await embedder.embedChunks(chunks);\n} catch (e) {\n  if (/empty embeddings for batch/.test(e.message)) {\n    throw new Error('Ollama model produced no vectors — use an embedding model', { cause: e });\n  }\n  throw e;\n}","preventionTips":["Use an embedding model (nomic-embed-text), not a chat model.","Filter empty/whitespace chunks before batching.","Ensure maximumChunkLength() returns a sane positive value so num_ctx isn't zero."],"tags":["ollama","embedding","runtime","data-validation"],"backgroundTag":null,"analyzedSha":"526360e320da9d1b36074be5ed64fe76e5bbfbbd","analyzedAt":"2026-08-13T01:45:47.170Z","schemaVersion":2},"datasetVersion":"2026-08-13T04:17:16.726Z"}