Skip to content

fix: DocumentCleaner wiped the middle page of a 3-page document - #11921

Merged
bogdankostic merged 2 commits into
deepset-ai:mainfrom
otiscuilei:fix/document-cleaner-3-page-wipe
Jul 9, 2026
Merged

fix: DocumentCleaner wiped the middle page of a 3-page document#11921
bogdankostic merged 2 commits into
deepset-ai:mainfrom
otiscuilei:fix/document-cleaner-3-page-wipe

Conversation

@otiscuilei

Copy link
Copy Markdown
Contributor

Who hits this: anyone running DocumentCleaner with remove_repeated_substrings=True over documents that use form-feed (\f) page separators and have exactly three pages — a common shape for short PDFs. The unique middle page silently disappears from the cleaned output.

Root cause: header/footer detection derives a common ngram only from the pages strictly between the first and the last (pages[n_first_pages_to_ignore:-n_last_pages_to_ignore]). For a 3-page document that slice is a single page. _find_longest_common_ngram was written to guard only the empty case, so with one sequence reduce(set.intersection, ...) returned that page's own ngram set and picked its longest ngram. That ngram was then treated as a repeated header/footer and str.replace'd out of every page, wiping the middle page's content.

Fix: require at least two sequences before deriving a "common" ngram. A single sequence shares no ngram with any other, so there is nothing to remove. Genuine repeated headers/footers (documents with two or more comparable inner pages) are still detected and stripped exactly as before.

Before/after with the public component API:

from haystack import Document
from haystack.components.preprocessors import DocumentCleaner

cleaner = DocumentCleaner(
    remove_empty_lines=False,
    remove_extra_whitespaces=False,
    remove_repeated_substrings=True,
)
text = "PAGE ONE\fThe quick brown fox jumps high\fPAGE THREE"
out = cleaner.run(documents=[Document(content=text)])["documents"][0]
print(out.content)

# Before: "PAGE ONE\f\fPAGE THREE"          # middle page wiped
# After:  "PAGE ONE\fThe quick brown fox jumps high\fPAGE THREE"   # all pages preserved

Tests: adds a regression test that runs the 3-page document through DocumentCleaner and asserts the full content round-trips unchanged — all three pages preserved in order, not merely the middle one. Verified it fails on the pre-fix code and passes with the fix. Includes a release note.

@otiscuilei
otiscuilei requested a review from a team as a code owner July 8, 2026 19:32
@otiscuilei
otiscuilei requested review from bogdankostic and removed request for a team July 8, 2026 19:32
@vercel

vercel Bot commented Jul 8, 2026

Copy link
Copy Markdown

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

A member of the Team first needs to authorize it.

@CLAassistant

CLAassistant commented Jul 8, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@otiscuilei
otiscuilei force-pushed the fix/document-cleaner-3-page-wipe branch 2 times, most recently from a06e286 to a53b74d Compare July 9, 2026 05:50
@github-actions github-actions Bot added the type:documentation Improvements on the docs label Jul 9, 2026
@github-actions

github-actions Bot commented Jul 9, 2026

Copy link
Copy Markdown
Contributor

Coverage report

Click to see where and how coverage changed

FileStatementsMissingCoverageCoverage
(new stmts)
Lines missing
  haystack/components/preprocessors
  document_cleaner.py
Project Total  

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

With remove_repeated_substrings=True, header/footer detection compares the pages
between the first and last. For a 3-page document that slice is a single page,
and _find_longest_common_ngram reduced set.intersection over that one page's
ngrams to the page's own longest ngram, which was then removed from every page,
wiping the unique middle page. Require at least two sequences before deriving a
common ngram.
@otiscuilei
otiscuilei force-pushed the fix/document-cleaner-3-page-wipe branch from a53b74d to 4d77e37 Compare July 9, 2026 07:46

@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 the fix @Otis0408!

@bogdankostic
bogdankostic merged commit e5107dc into deepset-ai:main Jul 9, 2026
24 of 25 checks passed
@otiscuilei

Copy link
Copy Markdown
Contributor Author

Thanks for the review and merge, much appreciated!

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.

3 participants