{"record":{"id":"eeb780e43161ea32","repo":"mem0ai/mem0","slug":"gemini-embed-batch-returned-len-all-embeddings","errorCode":null,"errorMessage":"Gemini embed_batch() returned {len(all_embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'","messagePattern":"Gemini embed_batch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/embeddings/gemini.py","lineNumber":52,"sourceCode":"        config = types.EmbedContentConfig(output_dimensionality=self.config.embedding_dims)\n\n        # Call the embed_content method with the correct parameters\n        response = self.client.models.embed_content(model=self.config.model, contents=text, config=config)\n\n        return response.embeddings[0].values\n\n    def embed_batch(self, texts, memory_action=\"add\"):\n        if not texts:\n            return []\n        config = types.EmbedContentConfig(output_dimensionality=self.config.embedding_dims)\n        MAX_BATCH = 100\n        all_embeddings = []\n        for i in range(0, len(texts), MAX_BATCH):\n            chunk = [t.replace(\"\\n\", \" \") for t in texts[i : i + MAX_BATCH]]\n            response = self.client.models.embed_content(model=self.config.model, contents=chunk, config=config)\n            all_embeddings.extend(e.values for e in response.embeddings)\n        if len(all_embeddings) != len(texts):\n            raise ValueError(\n                f\"Gemini 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":34,"sourceCodeEnd":57,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/embeddings/gemini.py#L34-L57","documentation":"GeminiEmbedding.embed_batch chunks texts into batches of 100, calls models.embed_content via the google-genai SDK, and extends all_embeddings from response.embeddings. A final count check requires one embedding per input text; a mismatch raises this ValueError naming the model. In practice the usual cause is batch_items_per_request / API limits causing the SDK to return fewer embedding entries, or empty input strings being dropped.","triggerScenarios":"Passing a list containing empty strings (the API may return no vector for them); a chunk larger than the model's per-request item or token budget returning partial results; model output_dimensionality misconfigured so some entries are omitted.","commonSituations":"Batching user memories where some cleaned to ''; using gemini-embedding-001 with batch sizes near API limits; version differences in the google-genai SDK silently truncating large responses.","solutions":["Filter empty/whitespace strings from the batch before calling embed_batch","Lower MAX_BATCH / split your input list into smaller chunks (e.g. ≤ 100 texts or fewer tokens per chunk)","Log the failing chunk boundaries and retry just that chunk to identify the problematic input"],"exampleFix":"# before\ntexts = [\"\", \"hello\", \"   \"]\nembedding.embed_batch(texts)\n\n# after\ntexts = [t for t in texts if t.strip()]\nembedding.embed_batch(texts)","handlingStrategy":"validation","validationCode":"clean = [t for t in texts if t and t.strip()]\nif not clean:\n    raise ValueError(\"nothing to embed\")\n# chunk defensively below API batch limits\nchunks = [clean[i:i+50] for i in range(0, len(clean), 50)]","typeGuard":"def is_embeddable_batch(xs: list[str]) -> bool:\n    return bool(xs) and all(isinstance(x, str) and x.strip() for x in xs)","tryCatchPattern":"try:\n    vecs = embedding.embed_batch(texts)\nexcept ValueError as e:\n    if \"embed_batch() returned\" in str(e):\n        vecs = [embedding.embed(t) for t in texts]  # fall back to per-item calls\n    else:\n        raise","preventionTips":["Prefer batch sizes well under the Gemini per-request item limit","Strip empty strings before embedding; retry a failed chunk in isolation to locate bad input"],"tags":["gemini","embeddings","batch","data-integrity"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}