Skip to content

fix(llm): preserve whitespace in multiline non-fncall parameter values - #6451

Merged
VascoSch92 merged 4 commits into
OpenHands:mainfrom
wangxuzhou666-arch:fix/non-fncall-multiline-param-whitespace
Nov 7, 2026
Merged

VascoSch92 merged 4 commits into
OpenHands:mainfrom
wangxuzhou666-arch:fix/non-fncall-multiline-param-whitespace

Conversation

@wangxuzhou666-arch

@wangxuzhou666-arch wangxuzhou666-arch commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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_string renders 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 a file_editor create wrote file_text without the first line's indentation or the final newline, and str_replace lost the indentation of new_str.

Summary

  • When a parameter value is still multiline after stripping, remove only the frame on each side: one newline (LF or CRLF) plus any spaces or tabs between it and the tag. Single-line values (enums, booleans, numbers) are stripped exactly as before.
  • Port the pinned test test_roundtrip_preserves_multiline_param_whitespace and its _first_tool_call_arguments helper from the worktree-test-edge-cases branch (commit f2fcf6e) without the xfail(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

uv run pytest tests/sdk/llm/test_llm_fncall_converter.py -q

37 passed on macOS / Python 3.13.17, and uv run pytest tests/sdk/llm -q gives 1566 passed, 2 xfailed. On main, the pinned test, the unframed-multiline-keeps-indentation case 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 --files on both changed files passes (ruff format and lint, pycodestyle, pyright, import rules).

Type

  • Bug fix

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 as create and 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.

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.
@wangxuzhou666-arch
wangxuzhou666-arch marked this pull request as ready for review November 4, 2026 03:42
…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.

@VascoSch92 VascoSch92 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.
@wangxuzhou666-arch

Copy link
Copy Markdown
Contributor Author

Thanks for trying those framings. Fixed in 062d5ce: _unframe_param_value now takes optional spaces or tabs plus \r?\n as the frame on each side, using the two re.sub calls you suggested. Your CRLF and space-padded examples are now parametrized cases, along with a tab-padded one, and they parse to " a()\r\n b()" and " a()\n b()". All three fail on main and on the previous head. I also updated the Notes in the description, since the CRLF caveat no longer applies.

@VascoSch92

Copy link
Copy Markdown
Member

Thanks for the rebase and for the framing fix. I checked _unframe_param_value with the CRLF and space-padded examples from my review, and they now come out as " a()\r\n b()" and " a()\n b()", while single-line values are still stripped. The merge also kept the alias tests from main next to yours, and the whole converter test file passes locally.

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.

@VascoSch92
VascoSch92 merged commit 9ba5d49 into OpenHands:main Nov 7, 2026
37 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