siyuan-note/siyuan · error

rerank returned indices for documents

Error message

rerank returned %d indices for %d documents

What it means

After a successful Rerank call, the caller validates that the number of returned indices equals the number of submitted documents and that every index maps 1:1 to a document. This error fires when the provider returned fewer or more indices than documents, so the rerank result cannot be trusted to reorder the original list. (The DECLARED/USED metadata in the error record points at app/electron/accessibility.js and is unrelated boilerplate; the authoritative location is the SOURCE region in kernel/util/openai.go.)

Solutions

  1. Compare the returned count to the submitted count logged in the error and check whether the provider applies TopN server-side
  2. Reduce batch size and retry with fewer documents to rule out provider-side truncation
  3. Verify provider docs: it must return one result per query-document pair for rerank calls
  4. If the provider legitimately supports TopN, handle partial results at the call site instead of expecting 1:1

Example fix

// before: assumes 1:1 without controlling TopN
options.TopN = len(documents)
indices, _, err := Rerank("1", documents, options)
// after: retry once with a smaller batch if the counts disagree
if len(indices) != len(documents) {
    options.TopN = len(documents)
    indices, _, err = Rerank("1", documents[:len(documents)/2], options)
}
Defensive patterns

Strategy: validation

Validate before calling

if len(docs) == 0 || docs.length > providerMaxBatch { chunk(docs, providerMaxBatch) }

Try / catch

if err != nil && strings.Contains(err.Error(), "indices for ") {
    // counts diverged: fall back to original order
    return docs
}

Prevention

When it happens

Trigger: Calling Rerank with N documents while the provider returns an index list of length != N — e.g. the provider silently drops documents, TopN is applied server-side, or the response truncates results.

Common situations: Provider that supports TopN/cutoff semantics differing from SiYuan's expectation; extremely large document batches where the provider truncates; a misbehaving or non-conformant rerank service.

Related errors


AI-assisted analysis of siyuan-note/siyuan@9f775e8a12 (2026-09-19). Data as JSON: /api/errors/f28ad574b03a93db. Report an issue: GitHub.

Appendix: source

Thrown at kernel/util/openai.go:745

func truncateRerankDocument(document string) string {
	runes := []rune(document)
	if len(runes) > rerankDocTextMaxRunes {
		return string(runes[:rerankDocTextMaxRunes])
	}
	return document
}

// TestRerankModel 测试重排模型可用性,用极简 query+documents 发一次重排请求验证连通性与鉴权。
// 返回值:matched 表示是否连通成功,err 为请求错误(鉴权失败、网络异常、模型不存在等,原样返回便于调用方展示原因)。
func TestRerankModel(options RerankOptions) (matched bool, err error) {
	documents := []string{"a", "b"}
	options.TopN = len(documents)
	indices, _, err := Rerank("1", documents, options)
	if nil != err {
		return
	}
	if len(indices) != len(documents) {
		err = fmt.Errorf("rerank returned %d indices for %d documents", len(indices), len(documents))
		return
	}
	seen := make(map[int]bool, len(indices))
	for _, index := range indices {
		if seen[index] {
			err = fmt.Errorf("rerank returned duplicate index %d", index)
			return
		}
		seen[index] = true
	}
	matched = true
	return
}

// PrepareModelImage 校验并按需缩放图片,尽量保留多模态模型支持的原始格式和图片质量。
func PrepareModelImage(data []byte, maxBytes, maxPixels, maxEdge int) (PreparedImage, error) {
	if len(data) == 0 {
		return PreparedImage{}, errors.New("image data is empty")

View on GitHub (pinned to 9f775e8a12)