{"record":{"id":"668b0071027865c6","repo":"mem0ai/mem0","slug":"ollama-embed-returned-len-embeddings-embeddin","errorCode":null,"errorMessage":"Ollama embed() returned {len(embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'","messagePattern":"Ollama embed\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/embeddings/ollama.py","lineNumber":62,"sourceCode":"            text (str): The text to embed.\n            memory_action (optional): The type of embedding to use. Must be one of \"add\", \"search\", or \"update\". Defaults to None.\n        Returns:\n            list: The embedding vector.\n        \"\"\"\n        response = self.client.embed(model=self.config.model, input=text)\n        embeddings = response.get(\"embeddings\") or []\n        if not embeddings:\n            raise ValueError(f\"Ollama embed() returned no embeddings for model '{self.config.model}'\")\n        return embeddings[0]\n\n    def embed_batch(self, texts, memory_action=\"add\"):\n        \"\"\"Embed multiple texts in a single Ollama API call.\"\"\"\n        if not texts:\n            return []\n        response = self.client.embed(model=self.config.model, input=texts)\n        embeddings = response.get(\"embeddings\") or []\n        if len(embeddings) != len(texts):\n            raise ValueError(f\"Ollama embed() returned {len(embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'\")\n        return embeddings\n","sourceCodeStart":44,"sourceCodeEnd":64,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/embeddings/ollama.py#L44-L64","documentation":"Raised by OllamaEmbedding.embed_batch when the number of vectors in the response's 'embeddings' list differs from the number of input texts (note: the message text says 'embed()' but the check lives in embed_batch). The local server dropped or added vectors for the batch, which larger batches make more likely.","triggerScenarios":"Calling Memory.add() with many messages in one call; an input list containing empty strings that the Ollama server skips; server-side truncation when the combined batch exceeds context handling.","commonSituations":"Bulk ingestion of chat histories; embedding batches mixing long and empty documents; older Ollama builds with per-request input limits.","solutions":["Split the batch into smaller chunks (16-64 texts) and call embed_batch per chunk","Filter empty/whitespace-only strings from texts before embedding","Update the Ollama server to a current release","If one input is malformed, embedding items individually with embed() isolates the offender"],"exampleFix":"// before\nembs = embedder.embed_batch(texts)  # 500 texts at once\n\n# after\ntexts = [t for t in texts if t and t.strip()]\nembs = []\nfor i in range(0, len(texts), 32):\n    embs.extend(embedder.embed_batch(texts[i:i+32]))","handlingStrategy":"retry","validationCode":"texts = [t for t in texts if t and t.strip()]  # drop empties that servers skip\nassert texts, \"nothing to embed\"","typeGuard":null,"tryCatchPattern":"try:\n    vecs = embedder.embed_batch(chunk)\nexcept ValueError as e:\n    if \"embeddings for\" in str(e):\n        half = max(1, len(chunk) // 2)\n        vecs = embedder.embed_batch(chunk[:half]) + embedder.embed_batch(chunk[half:])\n    else:\n        raise","preventionTips":["Filter empty strings before batching","Keep batches <=32 against local Ollama","Update the Ollama server when batch behavior changes"],"tags":["python","ollama","embeddings","batching","local-server","mem0"],"backgroundTag":null,"analyzedSha":"001c235229be8795e3834520467bd0d661ed8f34","analyzedAt":"2026-08-15T01:55:42.685Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}