Repository navigation
fix(llm): preserve whitespace in multiline non-fncall parameter values - #6451
Conversation
The non-native function-calling parser called .strip() on every <parameter=...> value. convert_tool_call_to_string frames a multiline string with one newline on each side, so .strip() also removed the value's own leading indentation and trailing newline. file_editor create then wrote file_text without the first line's indentation and final newline, and str_replace lost the indentation of new_str. Keep a value as parsed, minus the framing newline on each side, when it is still multiline after stripping. Single-line content is stripped exactly as before, so enum, bool and int parameters are unaffected.
…ram whitespace Port test_roundtrip_preserves_multiline_param_whitespace and its _first_tool_call_arguments helper from the worktree-test-edge-cases branch (f2fcf6e) without the xfail marker, in place of the equivalent roundtrip test. Keep two parser cases: unframed multiline content keeps its indentation, and framed single-line content is still stripped.
2bec744 to
2e521dd
Compare
VascoSch92
left a comment
There was a problem hiding this comment.
Thanks for the careful write-up, the LF case works and the round-trip test is a good one.
I tried a couple of other framings a model can produce, and they now come out worse than on main. With CRLF framing, "\r\n a()\r\n b()\r\n" parses to "\r\n a()\r\n b()\r", where main gives "a()\r\n b()". With a space before the newline, " \n a()\n b()\n " keeps all of the padding, where main gives "a()\n b()". For a file_editor create that means a stray blank line at the top of the file and a lone \r at the end.
Since you already offered this in the notes: could you make _unframe_param_value treat optional spaces or tabs plus \r?\n as the frame on each side? Something like re.sub(r"\A[ \t]*\r?\n", "", raw) and the mirror image at the end would do it. A couple of parametrized cases for CRLF and space-padded framing would be great.
A model can frame a multiline parameter value with CRLF, or put spaces or tabs between the tag and the framing newline. _unframe_param_value only removed a bare LF on each side, so such values kept the whole frame: a file_editor create got a blank first line and a lone carriage return at the end. Treat optional spaces or tabs plus one LF or CRLF as the frame on each side, as suggested in review, and cover CRLF, space-padded and tab-padded frames with parametrized cases.
|
Thanks for trying those framings. Fixed in 062d5ce: |
…ine-param-whitespace
|
Thanks for the rebase and for the framing fix. I checked The only thing left is CI, which hasn't run on the new head yet because the workflows need approval. Once that's green I'll clear my change request. |
HUMAN:
I verified this change locally. AI assisted with testing and review.
AGENT:
Why
On the non-native function-calling path,
convert_tool_call_to_stringrenders a multiline string parameter with one framing newline on each side, and the parser then called.strip()on every<parameter=...>value. That also removed the value's own leading indentation and trailing newline, so afile_editorcreatewrotefile_textwithout the first line's indentation or the final newline, andstr_replacelost the indentation ofnew_str.Summary
test_roundtrip_preserves_multiline_param_whitespaceand its_first_tool_call_argumentshelper from theworktree-test-edge-casesbranch (commit f2fcf6e) without thexfail(strict=True)marker, plus parser cases: unframed multiline content keeps its indentation, framed single-line content is still stripped, and CRLF, space-padded and tab-padded frames are removed.Issue Number
Part of #6348 (menu sub-item: "non-fncall parser .strip()s parameter values, dropping the leading indentation and trailing newline of multiline strings")
How to Test
37 passed on macOS / Python 3.13.17, and
uv run pytest tests/sdk/llm -qgives 1566 passed, 2 xfailed. Onmain, the pinned test, theunframed-multiline-keeps-indentationcase and the three frame cases fail and the single-line case passes; with the change, all six pass. Limiting the frame to LF fails only the CRLF case, and dropping tabs from it fails only the tab case.pre-commit run --fileson both changed files passes (ruff format and lint, pycodestyle, pyright, import rules).Type
Notes
The change is confined to parameter parsing in the non-native function-calling converter (
_extract_and_validate_params). The renderer only emits LF framing; CRLF and space- or tab-padded frames come from model-written calls and are removed the same way.A value that is one line plus a trailing newline (for example
"x = 1\n") still loses that newline, as it did before this change. The multiline check runs on the stripped value so that a single-line value padded with blank lines, such as"\n\ncreate\n\n", still parses ascreateand passes enum validation. Checking the unframed value instead would keep that newline but reject padded enum values; I can switch if you prefer that trade-off.Prepared with AI assistance.