{"record":{"id":"d409902016f9e824","repo":"mem0ai/mem0","slug":"lm-studio-embed-batch-returned-len-embeddings","errorCode":null,"errorMessage":"LM Studio embed_batch() returned {len(embeddings)} embeddings for {len(texts)} texts using model '{self.config.model}'","messagePattern":"LM Studio embed_batch\\(\\) returned (.+?) embeddings for (.+?) texts using model '(.+?)'","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"mem0/embeddings/lmstudio.py","lineNumber":39,"sourceCode":"        Get the embedding for the given text using LM Studio.\n        Args:\n            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        text = text.replace(\"\\n\", \" \")\n        return self.client.embeddings.create(input=[text], model=self.config.model).data[0].embedding\n\n    def embed_batch(self, texts, memory_action=\"add\"):\n        if not texts:\n            return []\n        cleaned = [t.replace(\"\\n\", \" \") for t in texts]\n        response = self.client.embeddings.create(input=cleaned, model=self.config.model)\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\"LM Studio embed_batch() returned {len(embeddings)} embeddings for {len(texts)} texts\"\n                f\" using model '{self.config.model}'\"\n            )\n        return embeddings\n","sourceCodeStart":21,"sourceCodeEnd":44,"githubUrl":"https://github.com/mem0ai/mem0/blob/001c235229be8795e3834520467bd0d661ed8f34/mem0/embeddings/lmstudio.py#L21-L44","documentation":"Raised by LMStudioEmbedding.embed_batch when the LM Studio local server returns a different number of embedding vectors than the number of input texts. After sorting the response by index, the code sanity-checks count equality; a mismatch means the server dropped, duplicated, or truncated inputs — behavior seen with concurrent requests or server versions that cap batch size.","triggerScenarios":"Calling embed_batch(texts) via Memory.add() on many memories at once where the LM Studio server silently drops items; running an LM Studio version whose /v1/embeddings endpoint limits input array length; a race where another client shares the same local model slot.","commonSituations":"Bulk-ingesting memories into mem0 pointed at localhost:1234; LM Studio server updated and its batching behavior changed; sending batches larger than the model's context handling on a low-memory machine.","solutions":["Reduce batch size — chunk texts client-side (e.g. 32-64 per call) before calling embed_batch","Update LM Studio to a current version and restart the server; check its logs for dropped requests","If it persists, embed texts one-by-one with embed() as a correctness check to isolate which inputs fail","Ensure only one client uses the model endpoint at a time, or enable a larger context in LM Studio server settings"],"exampleFix":"// before\nembs = embedder.embed_batch(texts)  # 1000 texts in one call -> count mismatch\n\n# after\nBATCH = 32\nembs = []\nfor i in range(0, len(texts), BATCH):\n    embs.extend(embedder.embed_batch(texts[i:i+BATCH]))","handlingStrategy":"retry","validationCode":"# client-side: cap batch size to what LM Studio reliably handles before calling\nMAX_SAFE = 64\ndef safe_batches(texts):\n    return [texts[i:i+MAX_SAFE] for i in range(0, len(texts), MAX_SAFE)]","typeGuard":null,"tryCatchPattern":"try:\n    vecs = embedder.embed_batch(chunk)\nexcept ValueError as e:\n    if \"returned\" in str(e) and \"embeddings for\" in str(e):\n        # fall back to per-item embedding for this chunk\n        vecs = [embedder.embed(t) for t in chunk]\n    else:\n        raise","preventionTips":["Keep batches small (<=64) against local LM Studio","Check LM Studio server logs after any mismatch","Ensure a single client uses the model endpoint at a time"],"tags":["python","lmstudio","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"}