Repository navigation
fix(python)!: fetch minimum rank-fusion window per side in hybrid search - #4772
Conversation
SIDDARTHAREDDY8
left a comment
There was a problem hiding this comment.
Reviewed python/python/lancedb/query.py (_create_query_builders and AsyncHybridQuery._create_child_queries), the test changes in test_hybrid_query.py, and the docs update. Code-read only; I did not run the tests.
-
Both execution paths get the floor, but only one gets a behavioral test. The sync path (
LanceHybridQueryBuilder._run_hybridvia_create_query_builders) is covered by the newtest_hybrid_query_top_result_stable_across_limits, which uses the synctable.search(query_type="hybrid")API. The async path (AsyncHybridQuery._create_child_queries) is only asserted on indirectly, by the explain-plan test expectingfetch=50. If the floor were dropped from the async path, no test would fail on ranking behavior. Is an async version of the stability assertion planned, or is the async path covered elsewhere? -
The window is not configurable.
DEFAULT_HYBRID_RANK_WINDOW = 50is referenced only at the two sites above, with no builder option or override. The docs note the floor, so the behavior change is discoverable, but a user with an expensive reranker cannot opt back to fetching only the requested window. Is a follow-up to expose this (or an explicit decision to keep it fixed) intended? -
The regression test's sensitivity is unverified from the diff alone.
test_hybrid_query_top_result_stable_across_limitsuses a fixed seed and assertstop_id(1) == top_id(3) == top_id(10). I did not run it, so I cannot tell whether this fixture actually diverges under the oldlimit + offsetfetching; if it passes with and without the floor, it would not catch a future regression of the floor. It may be worth confirming the test fails with the floor temporarily removed, or noting that check in the test comment. -
Small nit:
import randomlanded at line 15 oftest_hybrid_query.py, after the third-party imports, while the file's stdlib import (from unittest import mock) sits at the top. Moving it up with the stdlib group would match the file's existing layout (and whatever formatter CI runs).
The explain-plan expectations and the docs paragraph were updated consistently with the new fetching behavior, including the note that the Rust and TypeScript APIs keep their existing limits.
…ion window Addresses review feedback: the async path previously had only the explain-plan fetch=50 assertion, no behavioral ranking check. Mirrors test_hybrid_query_top_result_stable_across_limits for the async builder (fails on base, passes with fix). Also moves 'import random' to the stdlib import group.
…ion window Addresses review feedback: the async path previously had only the explain-plan fetch=50 assertion, no behavioral ranking check. Mirrors test_hybrid_query_top_result_stable_across_limits for the async builder (fails on base, passes with fix). Also moves 'import random' to the stdlib import group.
|
Thanks for the thorough code-read. Addressing each point:
|
|
On
These checks used the candidate Python package with the beta.28 native extension and recorded Enterprise requests on a loopback server. Please validate the explicit limit before Blob schema discovery and before executing plan analysis. Extending the existing request-counter tests to these two entry points would catch both cases. On the stock 0.41.0 wheel, |
…and plan analysis The async hybrid builder already rejected an explicit limit(0) before retrieval, but two entry points still executed work: to_arrow with blob_mode bytes requested the vector leg output schema during Blob schema discovery, and analyze_plan executed both legs. Both now validate the explicit zero limit first and raise the same ValueError, for any offset. Tests count schema discovery calls and leg plan executions for both paths and assert neither runs. Signed-off-by: shadowmodder <sudhirvissa@gmail.com>
|
Thanks, both entry points are covered now. The async hybrid query validates an explicit zero limit before Blob schema discovery and before plan analysis, not only before retrieval: _blob_output_schema and analyze_plan both reject limit(0) with the same ValueError before the vector leg's output schema is requested and before either leg is analyzed, for any offset. The execution path is unchanged. Tests now count leg plan executions for analyze_plan at offset 0 and offset(2).limit(0), and count schema discovery calls for to_arrow(blob_mode="bytes") on a table with a blob column; both stay at zero, and a positive-limit blob query still returns its rows. Title updated to fix(python)!: with a BREAKING CHANGE note in the description. New head 12494a3. |
|
The remaining correction is in the release note. The trailing paragraph says the custom reranker budget changed, but 0.41.0 already used
|
There was a problem hiding this comment.
The release-note update addresses the documentation request: it now describes async zero-limit rejection and tells callers to skip execution and analyze_plan() for zero-sized pages.
The RRF-only 50-candidate floor addresses small-limit ranking starvation while preserving custom/model rerankers’ limit + offset budgets and final page slicing. Async limit(0) rejects before retrieval, byte-mode schema discovery, or plan analysis, including with a positive offset.
The accepted trade-off remains extra retrieval for small RRF queries; candidate pools can still change when limit + offset exceeds 50.
AI-assisted review. At 12494a3, an explicit async hybrid limit(0) raises before Blob schema discovery or plan analysis, with no search requests. The opening release note now describes the 0.41.0 positive-offset behavior and how callers can migrate.
BREAKING CHANGE: Executing or analyzing an async Python hybrid query with
limit(0)now raisesValueErrorbefore either search runs. In 0.41.0,offset(2).limit(0).to_arrow()returned a typed empty table after retrieval and reranking. Skip query execution andanalyze_plan()when the requested page size is zero.Refs #4758
Problem
Each side of a hybrid query fetched only
limit + offsetrows before rank fusion, so asking for fewer results could return a different top result instead of just a shorter list.Repro (lancedb 0.41.0, 300 rows, RRF reranker):
limit=1-> top id 294limit=10-> top id 269Fix
With the default RRF reranker, both hybrid paths (
LanceHybridQueryBuilder._create_query_buildersandAsyncHybridQuery._create_child_queries) now fetchmax(limit + offset, DEFAULT_HYBRID_RANK_WINDOW)rows per side, withDEFAULT_HYBRID_RANK_WINDOW = 50. The requested limit/offset window is still sliced out of the fused results as before. This implements option 1 from the issue: a fixed minimum window, no new API surface.Custom and model rerankers keep the caller's
limit + offsetbudget per side, since every candidate is sent to them (for exampleOpenaiRerankerputs all merged texts in one prompt). Callers who want a wider window for those rerankers can raiselimit(). An explicitlimit(0)never gets the floor. In the async builder it raisesValueErrorbefore schema discovery or either sub-query runs, with or without an offset, so neither Embedded nor Enterprise retrieves or scores anything. On main, asynclimit(0)was rejected by the nativekcheck only without an offset;offset(2).limit(0)fetched and reranked two rows per side and returned nothing. The sync builder still treatslimit(0)as the default of 10, as before.Trade-off: default RRF queries with small limits now fetch up to 50 rows per side, and their result orderings can change, which is the bug being fixed.
Tests
test_hybrid_query_top_result_stable_across_limitsand its async mirror (issue repro): fail on base, pass with the fixtest_hybrid_query_custom_reranker_keeps_candidate_budgetand its async mirror: a recording reranker receives 1 vector and 1 FTS candidate forlimit(1)and 5 of each foroffset(2).limit(3)test_async_hybrid_query_default_rrf_candidate_window: default RRF receives 50 of each forlimit(1)test_async_hybrid_query_empty_page_does_not_fetch_candidates(offset 0 and 2):limit(0)raises, and neither sub-query nor the reranker runstest_remote_db.py::test_async_hybrid_zero_limit_sends_no_query(offset 0 and 2):limit(0)raises and no/query/request is sent; fails on 281fdc7, passes on this headtest_remote_db.py::test_query_sync_hybridnow expectsk=50for the default RRF request withlimit(42)test_hybrid_query.py: 30 passed, 1 failed. The failure is pre-existing and unrelated (test_hybrid_query_with_stale_fixed_size_binary_prefilterneeds thelancepackage)test_wal_hybrid.py: 15 passedruff checkandruff format --checkclean on the touched filesLocal runs used the lancedb 0.41.0 wheel with this PR's
query.pydiff applied, since main needs a newer native extension than the published wheel.test_query_sync_hybridand the new remote zero-limit test were run from an extracted copy becausetest_remote_db.pyimportslancedb.graph, which 0.41.0 does not have.