fix(rerank): filter empty docs before rerank API; concurrent read_batch
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.
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 themesSub-PR theme: fix(rerank): filter empty docs and add top_n parameterRelevant files:
Sub-PR theme: perf: concurrent read_batch with asyncio.gatherRelevant files:
|
โก Recommended focus areas for reviewBackward Incompatibility
|
PR Code Suggestions โจ
No code suggestions found for the PR.
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!