Fix template whitespace collapse merging lines on trailing whitespace#1935
Merged
RobinPicard merged 1 commit intoJul 20, 2026
Merged
Conversation
The whitespace-collapsing regex in build_template_from_string used \s, which includes newlines. Because the run starts after a word boundary (\b, so the negative lookahead (?![\r\n]) passes), a whitespace run with trailing space before a newline greedily swallowed the line break and the next line's indentation, merging two lines. A single, usually invisible, trailing space after a word would destroy a line break -- e.g. 'foo \nbar' rendered as 'foo bar'. Restrict the collapsed run to horizontal whitespace ([^\S\r\n]) so multi-space runs still collapse to one space while newlines and post-newline indentation are preserved. Co-Authored-By: Claude Fable 5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01N6RtoHuxrDqTUo9Mw9h4Cv
|
📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1935/ Preview updates automatically with each commit. |
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.
What's broken
The whitespace-collapsing regex in
build_template_from_stringuses\s, which includes newlines:The run starts after a word boundary (
\b, so the(?![\r\n])lookahead passes), then\s+greedily eats the following newline and the next line's indentation. So a single, usually invisible, trailing space after a word destroys the line break:An indented multi-line template with one trailing space loses both the newline and the indentation — no user expects a trailing space to merge two lines.
Fix
Restrict the collapsed run to horizontal whitespace (
[^\S\r\n]) so multi-space runs still collapse to one space while newlines and post-newline indentation are preserved. The existing\bguard already preserves post-newline indentation; this stops a collapsed run from crossing a newline.Tests
Adds two cases to
tests/test_templates.py(trailing space preserves the line break; and keeps next-line indentation). Fulltests/test_templates.pypasses (17).