Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/outlines/templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ def build_template_from_string(
# Remove extra whitespaces, except those that immediately follow a newline symbol.
# This is necessary to avoid introducing whitespaces after backslash `\` characters
# used to continue to the next line without linebreak.
cleaned_template = re.sub(r"(?![\r\n])(\b\s+)", " ", cleaned_template)
cleaned_template = re.sub(r"(?![\r\n])(\b[^\S\r\n]+)", " ", cleaned_template)

env = create_jinja_env(None, filters)

Expand Down
10 changes: 10 additions & 0 deletions tests/test_templates.py
Original file line number Diff line number Diff line change
Expand Up @@ -397,3 +397,13 @@ def test_get_schema():
assert pydantic_nested_schema_output == (
'{\n "name": "<name>",\n "inner": {\n "foo": "<foo>"\n }\n}'
)


def test_render_trailing_whitespace_preserves_linebreak():
# A trailing space before a newline must not swallow the line break.
assert build_template_from_string("foo \nbar").render() == "foo \nbar"


def test_render_trailing_whitespace_keeps_next_line_indentation():
src = "\n A test line \n An indented line\n "
assert build_template_from_string(src).render() == "A test line \n An indented line"
Loading