Skip to content

fix(glm cuda): a tool call the end token left open is sent when it parses whole; a missing <arg_key> is put back - #285

Closed
MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:fix/glm-open-tool-calls
Closed

MiaAI-Lab wants to merge 1 commit into
ashhart:mainfrom
MiaAI-Lab:fix/glm-open-tool-calls

Conversation

@MiaAI-Lab

@MiaAI-Lab MiaAI-Lab commented Oct 2, 2026 •

Copy link
Copy Markdown
Contributor

TL;DR: on CUDA, a GLM tool call that the model ends with its end token before </tool_call> is now closed and sent as a call when it parses as a whole call. If it doesn't parse, its markup stays the reply's text, as before. Separately, a GLM call where the model wrote whitespace instead of an <arg_key> token gets that token back in the shared GLM parser, so both servers read the call.

Problem

Reported on the GLM-5.3-Flash DGX Spark recipe (issue #22). Agent clients (opencode among them) sometimes got a reply with finish_reason: "stop" and no tool call, although the model had written one:

  1. End token inside the call. The model writes its end token before </tool_call>, for example <tool_call>read_file<arg_key>path</arg_key><arg_value>notes.txt</arg_value> and then the end token. parse_tool_calls only reads closed blocks, so no call goes out. The client receives the call's markup as plain text.
  2. Missing <arg_key>. The model writes whitespace where the <arg_key> token belongs: read_file path</arg_key><arg_value>.., or </arg_value>\nlimit</arg_key><arg_value>3</arg_value> for a later argument. In the first form the whole call comes back as text. In the second form the argument is silently lost, and a single-call request (parallel_tool_calls: false) refuses the call.

Fix

  • cuda/server.py: the fix applies only when all of these hold:

    • the model's own end token ended the reply (not a stop string, not the token limit, not ignore_eos)
    • the reply ended inside a <tool_call> block
    • the tokenizer has GLM's call marks as tokens

    In that case close_glm_call (cuda/reply_text.py) appends </tool_call>, plus </arg_value> when a value is still open. The suffix is kept only if the closed block then parses strictly: one whole call to an offered tool, nothing but its markup, arguments a JSON object. Streamed and non-streamed replies take the same path. This includes a call at the end of an unclosed think block, which split_thinking already treats as the answer once it is closed.

  • server/tools.py: repair_glm_keys puts a missing <arg_key> back before _parse_glm_payload reads a block: the word before an unopened </arg_key> becomes the key. Both servers' end parsers use this, so the Mac server reads these calls too. A block with no unopened </arg_key> is returned as it was.

Unchanged: these replies stay exactly as they are today:

  • replies without tools
  • non-GLM models
  • calls that are already closed
  • calls cut by the token limit or a stop string
  • open calls that wouldn't parse once closed (their markup stays the text)

Token ids and token_sha never change. Only the parsing of the finished text does.

Tests

  • tests/test_glm_tool_call_end.py (new):
    • repair_glm_keys cases, and both parsers (parse_tool_calls, parse_tool_calls_from_content) reading repaired calls
    • close_glm_call cases: what gets closed, and what stays open (a tool that wasn't offered, a key with no value, </think> after the call, text after an argument)
    • GlmApp through HTTP, streamed and not, on a byte-level tokenizer with GLM's marks. It covers calls the end token left open (with and without text before them, after a closed call, inside an unclosed think block), open calls that stay text, the token-limit and stop-string cuts (unchanged), and a closed call with a missing key.
  • Before this change, 16 of the server and parser cases fail; with it, all 49 pass.
  • CPU only, in an NVIDIA PyTorch container (aarch64, no mlx): pytest tests --ignore=tests/cuda -k 'tool or glm or cuda_server or stop or reply or thinking or policy'.
    • This branch: 983 passed. main: 934 passed (the difference is the 49 new tests).
    • Both have the same 19 failures and 2 collection errors, all of which need mlx.

…rses whole; a missing <arg_key> is put back

GLM-5.3-Flash sometimes writes its end token before </tool_call>. The CUDA server then sent no call: the
reply finished "stop" with the call's markup as its text, and agent clients stalled. When the model's end
token (not a stop string, not the token limit) ends the reply inside a GLM call, the call is now closed
(</arg_value> too when a value is open) and sent if it then parses strictly: one whole call to an offered
tool, nothing but its markup, arguments a JSON object (close_glm_call). Otherwise its markup stays the text,
as before. A call the token limit or a stop string cut is unchanged. Only models whose tokenizer has GLM's
call marks as tokens take this path.

GLM calls whose <arg_key> the model wrote as whitespace ("read path</arg_key><arg_value>..", or
"</arg_value>\npath</arg_key>") get it back in the shared GLM parser (repair_glm_keys), so both servers'
end parsers read them; before, the first form came back as text and the second lost the argument. Blocks
with no unopened </arg_key> are unchanged.

Reported on the GLM-5.3-Flash DGX Spark recipe, issue ashhart#22.

Tests: tests/test_glm_tool_call_end.py.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
ashhart added a commit that referenced this pull request Oct 6, 2026
This carries the server half of PR #285 by MiaAI-Lab. When the model's end
token ends a reply inside a tool call, before </tool_call>, the server now
closes the call and sends it if it then parses whole. Until now such a reply
finished "stop" with no call. A streamed client kept the call's opening delta
and arguments that never closed, and a non-streamed client got the markup as
text.

tool_parse.closeCall takes the opener the end parser would leave unclosed and
adds what the call's arguments lack: </function> for an XML call, or the open
arrays and objects that tool_params.closedJson closes for a JSON call. It
never closes a value, and it returns the closers only when the closed block
parses strictly as one call to an offered tool with nothing but its markup.
Generation.finish appends them to the reply's text before the think split, so
a call that ends an unclosed think block becomes the answer, as in the PR. In
a streamed reply the streamer reads the closers like markup the model wrote,
so the call completes with the same deltas. A call that does not parse whole
is left as it is. A non-streamed reply keeps its markup as text, and a
streamed reply whose opening delta already went out finishes as before.
Nothing closes a reply that ends on a stop string or the length limit, or one
that sets ignore_eos.

The PR's other half, which puts back a GLM <arg_key> the model wrote as
whitespace, waits for the GLM family in Zig and is not in this change.

The new unit tests close whole XML, JSON and GLM calls and check that the end
parser then returns them. Calls cut inside a value, a key without its value,
a tool that was not offered, an empty block and an earlier unclosed opener
stay text. A streamed call left open ends with the deltas of one closed by its
markup, and one cut inside a value gets nothing more. The two completion
tests fail with closeCall stubbed to close nothing, and the two guard tests
fail when it skips the whole-call check. The server's unit tests now run in
zig build test through a root.zig test module, the one
zig/tests/server/zig_test.sh runs, which takes the step from 37 to 60 tests.
On the scripted fake server, the open-call scripts now finish "tool_calls"
streamed and not, all 240 replies of the existing corpus are unchanged
against the base, and the parity harness still matches the Python server in
315 of 319 cases with no unexpected difference.

Co-authored-by: MiaAI-Lab <[email protected]>
@ashhart

ashhart commented Oct 6, 2026

Copy link
Copy Markdown
Owner

Thank you, @MiaAI-Lab. The server half of this landed in the Zig engine as 1c3487c, with your credit. A tool call that the model's end token leaves open is now completed when its arguments parse whole, and a non-streamed call that doesn't parse stays the reply's text, as in your fix.

There is one difference. The Zig server streams a call as it parses, so when a streamed call's arguments don't parse whole, the client has already seen the call start, and it stays unfinished rather than turning back into text.

The missing <arg_key> repair is GLM markup, and GLM isn't on the Zig engine yet, so that half waits for the family and carries your credit when it lands. The Python engine is frozen (#286), so this PR closes with that. Thanks for tracing the client reports back to the end token.

@ashhart ashhart closed this Oct 6, 2026
Bizuayeu added a commit to Bizuayeu/TensorFold that referenced this pull request Oct 12, 2026
… it back


GLM-5.3-Flash sometimes writes the key of an argument without its <arg_key>, leaving
whitespace where that token belongs: "read_file path</arg_key><arg_value>..", or
"</arg_value>\nlimit</arg_key><arg_value>..". glmCall then read the first as text and lost
the second argument. repairGlmKeys() puts <arg_key> back before the word ahead of each
unopened </arg_key> (one after the name, or after a </arg_value>), and glmCall reads the
repaired block; a block missing none, a key with no name before it, or no word at all
comes back as written. The parsers that end a reply and close a call the end token left
open both go through glmCall, so both read the repaired call.

The second half of TensorFold ashhart#285 by MiaAI-Lab (closed unmerged), carried by the 2.x
line since 2.5.0 (repair_glm_keys); the test cases are that commit's.
Bizuayeu added a commit to Bizuayeu/TensorFold that referenced this pull request Oct 12, 2026
…e closes it


When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; the 2.x line (TensorFold ashhart#285 by
MiaAI-Lab, since 2.5.0) closes the value too, with </arg_value> ahead of </tool_call>, and
sends the call when that parses. argumentsClose now does the same, so tool-eval reads the
3.x line as it read 2.x. The strict parse still decides: a key with no value, a tool not
offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and the 2.x cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.
Bizuayeu added a commit to Bizuayeu/TensorFold that referenced this pull request Oct 13, 2026
…hen parses

When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; ashhart#285 (by MiaAI-Lab, closed unmerged)
closed the value too, with </arg_value> ahead of </tool_call>, and sent the call when that
parsed. argumentsClose now does the same. The strict parse still decides: a key with no
value, a tool not offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and ashhart#285's cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
… it back

GLM-5.3-Flash sometimes writes the key of an argument without its <arg_key>, leaving
whitespace where that token belongs: "read_file path</arg_key><arg_value>..", or
"</arg_value>\nlimit</arg_key><arg_value>..". glmCall then read the first as text and lost
the second argument. repairGlmKeys() puts <arg_key> back before the word ahead of each
unopened </arg_key> (one after the name, or after a </arg_value>), and glmCall reads the
repaired block; a block missing none, a key with no name before it, or no word at all
comes back as written. The parsers that end a reply and close a call the end token left
open both go through glmCall, so both read the repaired call.

The second half of ashhart#285 by MiaAI-Lab (closed unmerged); the test cases are that PR's.

Co-authored-by: MiaAI-Lab <[email protected]>
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
…hen parses

When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; ashhart#285 (by MiaAI-Lab, closed unmerged)
closed the value too, with </arg_value> ahead of </tool_call>, and sent the call when that
parsed. argumentsClose now does the same. The strict parse still decides: a key with no
value, a tool not offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and ashhart#285's cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.

Co-authored-by: MiaAI-Lab <[email protected]>
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
… it back

GLM-5.3-Flash sometimes writes the key of an argument without its <arg_key>, leaving
whitespace where that token belongs: "read_file path</arg_key><arg_value>..", or
"</arg_value>\nlimit</arg_key><arg_value>..". glmCall then read the first as text and lost
the second argument. repairGlmKeys() puts <arg_key> back before the word ahead of each
unopened </arg_key> (one after the name, or after a </arg_value>), and glmCall reads the
repaired block; a block missing none, a key with no name before it, or no word at all
comes back as written. The parsers that end a reply and close a call the end token left
open both go through glmCall, so both read the repaired call.

The second half of ashhart#285 by MiaAI-Lab (closed unmerged); the test cases are that PR's.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
…hen parses

When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; ashhart#285 (by MiaAI-Lab, closed unmerged)
closed the value too, with </arg_value> ahead of </tool_call>, and sent the call when that
parsed. argumentsClose now does the same. The strict parse still decides: a key with no
value, a tool not offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and ashhart#285's cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.
jschmied pushed a commit to jschmied/TensorFold that referenced this pull request Oct 13, 2026
…al by name, as ashhart#759's and ashhart#761's heads now say

Comment and test-name wording only, from the PRs' current heads.

Co-authored-by: Bizuayeu <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
This carries the server half of PR ashhart#285 by MiaAI-Lab. When the model's end
token ends a reply inside a tool call, before </tool_call>, the server now
closes the call and sends it if it then parses whole. Until now such a reply
finished "stop" with no call. A streamed client kept the call's opening delta
and arguments that never closed, and a non-streamed client got the markup as
text.

tool_parse.closeCall takes the opener the end parser would leave unclosed and
adds what the call's arguments lack: </function> for an XML call, or the open
arrays and objects that tool_params.closedJson closes for a JSON call. It
never closes a value, and it returns the closers only when the closed block
parses strictly as one call to an offered tool with nothing but its markup.
Generation.finish appends them to the reply's text before the think split, so
a call that ends an unclosed think block becomes the answer, as in the PR. In
a streamed reply the streamer reads the closers like markup the model wrote,
so the call completes with the same deltas. A call that does not parse whole
is left as it is. A non-streamed reply keeps its markup as text, and a
streamed reply whose opening delta already went out finishes as before.
Nothing closes a reply that ends on a stop string or the length limit, or one
that sets ignore_eos.

The PR's other half, which puts back a GLM <arg_key> the model wrote as
whitespace, waits for the GLM family in Zig and is not in this change.

The new unit tests close whole XML, JSON and GLM calls and check that the end
parser then returns them. Calls cut inside a value, a key without its value,
a tool that was not offered, an empty block and an earlier unclosed opener
stay text. A streamed call left open ends with the deltas of one closed by its
markup, and one cut inside a value gets nothing more. The two completion
tests fail with closeCall stubbed to close nothing, and the two guard tests
fail when it skips the whole-call check. The server's unit tests now run in
zig build test through a root.zig test module, the one
zig/tests/server/zig_test.sh runs, which takes the step from 37 to 60 tests.
On the scripted fake server, the open-call scripts now finish "tool_calls"
streamed and not, all 240 replies of the existing corpus are unchanged
against the base, and the parity harness still matches the Python server in
315 of 319 cases with no unexpected difference.

Co-authored-by: MiaAI-Lab <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… it back

GLM-5.3-Flash sometimes writes the key of an argument without its <arg_key>, leaving
whitespace where that token belongs: "read_file path</arg_key><arg_value>..", or
"</arg_value>\nlimit</arg_key><arg_value>..". glmCall then read the first as text and lost
the second argument. repairGlmKeys() puts <arg_key> back before the word ahead of each
unopened </arg_key> (one after the name, or after a </arg_value>), and glmCall reads the
repaired block; a block missing none, a key with no name before it, or no word at all
comes back as written. The parsers that end a reply and close a call the end token left
open both go through glmCall, so both read the repaired call.

The second half of ashhart#285 by MiaAI-Lab (closed unmerged); the test cases are that PR's.

Co-authored-by: MiaAI-Lab <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…hen parses

When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; ashhart#285 (by MiaAI-Lab, closed unmerged)
closed the value too, with </arg_value> ahead of </tool_call>, and sent the call when that
parsed. argumentsClose now does the same. The strict parse still decides: a key with no
value, a tool not offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and ashhart#285's cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.

Co-authored-by: MiaAI-Lab <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
… it back

GLM-5.3-Flash sometimes writes the key of an argument without its <arg_key>, leaving
whitespace where that token belongs: "read_file path</arg_key><arg_value>..", or
"</arg_value>\nlimit</arg_key><arg_value>..". glmCall then read the first as text and lost
the second argument. repairGlmKeys() puts <arg_key> back before the word ahead of each
unopened </arg_key> (one after the name, or after a </arg_value>), and glmCall reads the
repaired block; a block missing none, a key with no name before it, or no word at all
comes back as written. The parsers that end a reply and close a call the end token left
open both go through glmCall, so both read the repaired call.

The second half of ashhart#285 by MiaAI-Lab (closed unmerged); the test cases are that PR's.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…hen parses

When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; ashhart#285 (by MiaAI-Lab, closed unmerged)
closed the value too, with </arg_value> ahead of </tool_call>, and sent the call when that
parsed. argumentsClose now does the same. The strict parse still decides: a key with no
value, a tool not offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and ashhart#285's cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…al by name, as ashhart#759's and ashhart#761's heads now say

Comment and test-name wording only, from the PRs' current heads.

Co-authored-by: Bizuayeu <[email protected]>
BlivionIaG pushed a commit to BlivionIaG/TensorFold that referenced this pull request Oct 20, 2026
…hen parses

When the model's end token comes before </tool_call>, closeCall closes the call if it then
parses strictly. A GLM call ended inside its value ("run<arg_key>command</arg_key>
<arg_value>ls -la") stayed the reply's text here; ashhart#285 (by MiaAI-Lab, closed unmerged)
closed the value too, with </arg_value> ahead of </tool_call>, and sent the call when that
parsed. argumentsClose now does the same. The strict parse still decides: a key with no
value, a tool not offered, text after an argument or a think block past the call stay open.

The test that kept an open GLM value as text now closes it, and ashhart#285's cases of
close_glm_call (tests/test_glm_tool_call_end.py) are added.
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