Repository navigation
fix(api): honor explicit zero top_k in dataset retrieval paths - #42980
Merged
crazywoola merged 2 commits intoOct 30, 2026
Merged
Conversation
Contributor
Pyrefly Type Coverage
|
1 task done
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #42980 +/- ##
==========================================
- Coverage 88.07% 88.05% -0.02%
==========================================
Files 5308 5151 -157
Lines 327512 323368 -4144
Branches 66277 65169 -1108
==========================================
- Hits 288443 284733 -3710
+ Misses 33685 33251 -434
Partials 5384 5384
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
The direct-retrieval path in DatasetRetrieval._retriever read the dataset's stored retrieval model with 'or 4', silently turning an explicit top_k=0 into the default. The SINGLE-retrieval tool path reads the same stored value as-is, and the caller-level zero is honored by the 'if top_k > 0' guard, so only a missing entry should fall back to 4.
Contributor
Author
|
Hi — gentle ping for review; open for 18 days with CI green (37 pass / 14 skip). Four self-contained falsy-zero top_k fixes — happy to address any feedback. Thanks! |
crazywoola
approved these changes
Oct 30, 2026
1 task done
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #42981
DatasetRetrieveConfigEntity.top_kis typedtop_k: int | None = None, so an explicittop_k=0("retrieve nothing from knowledge") is legal input. The codebase itself treats it that way in several places:DatasetRetrieval._fetch_model_configMULTIPLE branch passesretrieve_config.top_k or 0intomultiple_retrieve(api/core/rag/retrieval/dataset_retrieval.py:501)knowledge_retrieval_inner_servicepassestop_k=request.retrieval.top_k or 0(api/services/knowledge_retrieval_inner_service.py:135)DatasetMultiRetrieverTool._runalready guardsif self.top_k > 0before retrieving, so a zero tool-level top_k is a designed statetop_k = retrieval_model_config["top_k"]into_dataset_retriever_tool)But the retrieval paths reading the same values replace an explicit 0 with the default:
top_k=retrieve_config.top_k or 4when buildingDatasetMultiRetrieverTool.from_dataset(api/core/rag/retrieval/dataset_retrieval.py:1392)top_k=retrieval_model.get("top_k") or 4for both the ECONOMY and high-quality per-dataset retrieval (api/core/tools/utils/dataset_retriever/dataset_multi_retriever_tool.py:181, 192)top_k=retrieval_model.get("top_k") or 4inDatasetRetrieval._retriever's high-quality branch, reading the same stored per-dataset retrieval model (api/core/rag/retrieval/dataset_retrieval.py:1207)so an app/dataset configured with
top_k=0still retrieves 4 documents. This patch distinguishes omitted (None) from zero in all four places, keeping the existing default-4 behavior forNone/absent while honoring an explicit 0 — the same explicit-zero semantics already reported for annotation reply score thresholds in #42639.Checks
ruff check+ruff format --checkclean with the repository-pinned ruff 0.16.6 on the touched files.pytestcould not run:uv sync --project apifails to buildchroma-hnswlibin this environment (same limitation as previous contributions here). The existing api unit-test suite runs in CI and guards the touched modules; the changed expressions only differ from the previous behavior for an explicittop_k=0(None/absent keeps the default 4).