Repository navigation
Keep whitespace in glm47 string tool arguments - #2800
Merged
Merged
Conversation
parse_tool_call stripped every <arg_value> before checking the declared
type, so a string argument such as write_file's content lost the
indentation of its first line and its trailing newline. The GLM chat
templates write string values verbatim (<arg_value>{{ v }}</arg_value>),
so that whitespace is part of the value.
Keep the raw text for string arguments, and strip only to deserialize
the others; a value that does not deserialize stays raw text. The key is
still stripped.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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.
Problem
glm47.parse_tool_callstrips every<arg_value>before it looks at the declared type:So a string argument that carries code loses the indentation of its first line and its trailing newline:
This parser is vendored from mlx-lm, and the same bug was reported there in ml-explore/mlx-lm#2064. With GLM-5.3-Flash behind an OpenAI-compatible server driving a coding agent, every
write_filecontent arrived without its final newline (427 of 427 calls). A one-line replacement for an indented line arrived at column 0 (17 of 17), so the next run of a Python file failed withIndentationError. Sending the line again could not fix it, because the parser stripped it again.Why the whitespace belongs to the value
The GLM chat templates write a string argument verbatim and any other type as JSON, with nothing between the tags and the value:
This holds for GLM-4.7 and GLM-5.3-Flash, which put everything on one line. It also holds for GLM-4.5 and GLM-4.6: their templates put newlines between
</arg_key>and<arg_value>and after</arg_value>, but never inside<arg_value>._func_arg_regexalready skips the newlines between the tags ((?:\\n|\s)*). So any whitespace inside<arg_value>…</arg_value>is text the model wrote as part of the value, and the template does not add it.The upstream GLM parsers keep it too. vLLM's
vllm/parser/glm47_moe.pystoresmatch.group("value")unstripped and strips only the key. SGLang'sglm47_moe_detector.pypasses the raw regex group to its type-awareparse_arguments.Fix
"type": "string") keep the raw text between the tags._deserialize, so typed values parse as before ("\n3\n"->3," true "->True,'\n[1, 2]\n'->[1, 2]).toolsis not passed or the schema has notype:contentwould otherwise still be stripped. Undeclared values that do parse as JSON or literals behave as before.The change is limited to the
<arg_key>/<arg_value>loop inparse_tool_call. The JSON, plain-text andkey=valuefallbacks are unchanged. The same change applies tomlx_lm/tool_parsers/glm47.pyfor mlx-lm#2064.Tests
New
test_parser_syntaxcases inmlx_vlm/tests/test_processors.py, next to the existingglm-newlinecase:glm-string-whitespace:write_filewith a declared stringcontentof" return x\n"keeps the indentation and the trailing newline.glm-typed-whitespace: a declared integer written as"\n3\n"still parses to3.glm-untyped-whitespace: withtools=None,contentthat is not JSON keeps its whitespace, while" 420 "still parses to420.Before the fix,
glm-string-whitespaceandglm-untyped-whitespacefail ('return x'instead of' return x\n').glm-typed-whitespacepasses both before and after, as a guard that typed values still parse. After the fix:pytest mlx_vlm/tests/test_processors.py -k glm: 10 passed, 1 skippedpytest mlx_vlm/tests/test_processors.py mlx_vlm/tests/test_tool_streaming.py mlx_vlm/tests/test_server.py -k "tool or parser or glm or …": 463 passed, 5 skippedpre-commit run --files mlx_vlm/tools/parsers/glm47.py mlx_vlm/tests/test_processors.py: black, isort and autoflake pass🤖 Generated with Claude Code