{"record":{"id":"ab761e31f1493d45","repo":"langchain-ai/langchain","slug":"number-of-columns-in-x-and-y-must-be-the-same-x-h","errorCode":null,"errorMessage":"Number of columns in X and Y must be the same. X has shape {x.shape} and Y has shape {y.shape}.","messagePattern":"Number of columns in X and Y must be the same\\. X has shape (.+?) and Y has shape (.+?)\\.","errorType":"exception","errorClass":"ValueError","httpStatus":null,"severity":"error","filePath":"libs/core/langchain_core/vectorstores/utils.py","lineNumber":88,"sourceCode":"            \"NaN found in input arrays, unexpected return might follow\",\n            category=RuntimeWarning,\n            stacklevel=2,\n        )\n\n    # Check for Inf\n    if np.any(np.isinf(x)) or np.any(np.isinf(y)):\n        warnings.warn(\n            \"Inf found in input arrays, unexpected return might follow\",\n            category=RuntimeWarning,\n            stacklevel=2,\n        )\n\n    if x.shape[1] != y.shape[1]:\n        msg = (\n            f\"Number of columns in X and Y must be the same. X has shape {x.shape} \"\n            f\"and Y has shape {y.shape}.\"\n        )\n        raise ValueError(msg)\n    if not _HAS_SIMSIMD:\n        logger.debug(\n            \"Unable to import simsimd, defaulting to NumPy implementation. If you want \"\n            \"to use simsimd please install with `pip install simsimd`.\"\n        )\n        x_norm = np.linalg.norm(x, axis=1)\n        y_norm = np.linalg.norm(y, axis=1)\n        # Ignore divide by zero errors run time warnings as those are handled below.\n        with np.errstate(divide=\"ignore\", invalid=\"ignore\"):\n            similarity: npt.NDArray[np.floating] = np.dot(x, y.T) / np.outer(\n                x_norm, y_norm\n            )\n        if np.isnan(similarity).all():\n            msg = \"NaN values found, please remove the NaN values and try again\"\n            raise ValueError(msg) from None\n        similarity[np.isnan(similarity) | np.isinf(similarity)] = 0.0\n        return similarity\n","sourceCodeStart":70,"sourceCodeEnd":106,"githubUrl":"https://github.com/langchain-ai/langchain/blob/e32fa9a52eab3b61ad7a45399bfde59b3e580fc4/libs/core/langchain_core/vectorstores/utils.py#L70-L106","documentation":"cosine_similarity raises this ValueError after converting inputs to numpy arrays when the column dimensions differ: x has shape (n, d1) and y has shape (m, d2) with d1 != d2. Cosine similarity is only defined between vectors of the same dimensionality, and np.dot(x, y.T) (or simd.cdist) would fail or produce nonsense otherwise, so the function validates shapes explicitly and reports both shapes in the message.","triggerScenarios":"Calling cosine_similarity(x, y) where every row of x has length d1 and every row of y has length d2 != d1 — classically query embeddings produced by one model and document embeddings by another (different embedding dimensions, e.g. 1536 vs 3072, or 384 vs 768). Also triggered by malformed hand-built 1-D ragged inputs that np.array turns into an object/differently-shaped matrix.","commonSituations":"Switching embedding providers or model versions (e.g. moving documents from a 384-dim model to a 1536-dim model) without re-embedding the corpus; mixing query embeddings from a new model with a stale vector store embedded by the old one; copy-paste test fixtures with placeholder vectors of the wrong length; loading embeddings saved before a model upgrade.","solutions":["Re-embed the side with the wrong dimensionality so both x and y use the same embedding model, then retry.","Verify dimensions before the call: assert np.asarray(x).shape[1] == np.asarray(y).shape[1].","If you changed embedding models, rebuild the vector store (delete and re-add all documents with the new embedder) rather than mixing old and new vectors.","Inspect the shapes reported in the message — the side whose column count does not match your current model's dimension is the stale one."],"exampleFix":"// before\nquery_embs = openai_embedder.embed_queries(queries)   # 1536-dim\ndoc_embs = stored_vectors_from_old_model             # 1536? no, 3072-dim\nsim = cosine_similarity(query_embs, doc_embs)        # ValueError\n\n// after (re-embed docs with the same model)\ndoc_embs = openai_embedder.embed_documents(doc_texts)\nassert len(query_embs[0]) == len(doc_embs[0])\nsim = cosine_similarity(query_embs, doc_embs)","handlingStrategy":"validation","validationCode":"import numpy as np\n\ndef check_same_dimension(x, y) -> None:\n    dx = np.asarray(x).shape[1]\n    dy = np.asarray(y).shape[1]\n    if dx != dy:\n        msg = f\"embedding dimension mismatch: x={dx}, y={dy}; re-embed with one model\"\n        raise ValueError(msg)","typeGuard":"import numpy as np\n\ndef same_dimension(x: object, y: object) -> bool:\n    \"\"\"True if both inputs are 2-D with equal column counts.\"\"\"\n    try:\n        ax, ay = np.asarray(x), np.asarray(y)\n    except Exception:\n        return False\n    return ax.ndim == 2 and ay.ndim == 2 and ax.shape[1] == ay.shape[1]","tryCatchPattern":"try:\n    sim = cosine_similarity(x, y)\nexcept ValueError as e:\n    if \"Number of columns\" in str(e):\n        logger.error(\"stale embeddings detected (%s); rebuilding index\", e)\n        rebuild_vector_store()  # re-embed everything with the current model\n    else:\n        raise","preventionTips":["Record the embedding model name and dimension in the vector store's metadata and verify it on load.","Never mix embeddings from different models in one store; re-embed the whole corpus when switching models.","Assert dimension equality before calling cosine_similarity in pipeline code.","Wrap embedder instantiation in one factory so query-side and document-side always share the same configured model."],"tags":["validation","numpy","cosine-similarity","embeddings","dimension-mismatch","vectorstore"],"backgroundTag":null,"analyzedSha":"e32fa9a52eab3b61ad7a45399bfde59b3e580fc4","analyzedAt":"2026-08-14T18:42:09.092Z","schemaVersion":2},"datasetVersion":"2026-08-15T22:17:37.221Z"}