The agent engineering platform.
Python agentsaiai-agentsanthropicchatgptdeepagentsenterpriseframeworkgeminigenerative-ailangchainlanggraphllmmultiagentopen-sourceopenaipydanticpythonragtypescript
fix: issue a warning if `np.nan` or `np.inf` are in `_cosine_similarity` argument Matrices (#31532)
Casi686a6b754caa
diff --git a/libs/core/langchain_core/vectorstores/utils.py b/libs/core/langchain_core/vectorstores/utils.pyindex 1cbad1b363..645306bccd 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 annotationsimport logging+import warningsfrom typing import TYPE_CHECKING, Unionif 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} "