Skip to content

fix(api): honor explicit zero top_k in dataset retrieval paths - #42980

Merged
crazywoola merged 2 commits into
langgenius:mainfrom
yetuge:fix/honor-explicit-zero-top-k
Oct 30, 2026
Merged

crazywoola merged 2 commits into
langgenius:mainfrom
yetuge:fix/honor-explicit-zero-top-k

Conversation

@yetuge

@yetuge yetuge commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Fixes #42981

DatasetRetrieveConfigEntity.top_k is typed top_k: int | None = None, so an explicit top_k=0 ("retrieve nothing from knowledge") is legal input. The codebase itself treats it that way in several places:

  • DatasetRetrieval._fetch_model_config MULTIPLE branch passes retrieve_config.top_k or 0 into multiple_retrieve (api/core/rag/retrieval/dataset_retrieval.py:501)
  • knowledge_retrieval_inner_service passes top_k=request.retrieval.top_k or 0 (api/services/knowledge_retrieval_inner_service.py:135)
  • DatasetMultiRetrieverTool._run already guards if self.top_k > 0 before retrieving, so a zero tool-level top_k is a designed state
  • the SINGLE-retrieval tool path reads the dataset's stored top_k as-is with no fallback (top_k = retrieval_model_config["top_k"] in to_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 4 when building DatasetMultiRetrieverTool.from_dataset (api/core/rag/retrieval/dataset_retrieval.py:1392)
  • top_k=retrieval_model.get("top_k") or 4 for 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 4 in DatasetRetrieval._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=0 still retrieves 4 documents. This patch distinguishes omitted (None) from zero in all four places, keeping the existing default-4 behavior for None/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 --check clean with the repository-pinned ruff 0.16.6 on the touched files.
  • Local pytest could not run: uv sync --project api fails to build chroma-hnswlib in 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 explicit top_k=0 (None/absent keeps the default 4).

@github-actions github-actions Bot added the size:S This PR changes 10-29 lines, ignoring generated files. label Sep 26, 2026
@ghfind-review ghfind-review Bot added the review: medium ghfind author score; see https://ghfind.com label Sep 26, 2026
@github-actions

Copy link
Copy Markdown
Contributor

Pyrefly Type Coverage

Metric Base PR Delta
Type coverage 67.36% 67.36% 0.00%
Strict coverage 66.98% 66.98% 0.00%
Typed symbols 51,304 51,304 0
Untyped symbols 25,003 25,003 0
Modules 3562 3562 0

@codecov

codecov Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 88.05%. Comparing base (414cce2) to head (0b5e2ca).
⚠️ Report is 2 commits behind head on main.

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              
Flag Coverage Δ
api 87.89% <100.00%> (-0.01%) ⬇️
cli ?

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

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.
@yetuge yetuge changed the title fix(api): honor explicit zero top_k in dataset retrieval tool paths fix(api): honor explicit zero top_k in dataset retrieval paths Sep 26, 2026
@yetuge

yetuge commented Oct 14, 2026

Copy link
Copy Markdown
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
crazywoola added this pull request to the merge queue Oct 30, 2026
Merged via the queue into langgenius:main with commit 7904c8a Oct 30, 2026
53 checks passed
@laipz8200 laipz8200 mentioned this pull request Nov 3, 2026
1 task done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

review: medium ghfind author score; see https://ghfind.com size:S This PR changes 10-29 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Explicit top_k=0 in app/dataset config is silently replaced by 4 in the tool-based retrieval path

2 participants