mem0ai/mem0 · error · ValueError

Azure OpenAI embed_batch() returned {len(all_embeddings)} em

Error message

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

What it means

AzureOpenAIEmbedding.embed_batch chunks texts into groups of 100 and calls the Azure OpenAI embeddings endpoint, accumulating results sorted by index. After all chunks it asserts len(all_embeddings) == len(texts); a mismatch (duplicate/merged inputs, silent truncation, or the endpoint returning fewer data items than inputs) raises this ValueError. This is a defensive integrity check, not a normal validation rule.

Source

Thrown at mem0/embeddings/azure_openai.py:73

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

    def embed_batch(self, texts, memory_action="add"):
        """Embed multiple texts in a single Azure OpenAI API call.

        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]
            response = self.client.embeddings.create(
                input=chunk,
                model=self.config.model,
            )
            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"Azure 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. Sanitize inputs before the call: drop empty/whitespace-only strings and deduplicate if semantics allow
  2. Log texts[i] around the failing chunk to find the offending input, then split or trim it
  3. If it recurs with valid input, capture the raw response of the chunk to confirm whether Azure returned fewer items — then open a support issue / retry that chunk

Example fix

# before
texts = ["mem one", "", "mem two"]
mem.add_batch(texts)  # '' may be dropped by the endpoint

# after
texts = [t for t in texts if t and t.strip()]
mem.add_batch(texts)
Defensive patterns

Strategy: validation

Validate before calling

clean = [t for t in texts if isinstance(t, str) and t.strip()]
if len(clean) != len(texts):
    logger.warning("dropped %d empty texts before embed", len(texts) - len(clean))
texts = clean

Type guard

def is_clean_batch(xs: list[str]) -> bool:
    return bool(xs) and all(isinstance(x, str) and x.strip() for x in xs)

Try / catch

try:
    vecs = embedding.embed_batch(texts)
except ValueError as e:
    if "embed_batch() returned" in str(e):
        # bisect the batch to find and repair the offending chunk
        vecs = embed_with_bisect(embedding, texts)
    else:
        raise

Prevention

When it happens

Trigger: Sending a batch containing empty strings or duplicate texts that some Azure deployments collapse; an endpoint revision that drops invalid inputs instead of erroring; extremely large inputs where a chunk silently returns partial data. Rarely hit in normal operation.

Common situations: Preprocessing that leaves '' entries in the batch; text exceeding the model's token limit causing the service to skip rather than error; comparing counts after Nones are filtered upstream.

Related errors


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