fix(templates): don't leak stray whitespace from over-indented trailing blank lines - #1986
Open
BenAyedMedAla wants to merge 1 commit into
Open
Conversation
…ng blank lines `build_template_from_string` collapses trailing blank lines in a template down to a single linebreak, but only worked when the trailing blank line's indentation was pure spaces no deeper than the template's common margin. `inspect.cleandoc` only fully empties a trailing whitespace-only line when its (tab-expanded) indentation is <= that margin; a more deeply indented or tab-indented trailing blank line is left behind as a non-empty leftover line instead of being dropped. The existing `ends_with_linebreak` check also only stripped literal spaces from the raw content, so it couldn't detect the blank line in the tab case either. The combination leaked extra blank lines and stray trailing whitespace straight into rendered prompts. Strip cleandoc's own leftover whitespace explicitly, and broaden the blank-line detection to any horizontal whitespace, not just spaces.
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.
Fixes #1985.
What
build_template_from_string(used byTemplate.from_string/Template.from_fileand byApplication) is supposed to collapse any trailing blank line(s) in a template down to exactly one trailing linebreak. That only worked when the trailing blank line's indentation was pure spaces no deeper than the template's common margin — a more deeply indented, or tab-indented, trailing blank line leaked extra blank lines and stray whitespace straight into the rendered prompt instead.Root cause
inspect.cleandoconly fully empties a trailing whitespace-only line when its (tab-expanded) indentation is<=the common margin computed from the rest of the content; otherwise it leaves a non-empty leftover line rather than dropping it.ends_with_linebreakheuristic (content.replace(" ", "").endswith("\n\n")) only accounts for literal space characters, so it couldn't detect the blank line in the tab case either, even whencleandocdid clean it up.See #1985 for the full analysis and a minimal repro.
Fix
.rstrip()cleandoc's output before conditionally restoring a single trailing linebreak, so any leftover whitespace-only tail is removed regardless of whycleandocleft it behind." "), so tabs are handled the same as spaces.Both changes are scoped to
build_template_from_string;Template.from_fileshares the same dedent/whitespace-collapse tail so it benefits too, but the fix lives entirely in the string path since that's where the bug is (viacleandoc+heuristic), not in file-specific logic.Tests
Added two regression tests mirroring the existing
test_template_from_str_with_extra_linebreakscase:test_render_over_indented_trailing_blank_line_collapses_to_one_linebreaktest_render_tab_indented_trailing_blank_line_collapses_to_one_linebreakRan locally:
tests/test_templates.py(19/19) andtests/test_applications.py(6/6) pass.ruff check(pinned0.9.1, project config) andmypy(pinned1.14.1,--allow-redefinition) are clean on both changed files.maincommitpre-commitis installed and configured on my machine, and I ran the equivalent checks (pinnedruff/mypy) before opening this PRThis PR was developed with the help of an AI coding assistant (Claude Code); I've read and understand every line of the diff, and ran the tests/lint/type-checks myself before opening this.