You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Bug: Index.get() returns inconsistent values for non-existent key #494
When calling Index.get() with a key that doesn't exist in the index, it sometimes returns the vector of some values, instead of consistently returning None as specified in the official documentation.
Interestingly, trying to access the key -1 gives values which change over time, while too-large keys seems to give the last embedding again (as in the reporter's case)
x = usearch_index.Index(metric='IP', dtype=uindex.ScalarKind(12), ndim=3)
embs = np.float16(np.random.normal(scale=1e-4, size=[128, 3]))
x.add(np.arange(128), embs)
for i in (-2, -1, 0, 127, 128, 256):
print(i)
print(x.get(i))
print(x.get(i))
if i < 128:
print(embs[i])
I've traced this bug to the C++ Python binding implementation in python/lib.cpp. The issue occurs in the get_typed_vectors_for_keys function when multi=False.
The Problem
When multi=False, the code allocates a numpy array and calls index.get() without checking if the key exists:
The underlying C++ index.get() method returns false when a key doesn't exist (see index_dense.hpp lines 2194-2209), but this return value is completely ignored. This leaves uninitialized memory in the numpy array.
Compare this to the multi=True case (lines 913-930) which correctly handles non-existent keys:
if (!vectors_count) {
results[task_idx] = py::none();
continue;
}
Proposed Fix
The multi=False branch should check the return value of index.get() and handle non-existent keys appropriately:
} else {
// For single-key case, return the vector or None directlyif (keys_count == 1) {
dense_key_t key = *reinterpret_cast<dense_key_tconst*>(keys_data);
py::array_t<external_at> result_py({1, static_cast<Py_ssize_t>(index.scalar_words())});
auto result_py2d = result_py.templatemutable_unchecked<2>();
bool found = index.get(key, (internal_at*)&result_py2d(0, 0), 1);
if (!found) {
returnpy::none();
}
return result_py[0]; // Return the single vector, not a 2D array
}
// For multiple keys, return a tuple with None for missing keys
py::tuple results(keys_count);
for (Py_ssize_t task_idx = 0; task_idx != keys_count; ++task_idx) {
dense_key_t key = *reinterpret_cast<dense_key_tconst*>(keys_data + task_idx * keys_info.strides[0]);
py::array_t<external_at> result_py({1, static_cast<Py_ssize_t>(index.scalar_words())});
auto result_py2d = result_py.templatemutable_unchecked<2>();
bool found = index.get(key, (internal_at*)&result_py2d(0, 0), 1);
if (!found) {
results[task_idx] = py::none();
} else {
results[task_idx] = result_py[0];
}
}
return results;
}
This fix ensures:
Single non-existent keys return None (not uninitialized memory)
Multiple key queries return a tuple with None for non-existent keys
Behavior is consistent between multi=True and multi=False modes
Matches the documented API contract
Why This Is Critical
This bug can lead to:
Security issues: Uninitialized memory might contain sensitive data
Unpredictable behavior: Values change between calls (as shown with key -1)
Silent data corruption: Applications may process garbage data thinking it's valid
The fix is straightforward - just check the return value that's already being provided by the underlying C++ method.
Activity
sdenton4 commentedon Oct 15, 2024
Interestingly, trying to access the key -1 gives values which change over time, while too-large keys seems to give the last embedding again (as in the reporter's case)
Output:
titusz commentedon Sep 14, 2025
Root Cause Analysis
I've traced this bug to the C++ Python binding implementation in
python/lib.cpp. The issue occurs in theget_typed_vectors_for_keysfunction whenmulti=False.The Problem
When
multi=False, the code allocates a numpy array and callsindex.get()without checking if the key exists:The underlying C++
index.get()method returnsfalsewhen a key doesn't exist (seeindex_dense.hpplines 2194-2209), but this return value is completely ignored. This leaves uninitialized memory in the numpy array.Compare this to the
multi=Truecase (lines 913-930) which correctly handles non-existent keys:Proposed Fix
The
multi=Falsebranch should check the return value ofindex.get()and handle non-existent keys appropriately:This fix ensures:
None(not uninitialized memory)Nonefor non-existent keysmulti=Trueandmulti=FalsemodesWhy This Is Critical
This bug can lead to:
-1)The fix is straightforward - just check the return value that's already being provided by the underlying C++ method.
ashvardanian commentedon Sep 14, 2025
Nice suggestions, @titusz! Can you please open a PR? I’ll merge a bit later today 🤗
fix(python): return None for non-existent keys in Index.get()
fix(python): return None for non-existent keys in Index.get()
fix(python): return None for non-existent keys in Index.get()
Merge: Node Lookups, Stats, & CI Upgrades
Fix: Return None for non-existent keys in Index.get() instead of unin…