Repository navigation
Conversation
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.
|
Independent check of this PR at Upstream tests. Differential run. I drove the real HTTP server (
Each script was tokenized one character per token and in 200 seeded random segmentations of 1–6 characters, so pieces like
Things the description does not quite cover:
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
|
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, (b) Stop strings with tools. I kept the new behavior on purpose and pinned it with a test in c665736 ( (c) is the extra decode described in (a). For (d), I haven't reproduced it. If it comes from the stop-prefix hold in
|
|
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
I re-ran the same corpus unchanged at
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: |
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
|
Thanks for the reproduction, @mfethe1. You're right that this PR made (d) worse, I've fixed it here. Cause: with Fix: Measured with the one-character-per-token Tests: |
|
Thanks, @FeiyangP. I re-ran the same differential at 6e55481 (main is still 99a07e0). It confirms the fix.
One thing is unchanged from c665736. When a reply hits |
Problem
With a DeepSeek-V4.1 (DSML) container, a reply that calls a tool looks like
The
\n\n<is ordinary text and arrives before the|DSML|control token that opens the block.DSMLParser._literalsends each piece tocontentas it arrives. Only when thecallstag is read does_tagtrim\n\n<(or a bare<) offself.content, and by then it has already been streamed.So the two views of the same reply disagree:
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
DSMLParserholds back the end of the content that could still become that lead-in ("\n","\n\n","\n\n<", or"<") until the next token decides:callstag opens: the lead-in is dropped, exactly as the final message already trimmed it;invoketag, a channel switch, EOS,finish()): the held text is released unchanged.server.pystreams by slicingparser.content, so held text is simply not visible to the stream until it is released.One interaction needed care.
server.pycutsparser.contentat a stop string, andfinish()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"forstop="STOP". A newDSMLParser.drop_held()is called whereserver.pymakes that cut (throughgetattr, 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. Withtoolsandstop: "\n"(or"<"),mainended the reply at the lead-in:finish_reasonwasstopand 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 ofstream: falseresults, and a test pins it (below). If you prefermain'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."withstop="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 thecallstag is read. It also means a stop string cannot match across a block:"Let me check."+ block +"\nmore"withstop="check.\n"returns"Let me check.\nmore", wheremaincut it to"Let me ".Tests
tests/serve/test_dsml.py, newTestStreamedContent:"…\n\n", no newline, nothing) is never streamed and the streamed text equals the final content;"a\n\nb","x < y", trailing"\n"/"\n\n"/"<", with and without EOS) streams whole;tests/serve/test_dsml_server.py(new): a DSML container on the real HTTP server withFakeEngine. 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_callpins the stop-string behavior above: withstop"\n"or"<"the call is returned and the content is"Let me check."; onmainboth cases fail ('stop' != 'tool_calls'). Withstop="\n"and a newline inside the content, the reply is still cut there ("one"). Onmain(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_callandtests/serve/test_dsml.py::test_the_stop_floor_moves_past_each_tool_call_blockpinstop_floor; both fail without it.make serve-check(withlibwaste.sobuilt): 437 tests OK, 25 skipped with the change; 427 OK, 25 skipped onmain. The skips need a real container orDS41_DIR.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.