Repository navigation
Tools: Keep edge whitespace in parsed tool arguments - #524
Open
anubhavlal07 wants to merge 1 commit into
Open
anubhavlal07 wants to merge 1 commit into
anubhavlal07 wants to merge 1 commit into
Conversation
This branch has not been deployed
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.
Is your pull request related to a problem? Please describe.
Fixes #523.
Parsed tool-call arguments lose their leading and trailing whitespace.
coerce_param_value()intoolcall_formats/common.pycalledraw.strip(), although its docstring says only the newlines the template adds around a value are removed. The qwen3_coder, minimax_m2, glm4_5 and hy3 parsers also called.strip()on each value before coercing it. The delta streamer intoolcall_stream.pydid the same so that its output matched those parsers. As a result a string argument" SPACED2 "came back as"SPACED2", and code passed to edit/write tools lost the indentation on its first line.Why should this feature be added?
Leading and trailing whitespace in a string argument is part of the value: an indented code block, a padded search string for an edit tool, and so on. Only the template's own
\nwrappers should be removed.The change:
strip_template_newlines()incommon.py(raw.strip("\r\n")).coerce_param_value()uses it. A.strip()copy is still used for the empty check and thejson.loadsattempt, so" 7 "with typeintegerstill becomes7. The plain-string fallback returns the newline-stripped value.coerce_param_value()and no longer strip it first.ToolCallDeltaEmitter.value_textandclose_paramuse the same helper, so streamed and end-of-stream arguments stay byte-identical._is_streamablestill gets the fully stripped text. Trailing newlines are held back until later text makes them interior, the same way trailing whitespace was held back before. I updated the module docstring and the inline comment to match.The GLM-4.5 and Hy3 templates render
<arg_value>without newlines around the value, so for them the change just stops removing whitespace the model wrote.Examples
<parameter=path>\n SPACED2 \n</parameter>withpath: {"type": "string"}:{"path": "SPACED2"}{"path": " SPACED2 "}Additional context
New tests in
tests/test_tool_param_types.py:test_coercion_keeps_edge_whitespace: string, untyped, integer, object and all-whitespace values.test_edge_whitespace_survives_parsing: qwen3_coder with a padded string, a tab-indented code block and padded integer/object values, plus minimax_m2, glm4_5 and hy3.test_edge_whitespace_streams_like_the_parse: the joinedQwenToolCallDeltaStreamerdeltas equal the parser's arguments at chunk sizes 1, 3 and 7, with and without a schema, and no "diverged" error is logged.The three new tests fail without the fix and pass with it.
test_tool_param_types.py,test_toolcall_stream.py,test_tool_delta_streaming.pyandtest_stream_parser.pyall pass, and ruff check and format are clean. #522 also editscoerce_param_value(), so whichever lands second will need a small rebase.