mem0ai/mem0 · error · ValueError

Ollama embed() returned {len(embeddings)} embeddings for {le

Error message

Ollama embed() returned {len(embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'

What it means

Raised by OllamaEmbedding.embed_batch when the number of vectors in the response's 'embeddings' list differs from the number of input texts (note: the message text says 'embed()' but the check lives in embed_batch). The local server dropped or added vectors for the batch, which larger batches make more likely.

Source

Thrown at mem0/embeddings/ollama.py:62

            text (str): The text to embed.
            memory_action (optional): The type of embedding to use. Must be one of "add", "search", or "update". Defaults to None.
        Returns:
            list: The embedding vector.
        """
        response = self.client.embed(model=self.config.model, input=text)
        embeddings = response.get("embeddings") or []
        if not embeddings:
            raise ValueError(f"Ollama embed() returned no embeddings for model '{self.config.model}'")
        return embeddings[0]

    def embed_batch(self, texts, memory_action="add"):
        """Embed multiple texts in a single Ollama API call."""
        if not texts:
            return []
        response = self.client.embed(model=self.config.model, input=texts)
        embeddings = response.get("embeddings") or []
        if len(embeddings) != len(texts):
            raise ValueError(f"Ollama embed() returned {len(embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'")
        return embeddings

View on GitHub (pinned to 001c235229)

Solutions

  1. Split the batch into smaller chunks (16-64 texts) and call embed_batch per chunk
  2. Filter empty/whitespace-only strings from texts before embedding
  3. Update the Ollama server to a current release
  4. If one input is malformed, embedding items individually with embed() isolates the offender

Example fix

// before
embs = embedder.embed_batch(texts)  # 500 texts at once

# after
texts = [t for t in texts if t and t.strip()]
embs = []
for i in range(0, len(texts), 32):
    embs.extend(embedder.embed_batch(texts[i:i+32]))
Defensive patterns

Strategy: retry

Validate before calling

texts = [t for t in texts if t and t.strip()]  # drop empties that servers skip
assert texts, "nothing to embed"

Try / catch

try:
    vecs = embedder.embed_batch(chunk)
except ValueError as e:
    if "embeddings for" in str(e):
        half = max(1, len(chunk) // 2)
        vecs = embedder.embed_batch(chunk[:half]) + embedder.embed_batch(chunk[half:])
    else:
        raise

Prevention

When it happens

Trigger: Calling Memory.add() with many messages in one call; an input list containing empty strings that the Ollama server skips; server-side truncation when the combined batch exceeds context handling.

Common situations: Bulk ingestion of chat histories; embedding batches mixing long and empty documents; older Ollama builds with per-request input limits.

Related errors


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