Description
While testing the VectorStore class, we noticed issues with the approximate search feature (enabled with use_approx=True). This feature is supposed to make searches faster by grouping similar items into clusters and searching only the closest clusters instead of the entire dataset.
However, in some cases:
- Search fails completely with an error like:
ValueError: need at least one array to concatenate
This happens when the feature tries to look for items in a cluster but the cluster ends up empty.
- Search returns fewer results than requested, for example asking for 2 results but getting only 1.
These problems seem related to how clusters are created and queried in the approximate search.
Steps to Reproduce
- Create a
VectorStore with a small number of items (e.g., 5 company names with 8-dimensional embeddings).
- Build the KMeans index with 2 clusters.
- Call
search with use_approx=True and small n_probe_clusters values.
Example failing test cases:
def test_approx_search_fallback_to_exact(vector_store: VectorStore) -> None:
vector_store.build_index(n_clusters=2)
query: NDArray[np.floating] = vector_store.embeddings[2]
results = vector_store.search(
query,
k=2,
use_approx=True,
n_probe_clusters=0,
)
# Fails with ValueError: need at least one array to concatenate
def test_approx_search_returns_results(vector_store: VectorStore) -> None:
vector_store.build_index(n_clusters=2)
query: NDArray[np.floating] = vector_store.embeddings[1]
results = vector_store.search(
query,
k=2,
use_approx=True,
n_probe_clusters=1,
)
assert len(results) == 2
# Currently returns only 1 result
Expected Behavior
- Approximate search should never crash.
- Approximate search should return up to the requested number of results, falling back to exact search if needed.
Observed Behavior
- Crashes with
ValueError in some cases.
- Returns fewer results than requested in other cases.
Suggested Next Steps
- Investigate why clusters can end up empty and cause concatenation errors.
- Ensure the search function gracefully falls back to exact search if there are no items in the probed clusters.
- Verify that
k results are always returned whenever possible.
Notes
Description
While testing the
VectorStoreclass, we noticed issues with the approximate search feature (enabled withuse_approx=True). This feature is supposed to make searches faster by grouping similar items into clusters and searching only the closest clusters instead of the entire dataset.However, in some cases:
This happens when the feature tries to look for items in a cluster but the cluster ends up empty.
These problems seem related to how clusters are created and queried in the approximate search.
Steps to Reproduce
VectorStorewith a small number of items (e.g., 5 company names with 8-dimensional embeddings).searchwithuse_approx=Trueand smalln_probe_clustersvalues.Example failing test cases:
Expected Behavior
Observed Behavior
ValueErrorin some cases.Suggested Next Steps
kresults are always returned whenever possible.Notes
use_approx=True) with small datasets or lown_probe_clusters.use_approx=False) works correctly and all related tests pass. (See PR tests: Add initial unit tests forVectorStore#128 )