Skip to content

fix(api): send a rerank window that covers top_k in Dify retrieval - #21427

Merged
wangq8 merged 2 commits into
infiniflow:mainfrom
devYRPauli:fix/go-dify-retrieval-rerank-window
Oct 27, 2026
Merged

wangq8 merged 2 commits into
infiniflow:mainfrom
devYRPauli:fix/go-dify-retrieval-rerank-window

Conversation

@devYRPauli

Copy link
Copy Markdown
Contributor

Summary

The Go Dify external knowledge endpoint (/api/v1/dify/retrieval) fails for every top_k above 64, and for the default top_k of 1024.

DifyRetrievalHandler.Retrieval sends PageSize: top_k and no RerankCandidatesCount. RetrievalService.Retrieval then sets the window to 64 and rejects Page > 64 / PageSize. I called the real RetrievalService.Retrieval with the request the handler builds on main:

top_k main this branch
not set (1024) error: rerank_candidates_count(64) must be greater than or equal to page(1) * page_size(1024) passes the window check
100 error, same message passes
65 error, same message passes
10 passes, window 64 passes, window 64

This PR sends RerankCandidatesCount: max(top_k, 64). A top_k above 64 gets a window that holds it. A top_k of 64 or less keeps the window of 64, so the engine fetches the same candidates as before. The handler also rejects a top_k outside 1 to 1024 with 400, which is the range the Python endpoint accepts.

Tests in internal/handler/dify_retrieval_handler_test.go:

  • TestDifyRetrieval_RerankWindowCoversTopK: the default, POST top_k 100, GET top_k=200 and POST top_k 10. It checks PageSize and RerankCandidatesCount on the request that reaches the retrieval service.
  • TestDifyRetrieval_TopKOutOfRange: top_k 0, -1 and 1025 return 400, and retrieval does not run.

Each change has a test that fails without it. With no window, all four cases fail. With top_k as the window, the top_k 10 case fails with window 10. With no range check, the out-of-range test fails.

go test ./internal/handler/ ./internal/service/nlp/ passes. CGO_ENABLED=0 builds of internal/handler fail on main since #21250, because internal/rag/decisionhead has no non-cgo Session. I ran the tests with a local non-cgo stub of that type, which is not part of this PR.

The Python endpoint on 0.27.x has the same defect. dify_retrieval_api.py passes page_size=top and keeps the default rerank_candidates_count=64, so top_k 65, 100 and 1024 raise in Dealer.retrieval.

devYRPauli and others added 2 commits October 23, 2026 10:09
The Dify handler set PageSize to top_k, or to 1024 when the request
sent no top_k, but never set RerankCandidatesCount. The service then
defaulted the window to 64 and rejected every page size above 64, so
the default request and any top_k above 64 returned HTTP 500.

Send PageSize as the rerank window, as the MCP handler does, and
reject a top_k outside 1..1024 with HTTP 400, as the Python endpoint
does.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
A top_k below 64 set the window to top_k, so the engine fetched fewer
candidates than before and a score threshold could return fewer
records. Use max(top_k, 64), which keeps the service default for small
values.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@coderabbitai

coderabbitai Bot commented Oct 23, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: 3946fdec-1d92-4990-86a8-419793d48455
📥 Commits

Reviewing files that changed from the base of the PR and between a55dcce and e427c79.

📒 Files selected for processing: 2
  • internal/handler/dify_retrieval_handler.go
  • internal/handler/dify_retrieval_handler_test.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

📝 Walkthrough

The retrieval handler now rejects top_k values outside 1–1024 and sets RerankCandidatesCount to at least 64. Tests cover POST and GET requests, pagination values, and invalid inputs.

Changes: Retrieval top-k handling

Layer / File(s) Summary
Validate top-k and build retrieval request
internal/handler/dify_retrieval_handler.go, internal/handler/dify_retrieval_handler_test.go
The handler returns HTTP 400 when top_k is below 1 or above 1024. It sets RerankCandidatesCount to the greater of the page size and 64. Tests check default, above-64, below-64, and invalid values for POST and GET requests.

Priority: ➖ Normal

Estimated code review effort: 2 (Simple) | ~10 minutes

Change: Bug fix

Suggested labels: 🐞 bug, go, 🧪 test

Suggested reviewers: buua436

Merge Risk: ⚪ Minimal · up to e427c

The requested top_k can flow through retrieval without a smaller candidate limit. No issue requiring a fix before merge was identified.

Pre-merge checks: Passed 4 · Failed 1

Failed Failed checks: 1 warning

Check name Status Explanation Resolution
Docstring Coverage Warning Docstring coverage is 0.00% which is insufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 3 functions across 2 files. Write docstrings for the functions missing them to satisfy the coverage threshold.
Passed Passed checks: 4 passed
Check name Status Explanation
Title check Passed The title clearly and concisely describes the primary change: increasing the Dify retrieval rerank window to cover top_k.
Description check Passed The description includes the required Summary section and clearly explains the problem, implementation, test coverage, validation results, and known build limitation.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit checks the top-k gate,
Then counts rerank candidates straight.
Sixty-four is the floor,
Bigger pages count more,
And invalid requests wait.

Comment @coderabbitai help to get the list of available commands.

devYRPauli added a commit to devYRPauli/devYRPauli that referenced this pull request Oct 23, 2026
Open: 52 -> 54 (infiniflow/ragflow#21427 and #21428). Merged 148 across
38 projects and upstream issues 20 are unchanged. No table row moved.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
@Harsh23Kashyap

Copy link
Copy Markdown
Collaborator

Verified the chain on main: DifyRetrievalHandler sends PageSize: top_k with no RerankCandidatesCount, RetrievalService defaults the window to 64 when it is nil, and then rejects any page * page_size above the window. So every top_k over 64, including the endpoint's own default of 1024, fails before search even runs. Sending max(top_k, 64) keeps the small-top_k ranking identical while covering the large ones. No concerns.

@wangq8

wangq8 commented Oct 27, 2026

Copy link
Copy Markdown
Collaborator

LGTM.

@wangq8 wangq8 added the ci Continue Integration label Oct 27, 2026
@wangq8
wangq8 merged commit 3d90503 into infiniflow:main Oct 27, 2026
7 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants