Repository navigation
fix(content filter): preserve inline text in retained paragraphs - #2422
Open
Ha1baraA11 wants to merge 3 commits into
Open
Ha1baraA11 wants to merge 3 commits into
Ha1baraA11 wants to merge 3 commits into
Conversation
SohamKukreti
requested changes
Oct 22, 2026
SohamKukreti
left a comment
Collaborator
There was a problem hiding this comment.
Thanks for the fix! The approach is right: inline elements now follow their block's keep/remove decision, in both the BS4 and LXML engines. I checked that the new tests fail on develop and pass with this change, and that the existing prune and parity tests still pass. (test_threshold_types also fails on develop, so it is not caused by this PR.)
I also ran it on 8 real pages (Wikipedia, HN, GitHub, The Verge, BBC, Python docs, MDN, Reddit). The new text is real content that the filter was cutting out of sentences. Nothing that was kept before is lost. A few changes before we merge:
- Make the tag list smaller. Some entries do nothing:
script,noscriptandiframeare removed byexcluded_tagsbefore pruning starts. Other entries don't hold sentence text, and they change behavior beyond this issue. For example,<img>inside kept paragraphs was always pruned before and is now always kept. Please limit the list to text-level phrasing tags:
a abbr b bdi bdo cite code data del dfn em i ins kbd mark q ruby s samp small span strong sub sup time u var
This still fixes #2417. On the Tokyo page it adds 12% tofit_markdowninstead of 18%. - Put the set on the class (e.g.
self.inline_tags, next totag_weights), not in a private module constant imported fromcontent_filter_strategy_lxml.py. The LXML subclass then gets it for free, and users can change it. - Remove the unrelated re-wrap of the
_should_removesignature incontent_filter_strategy_lxml.py. - Remove the duplicated child loop in
_prune_tree: only score and maybe remove the node when it is not inline, and then go into the children once. - Merge the two tests into one with
@pytest.mark.parametrizeoverPruningContentFilterandPruningContentFilterLXML.
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
Summary
Keeps nested inline text when its content-filter paragraph is retained.
Related to #2417
List of files changed and why
How Has This Been Tested?
uv run --frozen --python=3.12 --with=pytest python -m pytest tests/async/test_content_filter_prune.py::TestPruningContentFilter::test_pruning_preserves_nested_inline_content_in_kept_paragraphs -q --disable-warnings— 1 passed.python3 -m compileall -q crawl4ai/content_filter_strategy.py crawl4ai/content_filter_strategy_lxml.py tests/async/test_content_filter_prune.py— passed.uv run --frozen --python=3.12 --with=pytest python -m pytest tests/async/test_content_filter_prune.py -q --disable-warnings— 1 failed, 13 passed; pre-existing base failure:tests/async/test_content_filter_prune.py::TestPruningContentFilter::test_threshold_types.Checklist: