mem0ai/mem0 · error · Error

Azure OpenAI embedBatch() returned ${allEmbeddings.length} e

Error message

Azure OpenAI embedBatch() returned ${allEmbeddings.length} embeddings for ${texts.length} texts using model '${this.model}'

What it means

Thrown by the AzureOpenAIEmbedder when the total number of embeddings returned across all batched requests does not equal the number of input texts. The embedder chunks inputs, sorts each response by index, and accumulates; a mismatch means the API dropped, duplicated, or mis-indexed results — returning mismatched vectors would corrupt memory storage, so it aborts. The message includes both counts and the model name for diagnosis.

Source

Thrown at mem0-ts/src/oss/src/embeddings/azure.ts:56

    const MAX_BATCH = 100;
    const allEmbeddings: number[][] = [];
    for (let i = 0; i < texts.length; i += MAX_BATCH) {
      const chunk = texts.slice(i, i + MAX_BATCH);
      const response = await this.client.embeddings.create({
        model: this.model,
        input: chunk,
        ...(this.embeddingDims !== undefined && {
          dimensions: this.embeddingDims,
        }),
      });
      allEmbeddings.push(
        ...response.data
          .sort((a, b) => a.index - b.index)
          .map((item) => item.embedding),
      );
    }
    if (allEmbeddings.length !== texts.length) {
      throw new Error(
        `Azure OpenAI embedBatch() returned ${allEmbeddings.length} embeddings for ${texts.length} texts using model '${this.model}'`,
      );
    }
    return allEmbeddings;
  }
}

View on GitHub (pinned to 001c235229)

Solutions

  1. Retry with a smaller batch: split texts into chunks of ~100-500 and call embedBatch per chunk, so a mismatch is isolated
  2. Log the counts from the message — if returned > sent, dedupe/inspect index values; if returned < sent, look for empty or oversized inputs
  3. Sanitize inputs (trim, drop empties, cap length) before embedding
  4. Add a retry with backoff for transient responses — mismatch on one request is often transient

Example fix

// before
const vectors = await embedder.embedBatch(allTexts); // 10k texts in one call

// after
const vectors: number[][] = [];
for (let i = 0; i < allTexts.length; i += 256) {
  vectors.push(...(await embedder.embedBatch(allTexts.slice(i, i + 256))));
}
Defensive patterns

Strategy: retry

Validate before calling

const CHUNK = 256; // safely under Azure per-request limits
for (let i = 0; i < texts.length; i += CHUNK) {
  const part = await embedder.embedBatch(texts.slice(i, i + CHUNK));
  // part.length === slice length or the embedder already threw for this small chunk
}

Try / catch

try {
  vectors = await embedder.embedBatch(texts);
} catch (e) {
  if (e instanceof Error && /embedBatch\(\) returned \d+ embeddings for \d+ texts/.test(e.message)) {
    // count mismatch: split and retry chunk-by-chunk so a bad chunk is isolated
    vectors = [];
    for (let i = 0; i < texts.length; i += 100) {
      vectors.push(...(await embedder.embedBatch(texts.slice(i, i + 100))));
    }
  } else throw e;
}

Prevention

When it happens

Trigger: Large embedBatch call where one chunked request fails partially or the service returns fewer data items; duplicated index values after sort shifting alignment; using a batch size near the API limit where inputs get merged/split unexpectedly (e.g. very long strings counted as multiple tokens).

Common situations: Batch embedding entire chat histories or document chunks in one call; occasional transient inconsistency under load; inputs containing empty strings that some deployments skip.

Related errors


AI-assisted analysis of mem0ai/mem0@001c235229 (2026-08-15). Data as JSON: /api/errors/4bbf5034bee28ef1. Report an issue: GitHub.