{"record":{"id":"be4bbee4aca70fe6","repo":"microsoft/graphrag","slug":"query-and-document-embeddings-are-not-compatible","errorCode":null,"errorMessage":"Query and document embeddings are not compatible. Please ensure that the embeddings are of the same type and length.","messagePattern":"Query and document embeddings are not compatible\\. Please ensure that the embeddings are of the same type and length\\.","errorType":"validation","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"packages/graphrag/graphrag/query/structured_search/drift_search/drift_context.py","lineNumber":214,"sourceCode":"            chat_model=self.model,\n            text_embedder=self.text_embedder,\n            tokenizer=self.tokenizer,\n            reports=self.reports,\n        )\n\n        query_embedding, token_ct = await query_processor(query)\n\n        report_df = self.convert_reports_to_df(self.reports)\n\n        # Check compatibility between query embedding and document embeddings\n        if not self.check_query_doc_encodings(\n            query_embedding, report_df[\"full_content_embedding\"].iloc[0]\n        ):\n            error_message = (\n                \"Query and document embeddings are not compatible. \"\n                \"Please ensure that the embeddings are of the same type and length.\"\n            )\n            raise ValueError(error_message)\n\n        # Vectorized cosine similarity computation\n        query_norm = np.linalg.norm(query_embedding)\n        document_norms = np.linalg.norm(\n            report_df[\"full_content_embedding\"].to_list(), axis=1\n        )\n        dot_products = np.dot(\n            np.vstack(report_df[\"full_content_embedding\"].to_list()), query_embedding\n        )\n        report_df[\"similarity\"] = dot_products / (document_norms * query_norm)\n\n        # Sort by similarity and select top-k\n        top_k = report_df.nlargest(self.config.drift_k_followups, \"similarity\")\n\n        return top_k.loc[:, [\"short_id\", \"community_id\", \"full_content\"]], token_ct\n","sourceCodeStart":196,"sourceCodeEnd":230,"githubUrl":"https://github.com/microsoft/graphrag/blob/f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704/packages/graphrag/graphrag/query/structured_search/drift_search/drift_context.py#L196-L230","documentation":"DRIFT search compares the embedding of the user query against the full_content_embedding column of the community reports using cosine similarity, and first verifies the two vectors are compatible (same dimensionality/type). If the query embedder produces vectors of a different dimension or dtype than the report embeddings stored in the index, this ValueError is raised.","triggerScenarios":"Calling DriftSearch.search with text_embedder configured for a different model than the one used at indexing time (e.g. index built with text-embedding-ada-002 of 1536 dims, query uses a 3072-dim model), or when full_content_embedding in the report DataFrame is empty/malformed.","commonSituations":"Switching embedding models or API tiers between indexing and querying, changing the embedding model in settings.yaml after the index was built, using a custom LocalSearch mixed context with mismatched vector lengths, or NaN/None embeddings in the parquet file.","solutions":["Use the same embedding model (same dimensions) for querying as was used to build the index","Re-index the data with the new embedding model if you intend to switch models","Inspect report_df['full_content_embedding'] for empty/NaN vectors and drop or regenerate those rows","Confirm your settings.yaml text_embedder configuration matches the index's model"],"exampleFix":"# before\ncontext = DriftContext(text_embedder=OpenAIEmbedding(model=\"text-embedding-3-large\"), ...)  # index used ada-002\n\n# after\ncontext = DriftContext(text_embedder=OpenAIEmbedding(model=\"text-embedding-ada-002\"), ...)  # match index model","handlingStrategy":"validation","validationCode":"query_emb = text_embedder(\"test\")\nreport_emb = report_df[\"full_content_embedding\"].iloc[0]\nif len(query_emb) != len(report_emb):\n    raise ValueError(\n        f\"Embedding dims differ: query={len(query_emb)}, reports={len(report_emb)}. \"\n        \"Use the same embedding model as indexing or re-index.\"\n    )","typeGuard":"def embeddings_compatible(q, d) -> bool:\n    import numpy as np\n    q, d = np.asarray(q), np.asarray(d)\n    return q.ndim == 1 and d.ndim == 1 and q.shape[0] == d.shape[0] and np.isfinite(q).all() and np.isfinite(d).all()","tryCatchPattern":null,"preventionTips":["Pin the same embedding model in settings.yaml for both indexing and querying","Store the embedding model name/dims as metadata alongside the index and check it at query time","After loading reports, drop rows with NaN/None in full_content_embedding"],"tags":["graphrag","drift-search","embeddings","dimension-mismatch"],"backgroundTag":"embedding-dimension-mismatch","analyzedSha":"f40e9a26ce62ba0b3fef8837d24aafdcc6e6c704","analyzedAt":"2026-08-27T11:16:29.677Z","schemaVersion":2},"datasetVersion":"2026-08-27T13:17:12.746Z"}