{"record":{"id":"e80c8b4ac9a52c21","repo":"deepset-ai/haystack","slug":"the-embedding-size-of-all-documents-should-be-the","errorCode":null,"errorMessage":"The embedding size of all Documents should be the same. Please make sure that the Documents have been embedded with the same model.","messagePattern":"The embedding size of all Documents should be the same\\. Please make sure that the Documents have been embedded with the same model\\.","errorType":"exception","errorClass":"DocumentStoreError","httpStatus":null,"severity":"error","filePath":"haystack/document_stores/in_memory/document_store.py","lineNumber":881,"sourceCode":"    ) -> list[float]:\n        \"\"\"\n        Computes the similarity scores between the query embedding and the embeddings of the documents.\n\n        :param embedding: Embedding of the query.\n        :param documents: A list of Documents.\n        :param scale_score: Whether to scale the scores of the Documents. Default is False.\n        :returns: A list of scores.\n        \"\"\"\n\n        query_embedding = np.array(embedding)\n        if query_embedding.ndim == 1:\n            query_embedding = np.expand_dims(a=query_embedding, axis=0)\n\n        try:\n            document_embeddings = np.array([doc.embedding for doc in documents])\n        except ValueError as e:\n            if \"inhomogeneous shape\" in str(e):\n                raise DocumentStoreError(\n                    \"The embedding size of all Documents should be the same. \"\n                    \"Please make sure that the Documents have been embedded with the same model.\"\n                ) from e\n            raise e\n        if document_embeddings.ndim == 1:\n            document_embeddings = np.expand_dims(a=document_embeddings, axis=0)\n\n        if self.embedding_similarity_function == \"cosine\":\n            # cosine similarity is a normed dot product; guard against zero-norm vectors\n            # (e.g. a zero embedding) which would otherwise divide by zero and yield NaN scores.\n            query_norm = np.linalg.norm(x=query_embedding, axis=1, keepdims=True)\n            document_norms = np.linalg.norm(x=document_embeddings, axis=1, keepdims=True)\n            query_embedding /= np.where(query_norm == 0.0, 1.0, query_norm)\n            document_embeddings /= np.where(document_norms == 0.0, 1.0, document_norms)\n\n        try:\n            scores = np.dot(a=query_embedding, b=document_embeddings.T)[0].tolist()\n        except ValueError as e:","sourceCodeStart":863,"sourceCodeEnd":899,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/document_stores/in_memory/document_store.py#L863-L899","documentation":"_compute_query_embedding_similarity_scores raises DocumentStoreError when the stored documents' embeddings have differing sizes. numpy cannot build a rectangular array from an inhomogeneous list ('inhomogeneous shape'), so the store translates that into a clear error telling you to re-embed with one model.","triggerScenarios":"Calling embedding_retrieval when documents in the store were embedded with different models or different truncation/dimension settings (e.g. mixed 384-dim and 768-dim vectors).","commonSituations":"Switching embedding models without re-indexing, re-embedding only a subset of documents, mixing documents from two corpora indexed with different models.","solutions":["Re-embed ALL documents with the same model (re-run the Embedder/DocumentWriter indexing pipeline into a fresh store)","Delete documents embedded with the old model before re-indexing","Verify dimensions are uniform before writing: len(set(len(d.embedding) for d in docs)) == 1"],"exampleFix":"// before\n# docs partly embedded with old 384-dim model, partly with new 768-dim model\nstore.embedding_retrieval(query_embedding=query_vec)\n// after\n# re-index everything with one model into a fresh store, then retrieve\nstore = InMemoryDocumentStore()\npipe = Pipeline().add_component(\"embedder\", embedder).add_component(\"writer\", DocumentWriter(store, policy=DuplicatePolicy.OVERWRITE))\npipe.run(...)\nstore.embedding_retrieval(query_embedding=query_vec)","handlingStrategy":"validation","validationCode":"dims = {len(d.embedding) for d in store.filter_documents() if d.embedding is not None}\nif len(dims) > 1:\n    raise RuntimeError(f\"Store has mixed embedding sizes: {dims}; re-embed all documents with one model\")","typeGuard":null,"tryCatchPattern":"from haystack.errors import DocumentStoreError\ntry:\n    docs = store.embedding_retrieval(query_embedding=query_embedding)\nexcept DocumentStoreError as e:\n    if \"embedding size of all Documents\" in str(e):\n        # re-index the whole corpus with a single embedding model, then retry\n        ...\n    else:\n        raise","preventionTips":["Never switch embedding models without re-embedding the entire corpus","Use DocumentWriter with a fresh store when changing models","Periodically audit stored document embedding dimensions for uniformity"],"tags":["embeddings","dimension-mismatch","document-store"],"backgroundTag":"embedding-dimension-mismatch","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}