Skip to content

serve/dsml: don't stream the "\n\n<" in front of a tool-call block - #80

Open
FeiyangP wants to merge 3 commits into
sqliteai:mainfrom
FeiyangP:fix/dsml-stream-calls-lead
Open

FeiyangP wants to merge 3 commits into
sqliteai:mainfrom
FeiyangP:fix/dsml-stream-calls-lead

Conversation

@FeiyangP

@FeiyangP FeiyangP commented Nov 18, 2026 •

Copy link
Copy Markdown

Problem

With a DeepSeek-V4.1 (DSML) container, a reply that calls a tool looks like

Let me check.\n\n<|DSML| calls>…

The \n\n< is ordinary text and arrives before the |DSML| control token that opens the block. DSMLParser._literal sends each piece to content as it arrives. Only when the calls tag is read does _tag trim \n\n< (or a bare <) off self.content, and by then it has already been streamed.

So the two views of the same reply disagree:

content
stream: false "Let me check."
stream: true (concatenated deltas) "Let me check.\n\n<"

A client that rebuilds the assistant turn from the stream and sends it back in the next request also replays the stray <.

Change

DSMLParser holds back the end of the content that could still become that lead-in ("\n", "\n\n", "\n\n<", or "<") until the next token decides:

  • the calls tag opens: the lead-in is dropped, exactly as the final message already trimmed it;
  • anything else (more text, an invoke tag, a channel switch, EOS, finish()): the held text is released unchanged.

server.py streams by slicing parser.content, so held text is simply not visible to the stream until it is released.

One interaction needed care. server.py cuts parser.content at a stop string, and finish() would then release held text after the cut. This happens when a BPE token completes the stop and also starts a lead-in, for example "P\n" for stop="STOP". A new DSMLParser.drop_held() is called where server.py makes that cut (through getattr, so the other parsers are untouched).

Only DSML replies change. Text that merely ends in newlines or < is delivered whole, but up to three tokens later: the longest lead-in, "\n\n<", can arrive one character per token and is only released by the token after it. For the same reason, a stop string inside such text is matched up to three tokens later, so up to three more tokens are decoded before the reply stops. The returned text is cut at the same place.

Stop strings and the lead-in

A stop string is matched against parser.content. The lead-in is held back and then dropped when the block opens, so it never reaches the content, and a stop string can no longer match it. With tools and stop: "\n" (or "<"), main ended the reply at the lead-in: finish_reason was stop and the tool call was lost, in both streaming and whole requests. With this change the reply runs into the block and returns the call (finish_reason: tool_calls). I think this is the right behavior, because the lead-in is formatting and the final message already trims it, so a stop string should not be able to end the reply on it. A stop string inside the content still cuts it as before. This is a deliberate change of stream: false results, and a test pins it (below). If you prefer main's behavior here, I can change it.

Stop strings across a block

A stop string is matched, and a possible stop prefix is held back, only from DSMLParser.stop_floor: the length of the content when the last tool-call block opened. Without it, text that ends in a stop prefix right before a block ("Let me check." with stop="check.\n") was held until the reply ended, because the lead-in that used to settle the match is now dropped. With it, that text is sent as soon as the calls tag is read. It also means a stop string cannot match across a block: "Let me check." + block + "\nmore" with stop="check.\n" returns "Let me check.\nmore", where main cut it to "Let me ".

Tests

  • tests/serve/test_dsml.py, new TestStreamedContent:
    • the lead-in to a block ("…\n\n", no newline, nothing) is never streamed and the streamed text equals the final content;
    • text that only looks like a lead-in ("a\n\nb", "x < y", trailing "\n"/"\n\n"/"<", with and without EOS) streams whole;
    • reasoning is unaffected;
    • a stop cut drops held text.
  • tests/serve/test_dsml_server.py (new): a DSML container on the real HTTP server with FakeEngine. It compares a streamed and a whole request for the tool-call case, a reply ending in newlines, and a stop string. test_a_stop_string_does_not_match_the_lead_in_to_a_tool_call pins the stop-string behavior above: with stop "\n" or "<" the call is returned and the content is "Let me check."; on main both cases fail ('stop' != 'tool_calls'). With stop="\n" and a newline inside the content, the reply is still cut there ("one"). On main (99a07e0), the tool-call case fails ('Let me check.\n\n<' != 'Let me check.'), and so do the three parser-level lead-in cases. All pass with the change.
  • tests/serve/test_dsml_server.py::test_a_stop_string_does_not_match_across_a_tool_call and tests/serve/test_dsml.py::test_the_stop_floor_moves_past_each_tool_call_block pin stop_floor; both fail without it.
  • make serve-check (with libwaste.so built): 437 tests OK, 25 skipped with the change; 427 OK, 25 skipped on main. The skips need a real container or DS41_DIR.
  • Not run: a real DeepSeek-V4.1 container.

Duplicates

There are no open PRs in the repository, and searching issues and PRs for "dsml" and "stream tool call" finds nothing related.

AI assistance: I prepared this change with an AI coding assistant. I reviewed the diff and ran the commands above myself.

With a DeepSeek-V4.1 (DSML) container, the "\n\n<" the model writes before
a tool-call block arrives as ordinary text, ahead of the |DSML| control
token that opens the block. DSMLParser streamed it as content and only
trimmed it from the final message, so a streaming client saw "Let me
check.\n\n<" where the same reply asked for whole says "Let me check.",
and a client replaying its streamed history sent the stray "<" back.

Hold back content that could still become that lead-in ("\n", "\n\n",
"\n\n<", "<") until the next token decides: a block drops it, anything
else releases it. A stop string that cuts the content also drops what is
held (DSMLParser.drop_held, called by server.py), so finish() cannot put
text back after the cut.
@mfethe1

mfethe1 commented Nov 18, 2026

Copy link
Copy Markdown
Contributor

Independent check of this PR at ebc8c89 against main 99a07e0, on an Apple M4 (macOS 15.6, Python 3.14). Like the PR's own tests, this uses a scripted subclass of the repo's FakeEngine, not a real DeepSeek-V4.1 container.

Upstream tests. make serve-check: 424 run, OK, 26 skipped on the PR; 417 run, OK, 26 skipped on main. (The description reports 434/427 with 25 skipped; the +7 is the same.) The PR's versions of tests/serve/test_dsml.py and tests/serve/test_dsml_server.py, run against main's serve/, give 5 failures in 33 tests: the three lead-in subtests, the server tool-call case, and the stop-cut test. All of them pass on the PR.

Differential run. I drove the real HTTP server (serve() on a loopback port) for both trees. Each case was sent once with stream: true and once with stream: false. The corpus had 17 DSML reply scripts:

  • plain text, and text ending in \n, \n\n, < and \n\n<;
  • a\n\nb, x < y, a bare <, text containing STOP, and a reasoning case;
  • a tool call after Let me check.\n\n, after Let me check., after only \n\n, and with no text before it;
  • two invokes in one block;
  • scripts cut off inside the lead-in and inside the tag header.

Each script was tokenized one character per token and in 200 seeded random segmentations of 1–6 characters, so pieces like ".\n\n" and "\n\n<" occur. Control tokens were always their own token. Stop sets were none, STOP, \n, \n\n, < and check.\n. The two cut-off scripts were run both with the max_tokens cap and without it. That gives 22,914 cases per tree: 3,819 with no stop string, 3,417 of them uncapped.

  • Stream vs whole. With no stop string, main streams different content from the whole response in 1,407 of the 3,417 uncapped cases. The difference is always a trailing \n\n< or < before a tool call. On the PR the count is 0 in all 22,914 cases, including the 19,095 with a stop string. The PR also removes a second main symptom. With stop check.\n and Let me check.<|DSML| calls>…, main streams Let me check.<check., so check. is sent twice.
  • Final results with no stop string (content, tool_calls, finish_reason and reasoning) are identical on both trees in all 3,819 cases, including the truncated scripts.

Things the description does not quite cover:

  1. Delivery can lag by more than one token. The description says held text arrives "one token later at most", but that does not hold when the held text grows. I compared the token at which each streamed character goes out on the PR and on main, over the 3,417 uncapped no-stop cases. 20,674 characters went out at the same token, 1,869 went out 1 token later, 165 went out 2 tokens later and 5 went out 3 tokens later. For example, with one character per token, the first \n of a\n\nb goes out with the b, 2 tokens later. In a\n\n< + EOS it goes out at EOS, 3 tokens later. This looks inherent to holding \n\n<, so the fix may only need a wording change.
  2. A stop string can no longer match inside the lead-in. Take Let me check.\n\n<|DSML| calls>… with stop \n, \n\n, < or check.\n. On main, the request stops before the block with finish_reason: stop and no tool call. The content is Let me check., Let me check.\n\n or Let me , depending on the stop. The PR runs on through the block and returns the tool call with finish_reason: tool_calls. This changes stream: false results too. It is consistent with the lead-in not being content, but requests that combine tools with such stop strings will now behave differently. You may want to make that choice explicitly and pin it with a test.
  3. Extra decode before a stop. On plain text, a stop string that ends in a held character matches only after the hold is released. For example, a\n\nb with stop \n takes 5 engine tokens on the PR and 3 on main, with the same final content. Across the corpus, the extra was 1 to 3 tokens on plain-text scripts. On the script cut off by max_tokens inside a tag header, the PR ran to the limit (2 to 7 more tokens) before the stop matched. The final result was the same.
  4. A small edge case. Sometimes the content before a block ends in a prefix of a stop string, for example Let me check. with stop check.\n. The server's stop-prefix hold then keeps check. back until the whole tool call has been generated. With one character per token, it arrived 112 tokens late. The final content is correct.

I found no case where the PR's stream differs from its whole response, and no change to final results when no stop string is set.

The "\n\n<" in front of a DSML tool-call block is held back and dropped
when the block opens, so it never reaches the content a stop string is
matched against. With tools and stop="\n" or stop="<", the reply now runs
into the block and returns the call; on main it stopped at the lead-in and
the call was lost. A stop inside the content still cuts it.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KuYoqNdWs6CF4ca1YJsYZg
@FeiyangP

Copy link
Copy Markdown
Author

Thanks a lot for the independent check, @mfethe1. The differential run is far more thorough than what I had, and both points are fair.

(a) Delay. You're right: held text can arrive up to three tokens late, not one. The longest lead-in, "\n\n<", can come one character per token and is only released by the token after it. The same delay applies to a stop string inside that text: it is matched up to three tokens later, so up to three more tokens are decoded, while the returned text is cut at the same place. I've corrected the description.

(b) Stop strings with tools. I kept the new behavior on purpose and pinned it with a test in c665736 (test_a_stop_string_does_not_match_the_lead_in_to_a_tool_call). The lead-in is formatting that the final message already trims, so I don't think a stop string should be able to end the reply on it and drop the tool call. That is what main does with stop="\n" or "<" (finish_reason: stop, no call). With this PR the call is returned, streamed or whole, and a stop inside the content still cuts it ("one\ntwo" with stop="\n" gives "one"). The test fails on main with 'stop' != 'tool_calls'. If the maintainers would rather keep main's behavior, I'm happy to change it.

(c) is the extra decode described in (a). For (d), I haven't reproduced it. If it comes from the stop-prefix hold in safe_content_end, which is already on main, I'd rather handle it in a separate PR. If this PR makes it worse, could you share the case? I'll look at it here.

make serve-check: 435 OK, 25 skipped.

@mfethe1

mfethe1 commented Nov 19, 2026

Copy link
Copy Markdown
Contributor

Thanks. The corrected description and the test in c665736 cover (a) to (c) for me.

On (d): it does come from the stop-prefix hold in safe_content_end, but this PR makes it a lot longer. Here is the case:

  • engine reply </think>Let me check. + the CALL block from test_dsml_server.py + EOS, one character per token
  • tools=TOOLS, stop="check.\n", stream: true

check. is a prefix of the stop string, so the server holds it back on both trees. On main (99a07e0) the next token is the < that leaks into the content. check.< can no longer match, so check. goes out with that <, 1 to 6 tokens after each of its characters was generated. (It is sent a second time at the end. That is the duplicate from my first comment.) On the PR, the < is held as lead-in and then dropped, so the content never grows past check.. The hold lasts until the reply ends: check. goes out at token 126 (EOS, one token after the block closes), 113 to 118 tokens late. That is 112 tokens later than on main, which is where my earlier figure came from. On the PR the same thing happens with Let me check.\n\n before the block (up to 120 tokens) and with two invokes (up to 212). On main those requests stop at check.\n instead, which is the change in (b), so there is no hold to compare. The streamed content and tool calls match the whole response in every case.

I re-ran the same corpus unchanged at c665736. Its streams are byte-identical to the ebc8c89 run. I then measured how long each streamed character was held, from the token that produced it to the token it was sent with:

  • main: the longest hold was 6 tokens (157,182 characters).
  • PR: 6,030 characters were held 34 to 212 tokens. All of them are stop check.\n in the five tool-call scripts whose content before the block is Let me check.. No plain-text script held a character longer than 6 tokens. Three characters in the script cut off inside the tag header were held 7 tokens. That is the lead-in hold meeting max_tokens, not this case.

A separate PR sounds right to me. One option, which I have not tried: release the stop-prefix hold when the parser opens a tool-call block. With (b), a stop string can no longer match into the lead-in, and nothing inside the block is content, so the held text cannot complete a match while the block is open. One caveat: DSMLParser does append text that follows the closing calls tag to the content, so Let me check. + block + \nmore would still match check.\n after check. had gone out. The release would only be safe if the reply cannot continue after the block, or if that case is handled.

A DSML reply that ends its text with the start of a stop string, such as
"Let me check." for stop="check.\n", was held back until the reply ended:
the "\n\n<" that would have completed or broken the match is the block's
lead-in, which is now dropped rather than added to the content. With one
character per token the text was held over 100 tokens.

DSMLParser now records stop_floor, the length of the content when a
tool-call block opens. server.py matches stop strings, and holds back a
possible stop prefix, only from there. Text before a block is sent once
the block opens, and a stop string cannot match across a block, so
"Let me check." + block + "\nmore" is no longer cut at "check.".

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01KuYoqNdWs6CF4ca1YJsYZg
@FeiyangP

Copy link
Copy Markdown
Author

Thanks for the reproduction, @mfethe1. You're right that this PR made (d) worse, I've fixed it here.

Cause: with stop="check.\n", the content Let me check. ends in a stop prefix. On main the next characters, the block's \n\n< lead-in, went into the content and settled the match. Now the lead-in is dropped, so nothing ever followed check. and it was held until the reply ended.

Fix: DSMLParser records stop_floor, the length of the content when a tool-call block opens. server.py matches stop strings, and holds back a possible stop prefix, only from there. This also covers the caveat you raised: a stop string can no longer match across a block, so Let me check. + block + \nmore with stop="check.\n" returns Let me check.\nmore (on main it was cut to Let me , because the match spanned the removed block). Streamed and whole replies still agree.

Measured with the one-character-per-token FakeEngine split and your Let me check. + block case: check. was held 114 tokens at c665736 (until the end of the reply), and is now held 9 tokens with no lead-in and 11 with \n\n. That is the length of the <|DSML| calls> tag, after which the block is known to have opened.

Tests: test_a_stop_string_does_not_match_across_a_tool_call (server) and test_the_stop_floor_moves_past_each_tool_call_block (parser) both fail at c665736 and pass now. make serve-check: 437 OK, 25 skipped.

@mfethe1

mfethe1 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor

Thanks, @FeiyangP. I re-ran the same differential at 6e55481 (main is still 99a07e0). It confirms the fix.

  • Streamed vs whole: all 22,914 cases now agree. On main, 4,221 of them differ (the lead-in leak).
  • Your (d) numbers reproduce. I used the one-character-per-token split with stop="check.\n". In Let me check. + block, the . is now held 9 tokens and the first c 14. With a \n\n lead-in the . is held 11 tokens and the c 16. At c665736 those characters were held 113 to 120 tokens.
  • Plain text: without a tool call, nothing is held more than 3 tokens (main: 1). That is the \n\n< lead-in hold, the same as at c665736; this commit adds none. Every hold above 6 at this head is in a tool-call script, and the longest is 16.
  • Tests: at c665736 with your new test files, the server test fails and the parser test errors (stop_floor does not exist there). Both pass at 6e55481. make serve-check on an M4 Mac mini gives 427 tests, OK, 26 skipped. My corpus has no script with text after the block, so the across-a-block case rests on your server test, not on this run.

One thing is unchanged from c665736. When a reply hits max_tokens inside the tag (my trunc_header script, capped at 22 tokens in the one-character split), the block never opens. Its \n\n then counts as content, so stop="check.\n" still fires and returns Let me . Uncapped, the same reply returns Let me check. and the tool call. Stream and whole agree in both cases, so I read this as expected for a block that never opened. I mention it only so it is a known edge.

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