InMemoryStore vector search returns nan similarity scores for zero-norm query vectors (numpy path)
In libs/checkpoint/langgraph/store/memory/__init__.py, _cosine_similarity has two code paths. The pure-Python fallback guards against zero-norm vectors (if norm1 > 0 and norm2 > 0 else 0.0), but the numpy path only masks zero-norm candidate vectors (Y), not a zero-norm query vector (X):
X_norm = np.linalg.norm(X_arr)
Y_norm = np.linalg.norm(Y_arr, axis=1)
mask = Y_norm != 0 # X_norm not checked
similarities[mask] = np.dot(Y_arr[mask], X_arr) / (Y_norm[mask] * X_norm)
When the query embedding has zero norm (some embedding models emit an all-zeros vector for empty/out-of-vocabulary input), the division is by zero and every score becomes nan. Because nan compares as neither greater nor less, the subsequent sorted(...) on scores yields an undefined order — silently corrupting search ranking. numpy is the normal (installed) path, so this is the common case.
Reproduce
from langgraph.store.memory import _cosine_similarity
print(_cosine_similarity([0.0, 0.0], [[1.0, 2.0], [3.0, 4.0]]))
# numpy installed -> [nan, nan] (pure-Python fallback returns [0.0, 0.0])
Expected
A zero-norm query vector should score 0.0 for every candidate, matching the pure-Python path, rather than producing nan.
I have a fix + regression test ready (mask X_norm == 0 too, covering both code paths). Happy to open a PR once assigned.
I'd like to work on this one.
The numpy path in _cosine_similarity skips the zero-norm guard that the pure-Python fallback already has, so a zero-norm query vector produces nan similarity scores instead of being handled consistently.
Plan: add the same zero-norm check to the numpy path (return 0.0 similarity for those rows, matching the fallback's behavior), plus a regression test covering zero-norm query vectors on the numpy path.
Could a maintainer assign this to me?
I'm the reporter and have a fix + regression test ready — opened #8375. It masks X_norm == 0 in the numpy path so a zero-norm query vector scores 0.0 (matching the pure-Python fallback) instead of nan, with a test parametrized across both code paths. Could a maintainer assign me?
Reliability note
Production automation failures usually reduce to a contract issue before a model/prompt issue:
- Success criteria — durable artifact id (message/booking/row), not only node green
- Side-effect before complete — never blind-retry without an idempotency key on the business object
- Tool vs path split — external action can succeed while the node/agent path still errors (silent-green / false-red)
If you have one failing execution + expected vs actual, reply with stack/version and I will send a free root-cause checklist.
Hi @GaoSSR , can I work on this issue ?
Hi @MThabsheer7, thanks for asking! I'm already on this one — the fix is ready locally, and I'm just waiting for a maintainer to assign the issue. Appreciate the interest though.
Could a maintainer assign me here? I've opened #8708 with the fix and a regression test (masks a zero-norm X in the numpy path so it returns 0.0 like the pure-Python fallback; test is parametrized over both paths). It's closed pending assignment.