{"record":{"id":"e28f5c4062d504a0","repo":"headroomlabs-ai/headroom","slug":"embedding-dimension-embedding-shape-0-does-not","errorCode":null,"errorMessage":"Embedding dimension {embedding.shape[0]} does not match index dimension {self._dimension}","messagePattern":"Embedding dimension (.+?) does not match index dimension (.+?)","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"headroom/memory/adapters/hnsw.py","lineNumber":338,"sourceCode":"\n    async def index(self, memory: Memory) -> None:\n        \"\"\"Index a memory's embedding for similarity search.\n\n        The memory must have an embedding set. If max_entries is set and\n        the limit is reached, low-importance entries are evicted.\n\n        Args:\n            memory: The memory to index.\n\n        Raises:\n            ValueError: If the memory has no embedding or wrong dimension.\n        \"\"\"\n        if memory.embedding is None:\n            raise ValueError(f\"Memory {memory.id} has no embedding\")\n\n        embedding = np.asarray(memory.embedding, dtype=np.float32)\n        if embedding.shape[0] != self._dimension:\n            raise ValueError(\n                f\"Embedding dimension {embedding.shape[0]} does not match \"\n                f\"index dimension {self._dimension}\"\n            )\n\n        with self._lock:\n            # Check if already indexed - update if so\n            if memory.id in self._memory_to_hnsw:\n                await self._update_embedding_internal(memory.id, embedding)\n                # Update metadata\n                self._metadata[memory.id] = IndexedMemoryMetadata.from_memory(memory)\n            else:\n                # Evict if at capacity (before adding new entry)\n                if self._max_entries is not None:\n                    current_size = len(self._memory_to_hnsw)\n                    if current_size >= self._max_entries:\n                        self._evict_entries(self._eviction_batch_size)\n\n                # Resize HNSW index if needed (separate from entry limit)","sourceCodeStart":320,"sourceCodeEnd":356,"githubUrl":"https://github.com/headroomlabs-ai/headroom/blob/322425c43bffde1ed0b64fecf3cf5951565dd82b/headroom/memory/adapters/hnsw.py#L320-L356","documentation":"Raised by HNSWVectorIndex.add_memory when the supplied embedding's length differs from the dimension the index was constructed with (self._dimension). HNSW graphs have a fixed vector size at creation, so any vector of a different length is rejected before insertion.","triggerScenarios":"Index created with dimension=384 but the Embedder produces 768-dim vectors (e.g. switching MiniLM to a larger model); mixing embeddings from different providers in one index; loading an index saved with another dimension.","commonSituations":"Changing embedding model without rebuilding the index; copy-pasting a dimension constant that no longer matches the model; using a default dimension while embedding with a non-default model.","solutions":["Print/inspect embedding.shape[0] and the index dimension, then recreate the index with dimension matching your Embedder output.","If the model changed, rebuild the index from scratch (re-embed all memories) — you cannot mix dimensions in HNSW.","Centralize the dimension: derive index dimension from embedder.dimension instead of a hardcoded literal."],"exampleFix":"// before\nindex = HNSWVectorIndex(dimension=384)\nawait index.add_memory(memory)  # embedder returns 768-dim\n\n// after\nindex = HNSWVectorIndex(dimension=embedder.dimension)\nawait index.add_memory(memory)","handlingStrategy":"validation","validationCode":"dim = np.asarray(memory.embedding).shape[0]\nif dim != index.dimension:\n    raise RuntimeError(f\"embedder dim {dim} != index dim {index.dimension}\")","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Construct the index with dimension=embedder.dimension, never a hardcoded literal.","Add an integration test asserting a fresh embedder output fits the configured index."],"tags":["hnsw","dimension-mismatch","embedding","validation"],"backgroundTag":null,"analyzedSha":"322425c43bffde1ed0b64fecf3cf5951565dd82b","analyzedAt":"2026-08-15T01:03:05.481Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}