Skip to content

fix: sort SentenceWindowRetriever context before merging text - #12976

Merged
bogdankostic merged 7 commits into
deepset-ai:mainfrom
LindseyZ1205:fix/sentence-window-retriever-context-order
Oct 2, 2026
Merged

bogdankostic merged 7 commits into
deepset-ai:mainfrom
LindseyZ1205:fix/sentence-window-retriever-context-order

Conversation

@LindseyZ1205

@LindseyZ1205 LindseyZ1205 commented Sep 26, 2026 •

Copy link
Copy Markdown
Contributor

Related Issues

Proposed Changes:

The shared _assemble_context path merges context text before sorting documents by split_id. When chunks have no split_idx_start (for example chunks from MarkdownHeaderSplitter), merge_documents_text concatenates them in the Document Store's return order, while context_documents is sorted. Sort once before assembling both outputs. This preserves the upstream batched retrieval used by both run and run_async.

How did you test it?

  • Existing shuffled custom-field tests now assert the exact context text and split ID order for both sync and async calls. On the current upstream logic, these two regression tests fail; with this fix, the two retriever unit-test files pass (37 passed, 4 integration cases deselected).
  • hatch run fmt-check and hatch run test:types pass for the changed source and two test files; git diff --check passes.

Notes for the reviewer

The run() docstring says context_documents are sorted by the split_idx_start meta field, but the code sorts by split_id_meta_field. I left it unchanged to keep this PR focused, happy to update it here if you prefer.

AI assistance: Claude Code assisted with the original bug investigation and patch. Codex assisted with adapting the fix to upstream batched retrieval, resolving the merge conflict, and running the checks above.

Checklist

  • I have read the contributors guidelines and the code of conduct.
  • I have updated the related issue with new insights and changes.
  • I have added unit tests and updated the docstrings.
  • I've used one of the conventional commit types for my PR title: fix:, feat:, build:, chore:, ci:, docs:, style:, refactor:, perf:, test: and added ! in case the PR includes breaking changes.
  • I have documented my code.
  • I have added a release note file, following the contributors guidelines.
  • I have run pre-commit hooks and fixed any issue.

🤖 Generated with Claude Code

The context documents were merged into `context_windows` before being
sorted by `split_id`. When chunks have no `split_idx_start` (for example
chunks from MarkdownHeaderSplitter), `merge_documents_text` concatenates
them in the order returned by the Document Store, so the context text
could be scrambled while `context_documents` was correctly sorted.

Sort first and merge the sorted list, in both run and run_async.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@LindseyZ1205
LindseyZ1205 requested a review from a team as a code owner September 26, 2026 20:18
@LindseyZ1205
LindseyZ1205 requested review from bogdankostic and removed request for a team September 26, 2026 20:18
@vercel

vercel Bot commented Sep 26, 2026

Copy link
Copy Markdown
Contributor

@LindseyZ1205 is attempting to deploy a commit to the deepset Team on Vercel.

A member of the Team first needs to authorize it.

@github-actions github-actions Bot added the type:documentation Improvements on the docs label Oct 2, 2026
@github-actions

github-actions Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/retrievers
  sentence_window_retriever.py
Project Total  

This report was generated by python-coverage-comment-action

bogdankostic and others added 5 commits October 2, 2026 12:45
The run/run_async docstrings said context_documents are sorted by
split_idx_start. They are sorted by split_id_meta_field within each
window, grouped in the order of retrieved_documents, and documents
shared by overlapping windows appear once per window.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
The Output variables row said context_documents are ordered by
split_idx_start. They are grouped by retrieved document and ordered by
split_id within each window. Updated docs/ and the current stable
version-3.3 page.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Document the metadata the component relies on (source_id, split_id,
optional split_idx_start), which splitters produce it, what happens
when it is missing, and what the two outputs contain.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
Bring back the technique name and the indexing/retrieval workflow that
the previous overview rewrite dropped.

Co-Authored-By: Claude Opus 5.5 (1M context) <noreply@anthropic.com>
@vercel

vercel Bot commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
haystack-docs Ready Ready Preview Oct 2, 2026 11:43am UTC

Request Review

@bogdankostic bogdankostic left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for this PR and congrats on your first contribution to Haystack! 🎉

As you suggested in the PR description, I fixed the run/run_async docstrings and updated the documentation page.

@bogdankostic
bogdankostic merged commit 414e6c7 into deepset-ai:main Oct 2, 2026
25 checks passed

This branch was successfully deployed

1 active deployment
Preview — 6037a8e9 Deployed Oct 2, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

topic:tests type:documentation Improvements on the docs

Projects

None yet

Development

Successfully merging this pull request may close these issues.

SentenceWindowRetriever returns context_windows in Document Store order when chunks have no split_idx_start

2 participants