mem0ai/mem0 · error · ValueError

OpenAI embed_batch() returned {len(all_embeddings)} embeddin

Error message

OpenAI embed_batch() returned {len(all_embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'

What it means

Raised by OpenAIEmbedding.embed_batch after chunking texts into MAX_BATCH-sized requests and collecting all vectors: if the total count does not equal the input count, at least one API response lost or duplicated an item. The code already sorts each response by index, so a mismatch points to the API dropping inputs or a chunking/config edge, not ordering.

Source

Thrown at mem0/embeddings/openai.py:77

        Automatically chunks into batches of 100 to stay within API limits.
        """
        MAX_BATCH = 100
        texts = [text.replace("\n", " ") for text in texts]
        all_embeddings = []
        for i in range(0, len(texts), MAX_BATCH):
            chunk = texts[i : i + MAX_BATCH]
            kwargs = {
                "input": chunk,
                "model": self.config.model,
                "encoding_format": "float",
            }
            if self._pass_dimensions_to_api:
                kwargs["dimensions"] = self.config.embedding_dims
            response = self.client.embeddings.create(**kwargs)
            all_embeddings.extend(item.embedding for item in sorted(response.data, key=lambda x: x.index))
        if len(all_embeddings) != len(texts):
            raise ValueError(
                f"OpenAI embed_batch() returned {len(all_embeddings)} embeddings for {len(texts)} texts"
                f" using model '{self.config.model}'"
            )
        return all_embeddings

View on GitHub (pinned to 001c235229)

Solutions

  1. Reduce batch size by chunking texts client-side before calling embed_batch
  2. Shorten or pre-truncate individual texts (OpenAI silently fails on over-limit inputs)
  3. Pin/upgrade the openai package to a version compatible with this mem0 release
  4. Log len(response.data) per chunk to find which request loses items; retry just that chunk

Example fix

// before
embs = embedder.embed_batch(all_texts)  # thousands at once

# after
embs = []
for i in range(0, len(all_texts), 512):
    embs.extend(embedder.embed_batch(all_texts[i:i+512]))
Defensive patterns

Strategy: retry

Validate before calling

# pre-truncate over-limit inputs and chunk conservatively before embed_batch
import tiktoken
enc = tiktoken.encoding_for_model("text-embedding-3-small")
texts = [t[:8000] for t in texts]  # rough char guard; chunks of 512 below

Try / catch

try:
    vecs = embedder.embed_batch(chunk)
except ValueError:
    # count mismatch on one chunk: retry items individually to isolate and recover
    vecs = []
    for t in chunk:
        try:
            vecs.append(embedder.embed(t))
        except Exception:
            logger.warning("dropping unembeddable input")

Prevention

When it happens

Trigger: Very large Memory.add() batches where one chunked request returns fewer data items than inputs; API-side truncation when inputs exceed token limits; network retry logic in the OpenAI SDK silently re-requesting a partial set.

Common situations: Bulk memory ingestion with thousands of texts; texts near the 8191-token per-input limit causing silent drops; mismatched openai package versions returning a different response shape.

Related errors


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