{"record":{"id":"3a8aec1a8e867d05","repo":"mem0ai/mem0","slug":"huggingface-embedbatch-returned-embeddings-len","errorCode":null,"errorMessage":"HuggingFace embedBatch() returned ${embeddings.length} embeddings for ${texts.length} texts using model '${this.model}'","messagePattern":"HuggingFace embedBatch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"Error","httpStatus":null,"severity":"error","filePath":"mem0-ts/src/oss/src/embeddings/huggingface.ts","lineNumber":71,"sourceCode":"        `HuggingFace embed() returned no embeddings for model '${this.model}'`,\n      );\n    }\n    return response.data[0].embedding;\n  }\n\n  async embedBatch(texts: string[]): Promise<number[][]> {\n    if (texts.length === 0) {\n      return [];\n    }\n    const response = await this.openai.embeddings.create({\n      model: this.model,\n      input: texts,\n    });\n    const embeddings = response.data\n      .sort((a, b) => a.index - b.index)\n      .map((item) => item.embedding);\n    if (embeddings.length !== texts.length) {\n      throw new Error(\n        `HuggingFace embedBatch() returned ${embeddings.length} embeddings ` +\n          `for ${texts.length} texts using model '${this.model}'`,\n      );\n    }\n    return embeddings;\n  }\n}\n","sourceCodeStart":53,"sourceCodeEnd":79,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0-ts/src/oss/src/embeddings/huggingface.ts#L53-L79","documentation":"Thrown by HuggingFaceEmbedder.embedBatch() when the number of embeddings extracted from the response does not equal the number of input texts. Results are sorted by their index field then mapped; a count mismatch means the server dropped or duplicated rows, and returning them would silently mis-align vectors with texts. The message carries both counts and the model name. Empty input is short-circuited to [] before the request, so this only fires for non-empty batches.","triggerScenarios":"TEI server with a lower max batch size than the input array (extra rows silently truncated); response rows missing/malformed index fields causing dedupe or mis-sort; one input in the batch rejected server-side.","commonSituations":"Embedding large document chunk lists in one call exceeding TEI's --max-batch-size; mixing string lengths that trip server-side truncation rules.","solutions":["Chunk the call below the TEI server's max batch size (default often 32-64): loop with slice","Log the two counts from the message to confirm truncation (returned < sent almost always means batch-size cap)","Increase TEI's --max-batch-size / --max-concurrent-requests if you control the server","Retry once with backoff for transient server-side drops"],"exampleFix":"// before\nconst vecs = await embedder.embedBatch(chunks); // 500 chunks vs TEI max 32\n\n// after\nconst vecs: number[][] = [];\nfor (let i = 0; i < chunks.length; i += 32) {\n  vecs.push(...(await embedder.embedBatch(chunks.slice(i, i + 32))));\n}","handlingStrategy":"retry","validationCode":"const TEI_MAX_BATCH = 32; // match your TEI server's --max-batch-size\nif (texts.length > TEI_MAX_BATCH) {\n  // chunk upstream instead of letting the server truncate\n}\nfor (let i = 0; i < texts.length; i += TEI_MAX_BATCH) {\n  await embedder.embedBatch(texts.slice(i, i + TEI_MAX_BATCH));\n}","typeGuard":null,"tryCatchPattern":"try {\n  vectors = await embedder.embedBatch(texts);\n} catch (e) {\n  if (e instanceof Error && /embedBatch\\(\\) returned \\d+ embeddings/.test(e.message)) {\n    // almost always server batch-size truncation: halve and retry\n    const half = Math.ceil(texts.length / 2);\n    vectors = [\n      ...(await embedder.embedBatch(texts.slice(0, half))),\n      ...(await embedder.embedBatch(texts.slice(half))),\n    ];\n  } else throw e;\n}","preventionTips":["Know your TEI server's --max-batch-size and chunk client-side below it","Never use partial results on mismatch — vectors must stay index-aligned with texts","Log the counts from the message to distinguish truncation (fewer) from duplication (more)"],"tags":["huggingface","tei","embeddings","batching","data-integrity"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}