mem0ai/mem0 · error · Error

OpenAI embedBatch() returned ${allEmbeddings.length} embeddi

Error message

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

What it means

OpenAIEmbedder.embedBatch() chunks texts, collects all embeddings, and verifies the total equals the number of input texts. A count mismatch means the API honored the request but returned a different number of embedding records than inputs, which would silently corrupt vector-to-memory association if accepted, so it throws.

Source

Thrown at mem0-ts/src/oss/src/embeddings/openai.ts:51

    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.openai.embeddings.create({
        model: this.model,
        input: chunk,
        encoding_format: "float",
        ...(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(
        `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 the operation: a mismatch is almost always a transient proxy/gateway issue
  2. If behind a gateway, bypass it and call api.openai.com directly to confirm where items are lost
  3. Update the gateway/self-hosted server to a version with correct batch embeddings support
  4. Report a bug with the model name, batch size, and gateway in the path if it reproduces against the real API
Defensive patterns

Strategy: retry

Type guard

function isBatchCountMismatch(err: unknown): boolean {
  return err instanceof Error && /embedBatch\(\) returned \d+ embeddings for \d+ texts/.test(err.message);
}

Try / catch

async function embedWithRetry(texts: string[], tries = 2) {
  for (let i = 0; ; i++) {
    try { return await embedder.embedBatch(texts); }
    catch (err) {
      if (i < tries && err instanceof Error && err.message.includes("embedBatch() returned")) continue;
      throw err;
    }
  }
}

Prevention

When it happens

Trigger: A proxy/gateway between the SDK and the OpenAI API that drops or duplicates items; a model or server bug returning partial data; passing duplicate or empty-string inputs through a relay that de-duplicates. Essentially never happens against the real OpenAI API with a healthy network path.

Common situations: Corporate LLM gateway or LiteLLM-style proxy mangling batch responses; locally hosted OpenAI-compatible servers (vLLM, older LM Studio) with incomplete batch support; interleaved retries at the HTTP layer.

Related errors


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