-
Notifications
You must be signed in to change notification settings - Fork 1.1k
fix: Preserve Line Breaks in Code Blocks During Chunking #4196
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
badGarnet
merged 10 commits into
Unstructured-IO:main
from
eureka928:fix/preserve-code-block-line-breaks
Jan 26, 2026
Merged
fix: Preserve Line Breaks in Code Blocks During Chunking #4196
badGarnet
merged 10 commits into
Unstructured-IO:main
from
eureka928:fix/preserve-code-block-line-breaks
Jan 26, 2026
+83
−17
Conversation
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
169fd08 to
294f471
Compare
badGarnet
reviewed
Jan 23, 2026
Contributor
Author
0bde867 to
cba9237
Compare
Contributor
Author
|
@badGarnet finally, this passed all! |
- Make <pre> elements generate CodeSnippet instead of Text elements - Preserve internal whitespace for CodeSnippet elements during chunking - Add tests for CodeSnippet whitespace preservation Closes Unstructured-IO#4095
270ce94 to
27fdd9a
Compare
badGarnet
reviewed
Jan 26, 2026
…x to correct version - Restored "Resolve GHSA-58pv-8j8x-9vj2" in 0.18.30 section - Moved Unstructured-IO#4095 fix to 0.18.31-dev1 section
badGarnet
approved these changes
Jan 26, 2026
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.

Problem
When using
chunk_elements()on markdown files containing code blocks, line breaks within the code were being discarded, resulting in unreadable code:Fixes #4095
Root Cause
Two issues were identified:
<pre>elements generated genericTextelements instead ofCodeSnippetelements_iter_text_segments()method normalized all whitespace to single spaces, destroying newlinesSolution
1. HTML Parser Change (
unstructured/partition/html/parser.py)Made
<pre>elements generateCodeSnippetelements:2. Chunking Change (
unstructured/chunking/base.py)Modified
_iter_text_segments()to preserve whitespace forCodeSnippetelements:Files Changed
unstructured/partition/html/parser.pyCodeSnippetimport, set_ElementCls = CodeSnippetinPreclassunstructured/chunking/base.pyCodeSnippetimport, special handling in_iter_text_segments()test_unstructured/partition/html/test_parser.pyCodeSnippetgeneration, updated existing testtest_unstructured/chunking/test_base.pyContribution by Gittensor, see my contribution statistics at https://gittensor.io/miners/details?githubId=42954461