From 686a6b754caa7353db88c9ccbf2fef36434e4219 Mon Sep 17 00:00:00 2001 From: Casi <73135100+CasiCode@users.noreply.github.com> Date: Wed, 16 Jul 2025 19:50:09 +0400 Subject: [PATCH] fix: issue a warning if `np.nan` or `np.inf` are in `_cosine_similarity` argument Matrices (#31532) - **Description**: issues a warning if inf and nan are passed as inputs to langchain_core.vectorstores.utils._cosine_similarity - **Issue**: Fixes #31496 - **Dependencies**: no external dependencies added, only warnings module imported --------- Co-authored-by: Mason Daugherty --- libs/core/langchain_core/vectorstores/utils.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/libs/core/langchain_core/vectorstores/utils.py b/libs/core/langchain_core/vectorstores/utils.py index 1cbad1b3633..645306bccde 100644 --- a/libs/core/langchain_core/vectorstores/utils.py +++ b/libs/core/langchain_core/vectorstores/utils.py @@ -7,6 +7,7 @@ as they can change without notice. from __future__ import annotations import logging +import warnings from typing import TYPE_CHECKING, Union if TYPE_CHECKING: @@ -46,6 +47,23 @@ def _cosine_similarity(x: Matrix, y: Matrix) -> np.ndarray: x = np.array(x) y = np.array(y) + + # Check for NaN + if np.any(np.isnan(x)) or np.any(np.isnan(y)): + warnings.warn( + "NaN found in input arrays, unexpected return might follow", + category=RuntimeWarning, + stacklevel=2, + ) + + # Check for Inf + if np.any(np.isinf(x)) or np.any(np.isinf(y)): + warnings.warn( + "Inf found in input arrays, unexpected return might follow", + category=RuntimeWarning, + stacklevel=2, + ) + if x.shape[1] != y.shape[1]: msg = ( f"Number of columns in X and Y must be the same. X has shape {x.shape} "