Repository navigation
fix(llm): recover orphan DeepSeek V4.1 tool calls - #6311
Conversation
rogercloud
left a comment
There was a problem hiding this comment.
PR #6311 changes the DeepSeek V4.1 DSML parser to recover complete orphan invokes only for tools declared in the request, suppress standalone orphan closing tags, and use filtered parser text in non-streaming responses. It also adds streaming and non-streaming coverage for orphan calls and code examples. The goal is to prevent stray protocol artifacts from leaking as assistant text without turning malformed or undeclared invokes into tool calls.
Blocking: yes — recommended event: COMMENT
Approach
The approach is sound: recovery is localized to the existing V4.1 parser, uses request-scoped tool declarations, and waits for complete invokes while leaving wire-format state to the shared response layer. The two confirmed issues are local boundary-recognition errors and do not require changing that overall design.
Findings
Major — A fenced code example can be surfaced as a tool call
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:91 — Blocking: yes. In a tool-enabled response, an inline triple-backtick run such as fence = "```" inside a still-open fenced code example clears _in_code's delimiter state even though it is not a Markdown closing-fence line. A subsequent valid orphan invoke for a declared tool is then accepted by recovery and serialized as a tool call instead of preserved as explanatory assistant text; clients that dispatch returned calls may act on sample code. Close a fence only in a valid closing-fence position, and cover the case in streaming and non-streaming tests that assert the exact content, no tool calls, and finish_reason == "stop".
Major — Invoke-like text inside a valid string parameter prevents recovery
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:137 — Blocking: yes. For a declared tool with a string="true" parameter, a complete orphan invoke may contain the literal <|DSML| invoke spelling inside the parameter value. The unchanged parameter grammar accepts that value verbatim, but this raw-text boundary search finds the embedded spelling before the outer invoke's real close, so the complete, strictly parseable call is returned as assistant text rather than recovered as a tool call. Treat markers within a parameter value as data, not sibling boundaries; make boundary recognition parameter-aware and add streaming/non-streaming regressions asserting recovery of the exact string argument while retaining malformed/incomplete-invoke coverage.
Verification limitations
Review evidence is static: tests, builds, linters, and runtime reproductions were not run. Real-model/runtime behavior remains unverified, and CI was deliberately not inspected under the repository review rule; the PR description's reported test results were not independently exercised.
Blocking status & recommended decision
Blocking: yes — recommended event: COMMENT.
Blocking issues:
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:91— major; explanatory fenced code can be returned as an actionable tool call that a client may dispatch. [new]xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:137— major; a complete valid orphan call with DSML-like text in a string argument is omitted and returned as assistant text. [new]
rogercloud
left a comment
There was a problem hiding this comment.
Review of #6311
Summary
This PR extends DeepSeek V4.1's DSML parser to recover complete unwrapped invocations only for tools declared by the request, filter orphan protocol closers, and preserve the parser's filtered text through shared response conversion. The latest update tightens Markdown fence recognition and makes orphan-boundary scanning parameter-aware, with regression cases for inline backticks inside fences and DSML-looking text inside string parameters. A standard indented Markdown example remains a false-positive tool call.
Blocking: yes — recommended event: COMMENT
Update and approach
Commit d4e073f01a2505f49b6c3f420bdc2f7b04f0c18a fixes the fence-boundary and parameter-value boundary cases tracked below. The overall approach is sound: DSML framing belongs in the model-specific parser, while request-scoped tool validation and OpenAI response conversion remain in their existing shared layers.
Prior findings
- H01 — FIXED (all occurrences). Inline
4793717887and the duplicate review-body occurrence6181313072._in_codenow recognizes valid fence boundaries (including a closing run with the matching delimiter and only trailing whitespace;xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:94-109). Theinline-backticks-in-fencecase checks preserved content/no calls and the response conversion matrix checks non-streaming plus chunk sizes 1, 13, and whole input, ending withstop(xinference/model/llm/tool_parsers/tests/test_argument_stream.py:256-325). Both source occurrences share this fixed root. - H02 — FIXED (all occurrences). Inline
4793717899and the duplicate review-body occurrence6181313072._find_invoke_boundaryskips a parameter value through its closing marker before looking for an invoke boundary (xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:117-137). The string-value invoke/closer cases are covered through the non-streaming and chunked-streaming response assertions (xinference/model/llm/tool_parsers/tests/test_argument_stream.py:268-325). Both source occurrences share this fixed root.
Findings
[major] Indented code examples become tool calls — Blocking: yes
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:91
For a tool-enabled response declaring write, this conventional four-space-indented Markdown example is supported assistant text, but contains no backtick/tilde delimiter for _in_code to recognize:
Here is an example:
<|DSML| invoke name="write"><|DSML| parameter name="code" string="true">example</|DSML| parameter></|DSML| invoke>
The code-context check therefore returns false, and orphan recovery accepts the complete invoke because write is declared and its string parameter parses (deepseek_v4_1_tool_parser.py:131-138,161-188). The example is lost from assistant content and returned as OpenAI message.tool_calls, changing a sole-call response's finish reason to tool_calls (xinference/model/llm/utils.py:1628-1656). The tools/client documentation describes application-side execution of returned calls (doc/source/user_guide/client_api.rst:166-169; doc/source/models/model_abilities/tools.rst:201-203); an application that dispatches this response may act on the example. No actual client-side execution is claimed. Four-space-indented examples are an ordinary supported representation of assistant text (doc/source/models/model_abilities/tools.rst:19-25; doc/source/user_guide/client_api.rst:214-218), and the base parser preserved this no-wrapper text; the false-positive is introduced by the new orphan-recovery path.
Please recognize conventional four-space/tab-indented code context before orphan recovery, without introducing a general Markdown parser. Preserve the exact example as assistant content, emit no tool_calls, and retain finish_reason == "stop" in both non-streaming and chunked-streaming responses; keep genuine unindented orphan invokes recoverable. Add regression coverage for those response contracts.
Limitations
Static review only: no tests, builds, linters, installs, runtime reproductions, or CI inspection were performed. The API misclassification is established from the parser and response-conversion paths; no actual client-side tool execution was observed.
Blocking status & recommended decision
Blocking: yes.
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:91— major: a declared-tool invoke in an indented example becomes an actionabletool_callsresponse instead of assistant content. [new]
Recommended event: COMMENT (PR author: qinxuye).
rogercloud
left a comment
There was a problem hiding this comment.
Minor
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:167— fence state is reset at each interval start, so an orphan invoke inside a fence that also contains a canonical block is recovered as an extra call (see inline).xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:180— streaming rescans the whole interval and every held orphan invoke on each chunk, which is O(n^2) on long outputs with stray DSML (see inline).xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:175— stripping orphan closers leaves the newlines between them (see inline).
Blocking: no — recommended event: APPROVE
rogercloud
left a comment
There was a problem hiding this comment.
Minor
xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:223— whitespace after a recovered orphan invoke is emitted as content (see inline).xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:141— one stray backtick keeps inline-code state on for the rest of the response (see inline).xinference/model/llm/tool_parsers/deepseek_v4_1_tool_parser.py:182— waiting for a parameter header>still rescans from the header start on every delta (see inline).xinference/model/llm/tool_parsers/tests/test_argument_stream.py— the strict-parse rejection branches (duplicate parameter, stray or trailing text between parameters,NaN/Infinity,RecursionError,string="maybe") and the no-tools case (orphan DSML left verbatim whentoolsis unset) have no tests; only bad JSON is covered. Add a few cases to the orphan matrix.
Simplification
- L103: shrink:
(results[-1][0] or "")cannot hit theor, because the branch already checksis not None. Useresults[-1][0] + text.
Blocking: no — recommended event: APPROVE
Problem
DeepSeek V4.1 can emit a complete DSML invoke without the outer
<|DSML| calls>opener, or emit only the closing tags. Xinference previously returned both forms as assistant text.Changes
Validation