{"record":{"id":"353d86a9aeb9eca8","repo":"mem0ai/mem0","slug":"huggingface-embed-batch-returned-len-embeddings","errorCode":null,"errorMessage":"HuggingFace embed_batch() returned {len(embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'","messagePattern":"HuggingFace embed_batch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/embeddings/huggingface.py","lineNumber":54,"sourceCode":"        Returns:\n            list: The embedding vector.\n        \"\"\"\n        if self.config.huggingface_base_url:\n            return self.client.embeddings.create(\n                input=text, model=self.config.model, **self.config.model_kwargs\n            ).data[0].embedding\n        else:\n            return self.model.encode(text, convert_to_numpy=True).tolist()\n\n    def embed_batch(self, texts, memory_action=\"add\"):\n        if not texts:\n            return []\n        if self.config.huggingface_base_url:\n            response = self.client.embeddings.create(input=texts, model=self.config.model, **self.config.model_kwargs)\n            sorted_data = sorted(response.data, key=lambda x: x.index)\n            embeddings = [item.embedding for item in sorted_data]\n            if len(embeddings) != len(texts):\n                raise ValueError(\n                    f\"HuggingFace embed_batch() returned {len(embeddings)} embeddings for {len(texts)} texts\"\n                    f\" using model '{self.config.model}'\"\n                )\n            return embeddings\n        else:\n            result = self.model.encode(texts, convert_to_numpy=True).tolist()\n            if len(result) != len(texts):\n                raise ValueError(\n                    f\"HuggingFace embed_batch() returned {len(result)} embeddings for {len(texts)} texts\"\n                    f\" using model '{self.config.model}'\"\n                )\n            return result\n","sourceCodeStart":36,"sourceCodeEnd":67,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/embeddings/huggingface.py#L36-L67","documentation":"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.","triggerScenarios":"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.","commonSituations":"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.","solutions":["Check the endpoint's serverless/limits config and reduce batch size below its max batch/tokens","Deduplicate inputs before the call, then map results back to original positions","Drop empty strings from the input list"],"exampleFix":"# before\ntexts = [\"dup\", \"dup\", \"unique\"]\nembed_batch(texts)  # endpoint returns 2 for 3 inputs\n\n# after\nuniq = list(dict.fromkeys(texts))\nvecs = embed_batch(uniq)\nvec_by_text = dict(zip(uniq, vecs))\nresult = [vec_by_text[t] for t in texts]","handlingStrategy":"fallback","validationCode":"seen = set()\nuniq = [t for t in texts if not (t in seen or seen.add(t))]\n# embed uniq, then expand back to original order/length","typeGuard":null,"tryCatchPattern":"try:\n    vecs = embedding.embed_batch(texts)\nexcept ValueError as e:\n    if \"embed_batch() returned\" in str(e):\n        vecs = embed_dedup_and_expand(embedding, texts)\n    else:\n        raise","preventionTips":["Know your HF endpoint's max batch/tokens and stay under it","Deduplicate before embedding and re-expand results afterward"],"tags":["huggingface","embeddings","batch","inference-endpoint","data-integrity"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}