Skip to content

fix(server): DSML tool calls are read from their own tags, never re-read as <tool_call> text - #20

Merged
bertholomus merged 1 commit into
bertholomus:deepseek-v41-tp2from
willjriley:fix/dsml-own-tags
Oct 19, 2026
Merged

bertholomus merged 1 commit into
bertholomus:deepseek-v41-tp2from
willjriley:fix/dsml-own-tags

Conversation

@willjriley

Copy link
Copy Markdown

The bug. The DSML reader rewrote each DSML calls block as <tool_call>{json}</tool_call> text and then parsed that text again, so anything call-like around a call could move its bounds:

  • A </tool_call> inside an argument (a file being written, an edit's new text), or a <tool_call> in the prose before a block, cut the call short. The call then reached the client as that rewritten text and never ran.
  • A call to a tool the request did not offer also reached the client as <tool_call> text. That is call syntax the model never wrote, which the conversation then carries back to it.

We hit this in long agent sessions: 21 calls in two sessions reached the client as text.

The fix. Each DSML block is read from its own tags. The <tool_call> forms are parsed only in the text around the blocks, and calls keep the reply's order. The call streamer reads only the text before the first DSML block, so a Qwen-style call quoted inside an argument is never streamed as a call.

One deliberate change in behavior: a call to a tool the request did not offer is now returned as a call, so the client answers it the way it answers any unknown tool. Before, it came back as <tool_call> text. With parallel_tool_calls: false (one call a reply), it is still skipped.

Unchanged where there was no bug. We generated 20,000 replies and parsed each with both call limits (40,000 parses). Every difference from the current reader is in a reply with a stray tag or an unoffered tool. All 21 real leaked calls come back as calls, and each leaks again with the current reader.

Tests. tests/test_cuda_dsml_calls.py gets 7 new tests (all pass):

  • well-formed calls parse as before;
  • prose naming the tag keeps every call;
  • an argument holding the tags is the argument;
  • a tool the request did not offer stays a call;
  • one call a reply skips an unoffered tool and keeps the first offered one;
  • calls keep the reply's order around a block;
  • the call streamer stays out of a block.

The four existing tool-call test files (test_cuda_tool_choice, test_glm_tool_calls, test_think_call, test_cuda_server_stream) give 44 passed and 3 skipped, before and after.

Prepared with Claude Code.

…ead as <tool_call> text

A DSML calls block was rewritten as <tool_call>{json}</tool_call> text and parsed again, so the text around a call
could move its bounds. A <tool_call> in the prose before a block, or a </tool_call> inside an argument (a file being
written, an edit's new text), cut a call short; the call then reached the client as that rewritten text and never ran.
A call to a tool the request did not offer also reached the client as <tool_call> text: call syntax the model never
wrote, which the conversation then carries back to it.

Each DSML block is now read from its own tags; the <tool_call> forms are parsed only in the text around the blocks,
and calls keep the reply's order. A call to a tool the request did not offer is returned as a call, so the client
answers it as it answers any unknown tool; one call a reply (parallel_tool_calls false) still skips it. The call
streamer reads only the text before the first DSML block, so a Qwen-style call quoted in an argument is never streamed
as a call (the handler counts streamed calls as the first of the final ones).

Replies with only offered tools and no <tool_call> tag in their text parse as before: 20,000 generated replies, each
parsed with both call limits (40,000 parses), differ only where a reply has a stray tag or an unoffered tool, the cases
above. 21 calls that reached clients as text in real sessions all come back as calls; each leaks again with the old
reader. tests/test_cuda_dsml_calls.py: 7 new.
@bertholomus

Copy link
Copy Markdown
Owner

Thank you @willjriley! The tests pass here, and a reply without a DSML block is unchanged.

This one changes what a few replies return (a call to a tool the request did not offer, an argument holding </tool_call>, a tool name with spaces around it), so it waits for a maintainer's yes. We will follow up here.

@bertholomus
bertholomus merged commit 222a9c0 into bertholomus:deepseek-v41-tp2 Oct 19, 2026
@bertholomus

Copy link
Copy Markdown
Owner

Thank you @willjriley — merged on deepseek-v41-tp2 as 222a9c0. The final diff does what it says: each DSML block is read from its own tags, the call streamer stays out of blocks, and a call to a tool the request did not offer goes out as a call for the client to answer. The new tests pass (7/7), and the four existing tool-call suites give 44 passed / 3 skipped. The 20,000-reply comparison made this easy to take. Thanks again!

bertholomus pushed a commit that referenced this pull request Oct 20, 2026
…<tool_call> text (the native side of #20)

The native parser collected the <tool_call> forms and DeepSeek-V4.1's DSML blocks independently and kept whichever
started first. A <tool_call> in the prose before a block, with a </tool_call> in one of its arguments (a file being
written), swallowed the start of the block: the call was dropped and reached the client as text. A call to a tool the
request did not offer was rewritten as <tool_call>{json}</tool_call>, call syntax the model never wrote. And the call
streamer read the whole answer, so a Qwen-style <tool_call><function=NAME> quoted in a DSML argument streamed as a real
call to NAME when that tool was offered, and the reply's DSML calls were then never sent (tool_calls_streamed).

DSML blocks are now found first and the other forms only in the text around them; every invoke is a call, under the
model's own name when the request did not offer it (with one call a reply, such an invoke drops out as before); the
streamer is fed beforeDsml(answer), the text before the first DSML block. As 222a9c0 did for the Python server.

Tests: the unoffered-tool case now expects a call; a new test covers a <tool_call> in the prose, a </tool_call> and a
whole call in an argument, and calls in the reply's order. Both fail before this change.
bertholomus added a commit that referenced this pull request Oct 20, 2026
…thanks @hashd1ve)

fix(server): DSML calls read from their own tags in the native server (the native side of #20)
bertholomus added a commit that referenced this pull request Nov 2, 2026
…s are

A <tool_call> in the prose before a V4.1 calls block, with a </tool_call> in one of its arguments (a file being
written), swallowed the start of the block: the call was dropped and a call quoted in the argument was made instead.
A form quoted inside a block could also consume the call written after it, and a call quoted in an unclosed block was
made. The call streamer read inside V4.1 blocks, so a quoted <tool_call><function=NAME> streamed as a call when NAME was
offered, and the reply's own calls were then never sent.

The parser now reads V4.1 blocks first and the other forms only in the text around them (a V4.1 opener inside a V4
block stays that block's text); an unclosed block stays text, whole, as V4's does, and closeCall closes only a call past
the last block. The streamer reads a V4.1 block whole: before any call has streamed it leaves the reply's calls to the
end, as it always has; after one, the block's calls stream when it closes, in the reply's order, as V4's do. An offered
name is matched stripped, as the served lane's parse of its rewritten call strips it. A call to a tool the request did
not offer keeps the lane's rewrite.

A port of Manrique's 4803479 ("the native side of #20", on the two-node line) to the four-node line's DSML
containment design; the test of a <tool_call> around and inside a block is theirs. A reply whose V4.1 blocks have no
other call form inside, beside or across them, and no padded name, parses and streams as before.

Co-authored-by: Manrique <[email protected]>
Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
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