OpenViking icon indicating copy to clipboard operation
OpenViking copied to clipboard

fix(rerank): filter empty docs before rerank API; concurrent read_batch

Open njuboy11 opened this issue 5 months ago โ€ข 2 comments

Problem

Two issues causing slow search (up to 8.3s worst-case):

1. Reranker fails when documents contain empty strings

SiliconFlow API silently drops empty documents from results. When one of N documents has an empty string, the API returns N-1 results. The client detects the count mismatch and falls back to vector scores, wasting the API call entirely (approximately 180ms per call, plus worse result quality).

Log evidence:

[OpenAIRerankClient] Unexpected rerank result length: expected=24 actual=23
[HierarchicalRetriever] Invalid rerank result, fallback to vector scores

2. read_batch reads files sequentially

VikingFS.read_batch() loops through URIs sequentially with await self.abstract() for each file. For 24 candidates each with up to 5 relations, that is up to 120 serial file reads.

Fix

openai_rerank.py

  • Filter out empty/blank documents before sending to the rerank API
  • Track original indices with non_empty_indices list
  • Map API results back to original document indices, with 0.0 for empty docs
  • Also add top_n parameter to prevent SiliconFlow default top_n=12 truncation

viking_fs.py

  • Rewrite read_batch() to use asyncio.gather() for concurrent I/O
  • Add Tuple to typing imports

Test Results

Before:

[OV-TIMING] global_search: 4632ms
[OV-TIMING] recursive_search: 334ms
[OV-TIMING] convert: 3385ms
Total worst: approximately 8.3s

After:

[OV-TIMING] global_search: 212ms
[OV-TIMING] recursive_search: 220ms
[OV-TIMING] convert: 4ms
Total: approximately 436ms

Reranker now works correctly - no more "Invalid rerank result" warnings.

njuboy11 avatar Jun 15 '26 11:06 njuboy11

PR Reviewer Guide ๐Ÿ”

Here are some key observations to aid the review process:

โฑ๏ธ Estimated effort to review: 2 ๐Ÿ”ต๐Ÿ”ตโšชโšชโšช
๐Ÿ… Score: 80
๐Ÿงช No relevant tests
๐Ÿ”’ No security concerns identified
โœ… No TODO sections
๐Ÿ”€ Multiple PR themes

Sub-PR theme: fix(rerank): filter empty docs and add top_n parameter

Relevant files:

  • openviking/models/rerank/openai_rerank.py

Sub-PR theme: perf: concurrent read_batch with asyncio.gather

Relevant files:

  • openviking/storage/viking_fs.py

โšก Recommended focus areas for review

Backward Incompatibility

read_batch() now includes URIs that failed to read in the result dict with empty string value, whereas the old implementation omitted them entirely. This changes API contract and may break existing callers that expect only successfully read URIs in the output.

async def read_batch(
    self, uris: List[str], level: str = "l0", ctx: Optional[RequestContext] = None
) -> Dict[str, str]:
    """Batch read content from multiple URIs concurrently."""
    async def _read_one(uri: str) -> Tuple[str, str]:
        try:
            if level == "l0":
                content = await self.abstract(uri, ctx=ctx)
            elif level == "l1":
                content = await self.overview(uri, ctx=ctx)
            else:
                content = ""
            return uri, content
        except Exception:
            return uri, ""

    tasks = [_read_one(uri) for uri in uris]
    gathered = await asyncio.gather(*tasks)
    return {uri: content for uri, content in gathered}
Error Handling Regression

The new read_batch() swallows all exceptions without logging, making it impossible to diagnose why individual URI reads failed. The old implementation at least didn't mask failures completely (by omitting failed URIs).

except Exception:
    return uri, ""

github-actions[bot] avatar Jun 15 '26 11:06 github-actions[bot]

PR Code Suggestions โœจ

No code suggestions found for the PR.

github-actions[bot] avatar Jun 15 '26 11:06 github-actions[bot]

Closing in favour of two split PRs (one fix per PR is easier to review and merge independently):

  • #2926 โ€” fix(rerank): filter empty docs before rerank API call
  • #2927 โ€” fix(viking_fs): concurrent read_batch via asyncio.gather

Both fixes pass their own regression tests. Thanks for the earlier review consideration!

njuboy11 avatar Jul 01 '26 10:07 njuboy11