Skip to content

Fix template whitespace collapse merging lines on trailing whitespace#1935

Merged
RobinPicard merged 1 commit into
dottxt-ai:mainfrom
lntutor:fix/template-whitespace-collapse-newline
Jul 20, 2026
Merged

Fix template whitespace collapse merging lines on trailing whitespace#1935
RobinPicard merged 1 commit into
dottxt-ai:mainfrom
lntutor:fix/template-whitespace-collapse-newline

Conversation

@lntutor

@lntutor lntutor commented Jul 19, 2026

Copy link
Copy Markdown
Contributor

What's broken

The whitespace-collapsing regex in build_template_from_string uses \s, which includes newlines:

# Remove extra whitespaces, except those that immediately follow a newline symbol.
cleaned_template = re.sub(r"(?![\r\n])(\b\s+)", " ", cleaned_template)

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:

build_template_from_string("foo   \nbar").render()
# 'foo bar'   -- expected 'foo \nbar'

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 \b guard 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). Full tests/test_templates.py passes (17).

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

@RobinPicard RobinPicard 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.

Good catch, thanks!

@github-actions

Copy link
Copy Markdown

📚 Documentation preview: https://dottxt-ai.github.io/outlines/pr-preview/pr-1935/

Preview updates automatically with each commit.

@RobinPicard
RobinPicard merged commit 4be7c31 into dottxt-ai:main Jul 20, 2026
6 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants