-
Notifications
You must be signed in to change notification settings - Fork 3k
feat: add strip_whitespaces and replace_regexes to DocumentCleaner #10400
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
julian-risch
merged 7 commits into
deepset-ai:main
from
VedantMadane:feat/document-cleaner-expansion
Feb 2, 2026
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cef0ab2
feat: add strip_whitespace and regex_replace to DocumentCleaner
VedantMadane ec65d07
Merge branch 'main' into feat/document-cleaner-expansion
VedantMadane 8359403
docs: Add release notes for DocumentCleaner enhancements
VedantMadane 8d0d5d8
Merge branch 'main' into feat/document-cleaner-expansion
VedantMadane 12b2dd7
Merge branch 'main' into feat/document-cleaner-expansion
VedantMadane 76b4bef
refactor: rename parameters and handle page breaks in replace_regexes
VedantMadane c01bb06
fmt
julian-risch File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
30 changes: 30 additions & 0 deletions
30
releasenotes/notes/document-cleaner-expansion-d7f301767ecd40a5.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| --- | ||
| enhancements: | ||
| - | | ||
| Add ``strip_whitespaces`` and ``replace_regexes`` parameters to DocumentCleaner component. | ||
|
|
||
| The ``strip_whitespaces`` parameter removes leading and trailing whitespace from document | ||
| content using Python's ``str.strip()``method. Unlike ``remove_extra_whitespaces``, this only | ||
| affects the beginning and end of the text, preserving internal whitespace which is useful | ||
| for maintaining markdown formatting. | ||
|
|
||
| The ``replace_regexes`` parameter accepts a dictionary mapping regex patterns to replacement | ||
| strings, allowing custom text transformations. For example, ``{r'\\n\\n+': '\\n'}`` replaces | ||
| multiple consecutive newlines with a single newline. This is applied after ``remove_regex`` | ||
| and provides more flexibility than simple pattern removal. | ||
|
|
||
| Example usage: | ||
|
|
||
| .. code:: python | ||
|
|
||
| from haystack.components.preprocessors import DocumentCleaner | ||
| from haystack.dataclasses import Document | ||
|
|
||
| cleaner = DocumentCleaner( | ||
| strip_whitespaces=True, | ||
| replace_regexes={r'\n\n+': '\n'} | ||
| ) | ||
|
|
||
| doc = Document(content=" \n\nHello World\n\n\n ") | ||
| result = cleaner.run(documents=[doc]) | ||
| # Result: "Hello World\n" |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.