← Back to DAGDA | Support | Source: https://github.com/lancedb/lancedb/pull/4772
Skip to content

fix(python)!: fetch minimum rank-fusion window per side in hybrid search - #4772

Merged
Xuanwo merged 9 commits into
lancedb:mainfrom
shadowmodder:shadowmodder/hybrid/rank-fusion-window
Nov 30, 2026
Merged

Xuanwo merged 9 commits into
lancedb:mainfrom
shadowmodder:shadowmodder/hybrid/rank-fusion-window

Conversation

@shadowmodder

@shadowmodder shadowmodder commented Oct 31, 2026 •

Copy link
Copy Markdown
Contributor

BREAKING CHANGE: Executing or analyzing an async Python hybrid query with limit(0) now raises ValueError before 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 and analyze_plan() when the requested page size is zero.

Refs #4758

Problem

Each side of a hybrid query fetched only limit + offset rows 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 294
  • limit=10 -> top id 269

Fix

With the default RRF reranker, both hybrid paths (LanceHybridQueryBuilder._create_query_builders and AsyncHybridQuery._create_child_queries) now fetch max(limit + offset, DEFAULT_HYBRID_RANK_WINDOW) rows per side, with DEFAULT_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 + offset budget per side, since every candidate is sent to them (for example OpenaiReranker puts all merged texts in one prompt). Callers who want a wider window for those rerankers can raise limit(). An explicit limit(0) never gets the floor. In the async builder it raises ValueError before schema discovery or either sub-query runs, with or without an offset, so neither Embedded nor Enterprise retrieves or scores anything. On main, async limit(0) was rejected by the native k check only without an offset; offset(2).limit(0) fetched and reranked two rows per side and returned nothing. The sync builder still treats limit(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_limits and its async mirror (issue repro): fail on base, pass with the fix
  • test_hybrid_query_custom_reranker_keeps_candidate_budget and its async mirror: a recording reranker receives 1 vector and 1 FTS candidate for limit(1) and 5 of each for offset(2).limit(3)
  • test_async_hybrid_query_default_rrf_candidate_window: default RRF receives 50 of each for limit(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 runs
  • test_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 head
  • test_remote_db.py::test_query_sync_hybrid now expects k=50 for the default RRF request with limit(42)
  • test_hybrid_query.py: 30 passed, 1 failed. The failure is pre-existing and unrelated (test_hybrid_query_with_stale_fixed_size_binary_prefilter needs the lance package)
  • test_wal_hybrid.py: 15 passed
  • ruff check and ruff format --check clean on the touched files

Local runs used the lancedb 0.41.0 wheel with this PR's query.py diff applied, since main needs a newer native extension than the published wheel. test_query_sync_hybrid and the new remote zero-limit test were run from an extracted copy because test_remote_db.py imports lancedb.graph, which 0.41.0 does not have.

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Claude Code Review

This pull request is from a fork — automated review is disabled. A repository maintainer can comment @claude review to run a one-time review.

@github-actions github-actions Bot added bug Something isn't working Python Python SDK labels Oct 31, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 31, 2026
@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 31, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 31, 2026
@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Oct 31, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Oct 31, 2026

@SIDDARTHAREDDY8 SIDDARTHAREDDY8 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

  1. Both execution paths get the floor, but only one gets a behavioral test. The sync path (LanceHybridQueryBuilder._run_hybrid via _create_query_builders) is covered by the new test_hybrid_query_top_result_stable_across_limits, which uses the sync table.search(query_type="hybrid") API. The async path (AsyncHybridQuery._create_child_queries) is only asserted on indirectly, by the explain-plan test expecting fetch=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?

  2. The window is not configurable. DEFAULT_HYBRID_RANK_WINDOW = 50 is 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?

  3. The regression test's sensitivity is unverified from the diff alone. test_hybrid_query_top_result_stable_across_limits uses a fixed seed and asserts top_id(1) == top_id(3) == top_id(10). I did not run it, so I cannot tell whether this fixture actually diverges under the old limit + offset fetching; 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.

  4. Small nit: import random landed at line 15 of test_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.
@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 2, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 2, 2026
@shadowmodder

Copy link
Copy Markdown
Contributor Author

Thanks for the thorough code-read. Addressing each point:

  1. Async stability test — fair catch, and you're right the explain-plan assertion wouldn't catch a ranking regression on the async path. Added test_async_hybrid_query_top_result_stable_across_limits in the latest commit: same 300-row seeded fixture as the sync test, run through table.query().nearest_to(...).nearest_to_text(...).to_list(), asserting top_id(1) == top_id(3) == top_id(10). Verified it fails with the floor removed and passes with it, same as the sync version.

  2. Fixed window is deliberate, not an oversight. The floor exists so rank fusion sees a stable candidate pool; making it user-configurable would let callers silently reintroduce the exact instability this fixes (Hybrid search: the top result changes with limit, because each side fetches only limit + offset rows before fusing #4758). If a use case with an expensive reranker needs a smaller window, that's worth a separate discussion with a concrete proposal — but I didn't want to expand the API surface in a bugfix.

  3. Sensitivity confirmed locally: test_hybrid_query_top_result_stable_across_limits fails on the base commit (old limit + offset fetching) and passes with the fix; same check done for the new async test. (Ran against the released 0.41.0 build with the repo's query.py overlaid — no Rust toolchain in my env — so the base/fix A/B is clean.)

  4. Nit taken — import random moved to the stdlib group in the same commit.

@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 2, 2026
@Xuanwo

Xuanwo commented Nov 27, 2026

Copy link
Copy Markdown
Member

On 6b208a1f, default to_arrow() rejects an explicit limit(0) before retrieval or reranking. Two other entry points still execute work:

  • On a table with no Blob columns, to_arrow(blob_mode="bytes") sends a vector /query/ request with k=0 during schema discovery, then raises ValueError.
  • offset(2).limit(0).analyze_plan() executes both legs on Embedded, with two rows reported for each. The Enterprise client sends two /analyze_plan/ requests with k=2.

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, offset(2).limit(0).to_arrow() returned a typed empty table. An omitted or zero offset raised ValueError. Since this PR makes the positive-limit contract uniform, please use fix(python)!: in the title and add a BREAKING CHANGE: note telling callers to skip query execution when the requested page size is zero.

@Xuanwo Xuanwo added the breaking-change A breaking change in our public APIs label Nov 27, 2026
@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 27, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 27, 2026
…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>
@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 27, 2026
@shadowmodder shadowmodder changed the title fix(python): fetch minimum rank-fusion window per side in hybrid search fix(python)!: fetch minimum rank-fusion window per side in hybrid search Nov 27, 2026
@shadowmodder

Copy link
Copy Markdown
Contributor Author

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.

lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 27, 2026
@Xuanwo

Xuanwo commented Nov 27, 2026

Copy link
Copy Markdown
Member

12494a34 fixes both execution paths. With the candidate Python code and the beta.28 native extension, the Enterprise loopback recorded zero /query/ and /analyze_plan/ requests across 72 zero-limit cases. All three new regression cases failed on the previous head and passed on this one.

The remaining correction is in the release note. The trailing paragraph says the custom reranker budget changed, but 0.41.0 already used limit + offset per side. Please replace that paragraph with the following at the start of the PR body, keeping Refs #4758 below it so the issue stays open for Rust and TypeScript.

BREAKING CHANGE: Executing or analyzing an async Python hybrid query with limit(0) now raises ValueError before 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 and analyze_plan() when the requested page size is zero.

@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 27, 2026
lancedb-gatekeeper[bot]

This comment was marked as outdated.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 27, 2026
@shadowmodder

Copy link
Copy Markdown
Contributor Author

Replaced the trailing release-note paragraph with your BREAKING CHANGE text at the start of the description, with Refs #4758 kept below it. No code change; head is still 12494a3.

@lancedb-gatekeeper lancedb-gatekeeper Bot removed K-risk Latest Gatekeeper recommendation includes a non-blocking risk. K-approved Latest Gatekeeper recommendation permits acceptance. labels Nov 27, 2026

@lancedb-gatekeeper lancedb-gatekeeper Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Gate recommendation: approve with a non-blocking risk.

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.

@lancedb-gatekeeper lancedb-gatekeeper Bot added K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. labels Nov 27, 2026
@Xuanwo
Xuanwo dismissed their stale review November 27, 2026 23:40

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.

@Xuanwo
Xuanwo merged commit 97c8865 into lancedb:main Nov 30, 2026
22 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

breaking-change A breaking change in our public APIs bug Something isn't working K-approved Latest Gatekeeper recommendation permits acceptance. K-risk Latest Gatekeeper recommendation includes a non-blocking risk. Python Python SDK

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants