{"record":{"id":"c63881e7a98cc4db","repo":"mem0ai/mem0","slug":"openai-embed-batch-returned-len-all-embeddings","errorCode":null,"errorMessage":"OpenAI embed_batch() returned {len(all_embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'","messagePattern":"OpenAI embed_batch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/embeddings/openai.py","lineNumber":77,"sourceCode":"\n        Automatically chunks into batches of 100 to stay within API limits.\n        \"\"\"\n        MAX_BATCH = 100\n        texts = [text.replace(\"\\n\", \" \") for text in texts]\n        all_embeddings = []\n        for i in range(0, len(texts), MAX_BATCH):\n            chunk = texts[i : i + MAX_BATCH]\n            kwargs = {\n                \"input\": chunk,\n                \"model\": self.config.model,\n                \"encoding_format\": \"float\",\n            }\n            if self._pass_dimensions_to_api:\n                kwargs[\"dimensions\"] = self.config.embedding_dims\n            response = self.client.embeddings.create(**kwargs)\n            all_embeddings.extend(item.embedding for item in sorted(response.data, key=lambda x: x.index))\n        if len(all_embeddings) != len(texts):\n            raise ValueError(\n                f\"OpenAI embed_batch() returned {len(all_embeddings)} embeddings for {len(texts)} texts\"\n                f\" using model '{self.config.model}'\"\n            )\n        return all_embeddings\n","sourceCodeStart":59,"sourceCodeEnd":82,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/embeddings/openai.py#L59-L82","documentation":"Raised by OpenAIEmbedding.embed_batch after chunking texts into MAX_BATCH-sized requests and collecting all vectors: if the total count does not equal the input count, at least one API response lost or duplicated an item. The code already sorts each response by index, so a mismatch points to the API dropping inputs or a chunking/config edge, not ordering.","triggerScenarios":"Very large Memory.add() batches where one chunked request returns fewer data items than inputs; API-side truncation when inputs exceed token limits; network retry logic in the OpenAI SDK silently re-requesting a partial set.","commonSituations":"Bulk memory ingestion with thousands of texts; texts near the 8191-token per-input limit causing silent drops; mismatched openai package versions returning a different response shape.","solutions":["Reduce batch size by chunking texts client-side before calling embed_batch","Shorten or pre-truncate individual texts (OpenAI silently fails on over-limit inputs)","Pin/upgrade the openai package to a version compatible with this mem0 release","Log len(response.data) per chunk to find which request loses items; retry just that chunk"],"exampleFix":"// before\nembs = embedder.embed_batch(all_texts)  # thousands at once\n\n# after\nembs = []\nfor i in range(0, len(all_texts), 512):\n    embs.extend(embedder.embed_batch(all_texts[i:i+512]))","handlingStrategy":"retry","validationCode":"# pre-truncate over-limit inputs and chunk conservatively before embed_batch\nimport tiktoken\nenc = tiktoken.encoding_for_model(\"text-embedding-3-small\")\ntexts = [t[:8000] for t in texts]  # rough char guard; chunks of 512 below","typeGuard":null,"tryCatchPattern":"try:\n    vecs = embedder.embed_batch(chunk)\nexcept ValueError:\n    # count mismatch on one chunk: retry items individually to isolate and recover\n    vecs = []\n    for t in chunk:\n        try:\n            vecs.append(embedder.embed(t))\n        except Exception:\n            logger.warning(\"dropping unembeddable input\")","preventionTips":["Cap client-side batch size below the provider MAX_BATCH","Pre-truncate texts to the model token limit","Monitor len(response.data) per chunk in debug builds"],"tags":["python","openai","embeddings","batching","api","mem0"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}