mem0ai/mem0 · error · ValueError

Together embed_batch() returned {len(embeddings)} embeddings

Error message

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

What it means

Raised by TogetherEmbedding.embed_batch when the Together AI embeddings endpoint returns a different number of vectors than input texts. After sorting response.data by index, the count check fails if Together dropped, merged, or truncated inputs — most often from batch-size limits on their embeddings API.

Source

Thrown at mem0/embeddings/together.py:39

        Get the embedding for the given text using OpenAI.

        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.
        """

        return self.client.embeddings.create(model=self.config.model, input=text).data[0].embedding

    def embed_batch(self, texts, memory_action="add"):
        if not texts:
            return []
        response = self.client.embeddings.create(model=self.config.model, input=texts)
        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"Together 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. Chunk texts to smaller batches (e.g. 32-128 per call) matching Together's documented batch limit for your model
  2. Remove empty/whitespace texts before the call
  3. Verify with a direct curl to api.together.xyz that N inputs return N embeddings for your model
  4. Retry the failing chunk — transient server-side truncation is possible

Example fix

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

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

Strategy: retry

Validate before calling

# keep batches within Together's per-request input cap
together_batches = [texts[i:i+64] for i in range(0, len(texts), 64)]

Try / catch

try:
    vecs = embedder.embed_batch(batch)
except ValueError as e:
    if "embeddings for" in str(e) and len(batch) > 1:
        mid = len(batch) // 2
        vecs = embedder.embed_batch(batch[:mid]) + embedder.embed_batch(batch[mid:])
    else:
        raise

Prevention

When it happens

Trigger: Calling embed_batch with more texts than Together's per-request input cap for the given embedding model; Together API behavior differences across model versions (e.g. model.json files with different batch limits); empty strings in the batch.

Common situations: Bulk ingestion via Memory.add on the Together provider; switching embedding models on Together without re-checking limits; proxy/gateway between client and Together modifying the payload.

Related errors


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