Skip to content

fix(function_call): stream text that precedes DSML tool calls - #34244

Closed
tancheng33 wants to merge 1 commit into
sgl-project:mainfrom
tancheng33:fix/dsv32-streaming-drops-leading-content
Closed

tancheng33 wants to merge 1 commit into
sgl-project:mainfrom
tancheng33:fix/dsv32-streaming-drops-leading-content

Conversation

@tancheng33

@tancheng33 tancheng33 commented Aug 10, 2026 •

Copy link
Copy Markdown
Contributor

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_increment has two ways out. If the buffer
holds 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 second
branch 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_parse doesn't have this problem: it takes text[:idx] before
parsing. 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_start looks for self.bot_token or <|DSML|invoke, the same
two markers has_tool_call accepts. DeepSeekV4Detector subclasses this
detector and only renames the section tag (tool_calls vs function_calls), so
it 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 CPU
unit test with no server or GPU needed. Three cases, each run against both
DeepSeekV4Detector and DeepSeekV32Detector:

  • content that shares a chunk with the opening tag still gets streamed (the
    reported bug)
  • content is flushed exactly once when the opening tag is split across two
    chunks, so it is neither dropped nor duplicated
  • content in front of a bare <|DSML|invoke with no section wrapper is
    streamed, which is the other marker has_tool_call accepts

All 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 TestDeepSeekV32Detector and TestDeepSeekV4Detector cases in
test_function_call_parser.py still pass (205 passed), so the leading-text split
doesn'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

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
@hnyls2002

Copy link
Copy Markdown
Collaborator

Covered by #34458 (5899674). Text preceding a DSML tool call is streamed as normal_text instead of being dropped, for wrapped and bare invoke forms.

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.

@hnyls2002 hnyls2002 closed this Aug 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Bug] Streaming response truncates content when both content and tool_calls are present (SGLang 0.5.17)

2 participants