{"record":{"id":"b79eefba3f3b548c","repo":"deepset-ai/haystack","slug":"the-embedding-size-of-the-query-should-be-the-same","errorCode":null,"errorMessage":"The embedding size of the query should be the same as the embedding size of the Documents. Please make sure that the query has been embedded with the same model as the Documents.","messagePattern":"The embedding size of the query should be the same as the embedding size of the Documents\\. Please make sure that the query has been embedded with the same model as the Documents\\.","errorType":"exception","errorClass":"DocumentStoreError","httpStatus":null,"severity":"error","filePath":"haystack/document_stores/in_memory/document_store.py","lineNumber":901,"sourceCode":"                    \"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:\n            if \"shapes\" in str(e) and \"not aligned\" in str(e):\n                raise DocumentStoreError(\n                    \"The embedding size of the query should be the same as the embedding size of the Documents. \"\n                    \"Please make sure that the query has been embedded with the same model as the Documents.\"\n                ) from e\n            raise e\n\n        if scale_score:\n            if self.embedding_similarity_function == \"dot_product\":\n                scores = [expit(float(score / DOT_PRODUCT_SCALING_FACTOR)) for score in scores]\n            elif self.embedding_similarity_function == \"cosine\":\n                scores = [(score + 1) / 2 for score in scores]\n\n        return scores\n\n    async def count_documents_async(self) -> int:\n        \"\"\"\n        Returns the number of documents present in the DocumentStore.\n        \"\"\"\n        return len(self.storage.keys())","sourceCodeStart":883,"sourceCodeEnd":919,"githubUrl":"https://github.com/deepset-ai/haystack/blob/e318778c9bf60a1963e3b5f451359655dd696c30/haystack/document_stores/in_memory/document_store.py#L883-L919","documentation":"_compute_query_embedding_similarity_scores raises DocumentStoreError when the query embedding's dimension differs from the document embeddings', so np.dot fails with a 'shapes not aligned' error. The store wraps it in a message telling you to embed the query with the same model as the documents.","triggerScenarios":"Calling embedding_retrieval with a query vector of different length than the stored document vectors — e.g. documents embedded with a 768-dim model but the query embedded with a 384-dim model (or vice versa).","commonSituations":"Swapping the retriever's embedder without re-indexing documents, using different embedding models for indexing and query time, config drift between indexing and query pipelines.","solutions":["Embed the query with the exact same model used for the documents (same name, version, and dimension)","Re-index documents if the query-side model must change","Assert dimension match before retrieval: len(query_embedding) == len(docs[0].embedding)"],"exampleFix":"// before\nquery_vec = embedder_query_model.encode(query)  # 384-dim, docs are 768-dim\nstore.embedding_retrieval(query_embedding=query_vec)\n// after\nquery_vec = same_model_as_documents.encode(query)\nassert len(query_vec) == len(store.storage[next(iter(store.storage))].embedding)\nstore.embedding_retrieval(query_embedding=query_vec)","handlingStrategy":"validation","validationCode":"sample_doc = next(iter(store.storage.values()), None)\nif sample_doc and sample_doc.embedding and len(query_embedding) != len(sample_doc.embedding):\n    raise RuntimeError(\n        f\"Query embedding dim {len(query_embedding)} != document dim {len(sample_doc.embedding)}; \"\n        \"use the same embedding model for query and documents\"\n    )","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 the query\" in str(e):\n        query_embedding = query_embedder.run(query)[\"embedding\"]  # same model as documents\n        docs = store.embedding_retrieval(query_embedding=query_embedding)\n    else:\n        raise","preventionTips":["Use the identical embedding model (and dimension) at index time and query time","Keep indexing and query pipelines in sync; version your embedding model config","Assert len(query_embedding) == len(document_embedding) in tests for retrieval pipelines"],"tags":["embeddings","dimension-mismatch","retrieval"],"backgroundTag":"embedding-dimension-mismatch","analyzedSha":"e318778c9bf60a1963e3b5f451359655dd696c30","analyzedAt":"2026-08-30T11:45:20.711Z","schemaVersion":2},"datasetVersion":"2026-08-30T13:17:10.514Z"}