{"record":{"id":"f28ad574b03a93db","repo":"siyuan-note/siyuan","slug":"rerank-returned-d-indices-for-d-documents","errorCode":null,"errorMessage":"rerank returned %d indices for %d documents","messagePattern":"rerank returned (.+?) indices for (.+?) documents","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"kernel/util/openai.go","lineNumber":605,"sourceCode":"\nfunc truncateRerankDocument(document string) string {\n\trunes := []rune(document)\n\tif len(runes) > rerankDocTextMaxRunes {\n\t\treturn string(runes[:rerankDocTextMaxRunes])\n\t}\n\treturn document\n}\n\n// TestRerankModel 测试重排模型可用性，用极简 query+documents 发一次重排请求验证连通性与鉴权。\n// 返回值：matched 表示是否连通成功，err 为请求错误（鉴权失败、网络异常、模型不存在等，原样返回便于调用方展示原因）。\nfunc TestRerankModel(apiKey, apiBaseURL, model string, timeout int) (matched bool, err error) {\n\tdocuments := []string{\"a\", \"b\"}\n\tindices, _, err := Rerank(\"1\", documents, apiKey, apiBaseURL, model, len(documents), timeout)\n\tif nil != err {\n\t\treturn\n\t}\n\tif len(indices) != len(documents) {\n\t\terr = fmt.Errorf(\"rerank returned %d indices for %d documents\", len(indices), len(documents))\n\t\treturn\n\t}\n\tseen := make(map[int]bool, len(indices))\n\tfor _, index := range indices {\n\t\tif seen[index] {\n\t\t\terr = fmt.Errorf(\"rerank returned duplicate index %d\", index)\n\t\t\treturn\n\t\t}\n\t\tseen[index] = true\n\t}\n\tmatched = true\n\treturn\n}\n\n// PrepareModelImage 校验并按需缩放图片，尽量保留多模态模型支持的原始格式和图片质量。\nfunc PrepareModelImage(data []byte, maxBytes, maxPixels, maxEdge int) (PreparedImage, error) {\n\tif len(data) == 0 {\n\t\treturn PreparedImage{}, errors.New(\"image data is empty\")","sourceCodeStart":587,"sourceCodeEnd":623,"githubUrl":"https://github.com/siyuan-note/siyuan/blob/251596fc0de2f9528c00c224252fd073a99973f4/kernel/util/openai.go#L587-L623","documentation":"TestRerankModel sends two trivial documents and expects exactly two indices back. If the count differs, the provider is applying a default top_n (returning fewer), returning scores without indices, or returning out-of-range indices that Rerank's loop filters out. The probe treats such a provider as non-conformant for the connectivity test.","triggerScenarios":"Rerank provider that caps results at a default top_n=1; provider returning scores without an index field; provider returning 1-based indices (filtered out as out-of-range for a 2-doc input).","commonSituations":"Provider whose default top_n is 1 and ignores the top_n=len(docs) hint; non-standard response schema; provider that omits low-relevance documents.","solutions":["Confirm the provider implements the standard {results:[{index,relevance_score}]} schema with 0-based indices.","Inspect the raw rerank response to see whether indices are 0-based and complete.","Verify the model name actually supports rerank.","If non-conformant, use a different provider or configure rerank manually without relying on the probe."],"exampleFix":null,"handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"if ok, err := util.TestRerankModel(key, endpoint, model, 30); err != nil || !ok {\n    log.Warnf(\"rerank provider may be non-conformant: %s; falling back to no rerank\", err)\n}","preventionTips":["Prefer providers that return all documents with 0-based indices","Inspect the rerank response schema before adopting a provider","Treat rerank as optional and degrade to original ranking on failure"],"tags":["ai","rerank","go","validation"],"backgroundTag":null,"analyzedSha":"251596fc0de2f9528c00c224252fd073a99973f4","analyzedAt":"2026-08-12T21:18:37.123Z","schemaVersion":2},"datasetVersion":"2026-08-12T23:17:12.415Z"}