Repository navigation
fix(function_call): stream text that precedes DSML tool calls - #34244
Closed
tancheng33 wants to merge 1 commit into
Closed
tancheng33 wants to merge 1 commit into
tancheng33 wants to merge 1 commit into
Conversation
Once any DSML marker showed up in the buffer, parse_streaming_increment returned normal_text="" for the rest of the response, so whatever content was still buffered when the opening tag arrived never reached the client. Only responses that mix content with tool calls hit this; detect_and_parse already splits on text[:idx], so the one-shot path was correct and only streaming disagreed with it. Do the same split in the streaming path and trim the buffer so the text is emitted exactly once. Keying off self.bot_token covers DeepSeekV4Detector too, which only renames the section tag. Fixes sgl-project#34214
Collaborator
|
Covered by #34458 (5899674). Text preceding a DSML tool call is streamed as You are credited as a co-author on that commit. If I misread what this PR does and part of it is still missing, please rebase on main and reopen — happy to take it. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Motivation
Fixes #34214.
On DeepSeek V3.2 / V4, when the model answers with some text and a tool call in
the same response, part of the text never reaches the client. The stream delivers
a few content chunks, jumps to the tool call, and the rest of the content is
gone. Content-only and tool-call-only responses are fine, which is what makes it
look intermittent.
Modifications
DeepSeekV32Detector.parse_streaming_incrementhas two ways out. If the bufferholds nothing that looks like DSML, it flushes the buffer as
normal_text.Otherwise it falls into the invoke-matching loop, and every return there is
StreamingParseResult(normal_text="", calls=all_calls). Nothing in that secondbranch looks at what sits before the opening tag, and the check that routes
into it trips on a bare
"<|". So as soon as the chunk carrying<|DSML|tool_calls>lands, everything buffered ahead of it is thrown away.detect_and_parsedoesn't have this problem: it takestext[:idx]beforeparsing. So the one-shot path was already correct and only streaming disagreed
with it.
This PR does the same split in the streaming path and trims the buffer at the
same time, so the leading text goes out exactly once and is not re-sent on later
chunks. The split only runs before the first tool call (
current_tool_id == -1),so nothing changes once a tool call is already in flight.
_find_tool_call_startlooks forself.bot_tokenor<|DSML|invoke, the sametwo markers
has_tool_callaccepts.DeepSeekV4Detectorsubclasses thisdetector and only renames the section tag (
tool_callsvsfunction_calls), soit was affected by the bug and is covered by the fix without hardcoding either
spelling. The issue was reported on DeepSeek-V4-Flash.
Accuracy Tests
Not applicable, this is parser-side only and doesn't touch model outputs.
Added
test/registered/unit/function_call/test_deepseekv32_detector.py, a CPUunit test with no server or GPU needed. Three cases, each run against both
DeepSeekV4DetectorandDeepSeekV32Detector:reported bug)
chunks, so it is neither dropped nor duplicated
<|DSML|invokewith no section wrapper isstreamed, which is the other marker
has_tool_callacceptsAll six fail on current main and pass with the fix. The first one fails as
'Sure, let me look that up. ' != 'Sure, let me look that up. One moment please.',which is the truncation from the issue.
The existing
TestDeepSeekV32DetectorandTestDeepSeekV4Detectorcases intest_function_call_parser.pystill pass (205 passed), so the leading-text splitdoesn't disturb the invoke parsing or the self-closing shapes.
Checklist
CI States
Latest PR Test (Base): ❌ Run #31369847412
Latest PR Test (Extra): ❌ Run #31369847140