[Bug] deepseek v3.2/v4 DSML parsing: asserts instead of documented ValueError (silently skipped under -O), batch vs streaming grammar and duplicate-parameter divergences
- 1. I have searched related issues but cannot get the expected help.
- 2. The bug has not been fixed in the latest version.
- 3. Please note that if the bug-related issue you submitted lacks corresponding environment info and a minimal reproducible demo, it will be challenging for us to reproduce and resolve the issue, reducing the likelihood of receiving feedback.
Describe the bug
three consistency issues around the deepseek v3.2/v4 DSML parsing code, found by code reading on main @ 110965c. the third one is demonstrated with a runnable repro below.
| # | site | issue |
|---|---|---|
| 1 | lmdeploy/deepseek_v4_encoding.py:694-750, same pattern in deepseek_v32_encoding.py:348-385 |
parse_message_from_completion_text documents "will raise ValueError for malformed output" but enforces the format with assert. in practice it raises AssertionError, and under python -O the checks vanish entirely and malformed output parses into a well-formed-looking message |
| 2 | deepseek_v4_encoding.py:653/:685 (batch, requires literal '>\n' separators) vs serve/parsers/tool_parser/deepseek_v32_tool_parser.py:91/:131/:223 (streaming, whitespace-tolerant) |
the batch reference parser and the serving streaming parser implement different grammars. output with e.g. '> ' between tags fails the batch parser but streams fine, so the two paths disagree on what "valid DSML" means |
| 3 | deepseek_v4_encoding.py:679-680 (batch: duplicate parameter name raises ValueError) vs deepseek_v32_tool_parser.py:157-174 (streaming: emits both, producing arguments JSON with duplicate keys) |
duplicate parameters are rejected in one path and silently accepted with ambiguous JSON in the other |
i found no production caller for parse_message_from_completion_text yet (tests only), so the practical impact today is limited to the module acting as the reference definition of the format; the streaming parser is the production surface and it is the lenient side of divergences 2 and 3.
Reproduction
for issue 1, loading the module standalone (it only imports stdlib) and calling it with a malformed completion (no eos, no tool-call block, e.g. output cut off by an early stop string):
import importlib.util
spec = importlib.util.spec_from_file_location('dsv4', 'lmdeploy/deepseek_v4_encoding.py')
m = importlib.util.module_from_spec(spec)
spec.loader.exec_module(m)
m.parse_message_from_completion_text('partial answer, no terminal to', 'chat')observed on python 3.10.11, main @ 110965c:
actual: AssertionError: Invalid format: missing EOS tokenthe same call under python -O returns normally:
{'role': 'assistant', 'content': 'partial answer, no terminal to', 'reasoning_content': '', 'tool_calls': []}so the documented ValueError never occurs, and with asserts stripped the function cannot detect the malformed output at all. happy to extend this to a unit test if the maintainers want the exceptions converted to ValueError raises.
Environment
- python 3.10.11, lmdeploy main @ 110965c (module loaded standalone, no torch needed for the repro)
- no server run involved, pure parsing call
Error traceback
Traceback (most recent call last):
File "repro.py", line 6, in <module>
m.parse_message_from_completion_text('partial answer, no terminal to', 'chat')
File "lmdeploy/deepseek_v4_encoding.py", line 731, in parse_message_from_completion_text
assert stop_token == eos_token, 'Invalid format: missing EOS token'
AssertionError: Invalid format: missing EOS tokenSource: InternLM/lmdeploy