Skip to content

Keep whitespace in glm47 string tool arguments - #2800

Merged
Blaizzy merged 2 commits into
Neywa-Labs:mainfrom
JarJarFran:fix-glm47-string-args
Nov 29, 2026
Merged

Blaizzy merged 2 commits into
Neywa-Labs:mainfrom
JarJarFran:fix-glm47-string-args

Conversation

@JarJarFran

Copy link
Copy Markdown
Contributor

Problem

glm47.parse_tool_call strips every <arg_value> before it looks at the declared type:

arg_key = match.group(1).strip()
arg_val = match.group(2).strip()
if arg_key not in string_args:
    arg_val = _deserialize(arg_val)

So a string argument that carries code loses the indentation of its first line and its trailing newline:

from mlx_vlm.tools import load_tool_module

tools = [{"type": "function", "function": {"name": "write_file", "parameters": {
    "type": "object",
    "properties": {"path": {"type": "string"}, "content": {"type": "string"}}}}}]
text = ("write_file<arg_key>path</arg_key><arg_value>a.py</arg_value>"
        "<arg_key>content</arg_key><arg_value>    return x\n</arg_value>")
load_tool_module("glm47").parse_tool_call(text, tools)["arguments"]["content"]
# before: 'return x'   after: '    return x\n'

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_file content 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 with IndentationError. 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:

<arg_value>{{ v | tojson(ensure_ascii=False) if v is not string else v }}</arg_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_regex already 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.py stores match.group("value") unstripped and strips only the key. SGLang's glm47_moe_detector.py passes the raw regex group to its type-aware parse_arguments.

Fix

  • String arguments (declared "type": "string") keep the raw text between the tags.
  • Other arguments are still stripped before _deserialize, so typed values parse as before ("\n3\n" -> 3, " true " -> True, '\n[1, 2]\n' -> [1, 2]).
  • A non-string or undeclared value that does not deserialize now stays as the raw text instead of the stripped text. This matters when tools is not passed or the schema has no type: content would otherwise still be stripped. Undeclared values that do parse as JSON or literals behave as before.
  • The key is still stripped.

The change is limited to the <arg_key>/<arg_value> loop in parse_tool_call. The JSON, plain-text and key=value fallbacks are unchanged. The same change applies to mlx_lm/tool_parsers/glm47.py for mlx-lm#2064.

Tests

New test_parser_syntax cases in mlx_vlm/tests/test_processors.py, next to the existing glm-newline case:

  • glm-string-whitespace: write_file with a declared string content of " return x\n" keeps the indentation and the trailing newline.
  • glm-typed-whitespace: a declared integer written as "\n3\n" still parses to 3.
  • glm-untyped-whitespace: with tools=None, content that is not JSON keeps its whitespace, while " 420 " still parses to 420.

Before the fix, glm-string-whitespace and glm-untyped-whitespace fail ('return x' instead of ' return x\n'). glm-typed-whitespace passes 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 skipped
  • pytest 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 skipped
  • pre-commit run --files mlx_vlm/tools/parsers/glm47.py mlx_vlm/tests/test_processors.py: black, isort and autoflake pass

🤖 Generated with Claude Code

JarJarFran and others added 2 commits November 18, 2026 17:30
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>

@Blaizzy Blaizzy left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

LGTM, thanks!

@Blaizzy
Blaizzy merged commit eb279be into Neywa-Labs:main Nov 29, 2026
11 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