mem0ai/mem0 · error · ValueError

LM Studio embed_batch() returned {len(embeddings)} embedding

Error message

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

What it means

Raised by LMStudioEmbedding.embed_batch when the LM Studio local server returns a different number of embedding vectors than the number of input texts. After sorting the response by index, the code sanity-checks count equality; a mismatch means the server dropped, duplicated, or truncated inputs — behavior seen with concurrent requests or server versions that cap batch size.

Source

Thrown at mem0/embeddings/lmstudio.py:39

        Get the embedding for the given text using LM Studio.
        Args:
            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.
        """
        text = text.replace("\n", " ")
        return self.client.embeddings.create(input=[text], model=self.config.model).data[0].embedding

    def embed_batch(self, texts, memory_action="add"):
        if not texts:
            return []
        cleaned = [t.replace("\n", " ") for t in texts]
        response = self.client.embeddings.create(input=cleaned, model=self.config.model)
        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"LM Studio embed_batch() returned {len(embeddings)} embeddings for {len(texts)} texts"
                f" using model '{self.config.model}'"
            )
        return embeddings

View on GitHub (pinned to 001c235229)

Solutions

  1. Reduce batch size — chunk texts client-side (e.g. 32-64 per call) before calling embed_batch
  2. Update LM Studio to a current version and restart the server; check its logs for dropped requests
  3. If it persists, embed texts one-by-one with embed() as a correctness check to isolate which inputs fail
  4. Ensure only one client uses the model endpoint at a time, or enable a larger context in LM Studio server settings

Example fix

// before
embs = embedder.embed_batch(texts)  # 1000 texts in one call -> count mismatch

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

Strategy: retry

Validate before calling

# client-side: cap batch size to what LM Studio reliably handles before calling
MAX_SAFE = 64
def safe_batches(texts):
    return [texts[i:i+MAX_SAFE] for i in range(0, len(texts), MAX_SAFE)]

Try / catch

try:
    vecs = embedder.embed_batch(chunk)
except ValueError as e:
    if "returned" in str(e) and "embeddings for" in str(e):
        # fall back to per-item embedding for this chunk
        vecs = [embedder.embed(t) for t in chunk]
    else:
        raise

Prevention

When it happens

Trigger: Calling embed_batch(texts) via Memory.add() on many memories at once where the LM Studio server silently drops items; running an LM Studio version whose /v1/embeddings endpoint limits input array length; a race where another client shares the same local model slot.

Common situations: Bulk-ingesting memories into mem0 pointed at localhost:1234; LM Studio server updated and its batching behavior changed; sending batches larger than the model's context handling on a low-memory machine.

Related errors


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