Repository navigation
fix(api): send a rerank window that covers top_k in Dify retrieval - #21427
Conversation
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]>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info
📝 Walkthrough
Merge Risk: ⚪ Minimal · up to
|
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]>
|
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. |
|
LGTM. |
Summary
The Go Dify external knowledge endpoint (
/api/v1/dify/retrieval) fails for everytop_kabove 64, and for the defaulttop_kof 1024.DifyRetrievalHandler.RetrievalsendsPageSize: top_kand noRerankCandidatesCount.RetrievalService.Retrievalthen sets the window to 64 and rejectsPage > 64 / PageSize. I called the realRetrievalService.Retrievalwith the request the handler builds onmain:top_kmainrerank_candidates_count(64) must be greater than or equal to page(1) * page_size(1024)This PR sends
RerankCandidatesCount: max(top_k, 64). Atop_kabove 64 gets a window that holds it. Atop_kof 64 or less keeps the window of 64, so the engine fetches the same candidates as before. The handler also rejects atop_koutside 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, POSTtop_k100, GETtop_k=200and POSTtop_k10. It checksPageSizeandRerankCandidatesCounton the request that reaches the retrieval service.TestDifyRetrieval_TopKOutOfRange:top_k0, -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_kas the window, thetop_k10 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=0builds ofinternal/handlerfail onmainsince #21250, becauseinternal/rag/decisionheadhas no non-cgoSession. 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.xhas the same defect.dify_retrieval_api.pypassespage_size=topand keeps the defaultrerank_candidates_count=64, sotop_k65, 100 and 1024 raise inDealer.retrieval.