mem0ai/mem0 · error · ValueError

HuggingFace embed_batch() returned {len(embeddings)} embeddi

Error message

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

What it means

HuggingFaceEmbedding.embed_batch has two paths. When huggingface_base_url is set (OpenAI-compatible HF Inference Endpoints), it calls client.embeddings.create, sorts response.data by index, and checks the count; the first raise (line ~54) fires when the endpoint returns fewer/more embedding objects than input texts. This guards against endpoint-side truncation or deduplication.

Source

Thrown at mem0/embeddings/huggingface.py:54

        Returns:
            list: The embedding vector.
        """
        if self.config.huggingface_base_url:
            return self.client.embeddings.create(
                input=text, model=self.config.model, **self.config.model_kwargs
            ).data[0].embedding
        else:
            return self.model.encode(text, convert_to_numpy=True).tolist()

    def embed_batch(self, texts, memory_action="add"):
        if not texts:
            return []
        if self.config.huggingface_base_url:
            response = self.client.embeddings.create(input=texts, model=self.config.model, **self.config.model_kwargs)
            sorted_data = sorted(response.data, key=lambda x: x.index)
            embeddings = [item.embedding for item in sorted_data]
            if len(embeddings) != len(texts):
                raise ValueError(
                    f"HuggingFace embed_batch() returned {len(embeddings)} embeddings for {len(texts)} texts"
                    f" using model '{self.config.model}'"
                )
            return embeddings
        else:
            result = self.model.encode(texts, convert_to_numpy=True).tolist()
            if len(result) != len(texts):
                raise ValueError(
                    f"HuggingFace embed_batch() returned {len(result)} embeddings for {len(texts)} texts"
                    f" using model '{self.config.model}'"
                )
            return result

View on GitHub (pinned to 001c235229)

Solutions

  1. Check the endpoint's serverless/limits config and reduce batch size below its max batch/tokens
  2. Deduplicate inputs before the call, then map results back to original positions
  3. Drop empty strings from the input list

Example fix

# before
texts = ["dup", "dup", "unique"]
embed_batch(texts)  # endpoint returns 2 for 3 inputs

# after
uniq = list(dict.fromkeys(texts))
vecs = embed_batch(uniq)
vec_by_text = dict(zip(uniq, vecs))
result = [vec_by_text[t] for t in texts]
Defensive patterns

Strategy: fallback

Validate before calling

seen = set()
uniq = [t for t in texts if not (t in seen or seen.add(t))]
# embed uniq, then expand back to original order/length

Try / catch

try:
    vecs = embedding.embed_batch(texts)
except ValueError as e:
    if "embed_batch() returned" in str(e):
        vecs = embed_dedup_and_expand(embedding, texts)
    else:
        raise

Prevention

When it happens

Trigger: Using an HF Inference Endpoint (huggingface_base_url set) whose OpenAI-compatible API merges or drops duplicate inputs; endpoint config with a max batch/token limit that silently truncates; response.data items missing for empty strings.

Common situations: Sending batches larger than the endpoint's configured limit; duplicated texts in a memory batch that a caching layer collapses; an endpoint revision with different batching semantics.

Related errors


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