langchain-ai/langchain · error · ValueError
NaN values found, please remove the NaN values and try again
Error message
NaN values found, please remove the NaN values and try again
What it means
Raised by cosine_similarity (numpy path) when every entry of the computed similarity matrix is NaN. The numpy implementation normalizes by x_norm and y_norm; a zero-norm vector (all zeros) makes the division produce NaN/Inf entries. As a last line of defense the function checks np.isnan(similarity).all() — if nothing in the matrix is a valid number, no meaningful similarity exists and it raises rather than returning garbage. Note the check uses .all(): partially-NaN matrices do not raise; individual NaN/Inf entries are just zeroed.
Source
Thrown at libs/core/langchain_core/vectorstores/utils.py:103
f"Number of columns in X and Y must be the same. X has shape {x.shape} "
f"and Y has shape {y.shape}."
)
raise ValueError(msg)
if not _HAS_SIMSIMD:
logger.debug(
"Unable to import simsimd, defaulting to NumPy implementation. If you want "
"to use simsimd please install with `pip install simsimd`."
)
x_norm = np.linalg.norm(x, axis=1)
y_norm = np.linalg.norm(y, axis=1)
# Ignore divide by zero errors run time warnings as those are handled below.
with np.errstate(divide="ignore", invalid="ignore"):
similarity: npt.NDArray[np.floating] = np.dot(x, y.T) / np.outer(
x_norm, y_norm
)
if np.isnan(similarity).all():
msg = "NaN values found, please remove the NaN values and try again"
raise ValueError(msg) from None
similarity[np.isnan(similarity) | np.isinf(similarity)] = 0.0
return similarity
x = np.array(x, dtype=np.float32)
y = np.array(y, dtype=np.float32)
return 1 - np.array(simd.cdist(x, y, metric="cosine"))
def maximal_marginal_relevance(
query_embedding: npt.NDArray[np.floating],
embedding_list: list[list[float]],
lambda_mult: float = 0.5,
k: int = 4,
) -> list[int]:
"""Calculate maximal marginal relevance.
Args:
query_embedding: The query embedding.View on GitHub (pinned to e32fa9a52e)
Solutions
- Find and fix the zero-norm inputs: filter or reject empty/whitespace texts before embedding (e.g. skip docs where not doc.page_content.strip()).
- If a vector is legitimately all zeros in your pipeline, replace it with a small epsilon vector or drop that row before calling cosine_similarity.
- Debug by locating zero rows: zero_rows = np.where(~np.any(x, axis=1))[0] (same for y) and inspecting the corresponding source texts.
- Check your embedding call — a mock or offline stub returning [0.0]*dim will always trip this in tests; make stubs return distinct nonzero vectors.
Example fix
// before
query_vec = embedder.embed_query("") # returns [0.0, 0.0, ..., 0.0]
sim = cosine_similarity([query_vec], doc_vecs) # all NaN -> ValueError
// after
query = ""
if not query.strip():
raise ValueError("query text is empty; refusing to embed")
query_vec = embedder.embed_query(query)
sim = cosine_similarity([query_vec], doc_vecs) Defensive patterns
Strategy: validation
Validate before calling
import numpy as np
def reject_zero_vectors(embeddings: list[list[float]]) -> None:
arr = np.asarray(embeddings, dtype=np.float32)
norms = np.linalg.norm(arr, axis=-1)
if np.any(norms == 0):
bad = np.where(norms == 0)[0].tolist()
msg = f"zero-norm (all-zero) embeddings at indices {bad}; check empty inputs"
raise ValueError(msg) Type guard
import numpy as np
def all_vectors_nonzero(embeddings: object) -> bool:
"""True if input is array-like with no all-zero rows."""
try:
arr = np.asarray(embeddings, dtype=np.float32)
except Exception:
return False
return bool(np.all(np.linalg.norm(arr, axis=-1) > 0)) Try / catch
try:
sim = cosine_similarity(x, y)
except ValueError as e:
if "NaN values found" in str(e):
logger.warning("zero-norm vector in inputs; dropping it and retrying")
x = [v for v in x if any(v)]
y = [v for v in y if any(v)]
sim = cosine_similarity(x, y)
else:
raise Prevention
- Reject empty or whitespace-only texts before embedding (if not text.strip(): skip or raise).
- Make test/mock embedders return distinct nonzero vectors, never np.zeros.
- Validate embeddings after generation: assert np.linalg.norm(vec) > 0.
- Log a warning when an embedder returns identical zero vectors — it usually signals an upstream empty-input bug.
When it happens
Trigger: Passing at least one all-zero vector on one side while every pairing against the other side yields NaN — concretely, when a zero-norm row makes its entire row/column NaN and that covers the whole matrix (e.g. a single zero query vector against any documents, or zero vectors on both sides). Typical sources: an embedding model returning zeros for empty/whitespace text, dummy placeholder embeddings of [0.0]*dim, or a test fixture built with np.zeros.
Common situations: Embedding empty strings ('' or ' ') with models or mock embedders that return zero vectors; unit tests with placeholder zero embeddings passed through cosine_similarity; batch pipelines where a parsing bug produces empty documents whose embeddings are zeros; normalizing vectors to zero when a document's text is dropped.
Related errors
- Number of columns in X and Y must be the same. X has shape {
- cosine_similarity requires numpy to be installed. Please ins
- invalid IP address
- Failed to resolve hostname '{hostname}': {e}
- Network error while validating URL: {e}
AI-assisted analysis of langchain-ai/langchain@e32fa9a52e (2026-08-14).
Data as JSON: /api/errors/b4cda64fa283d874.
Report an issue: GitHub.