Skip to content

[Fix] dsv41: Handle a final assistant message with text parts like string content - #45152

Closed
JohnZhao01 wants to merge 1 commit into
sgl-project:mainfrom
JohnZhao01:fix/dsv41-final-assistant-text-parts
Closed

JohnZhao01 wants to merge 1 commit into
sgl-project:mainfrom
JohnZhao01:fix/dsv41-final-assistant-text-parts

Conversation

@JohnZhao01

@JohnZhao01 JohnZhao01 commented Oct 19, 2026 •

Copy link
Copy Markdown
Contributor

Motivation

On the DeepSeek-V4.1 chat-encoding path (chat_encoding_spec == "dsv41"), a final assistant message whose content is a list of text parts (OpenAI SDK style, [{"type": "text", "text": "..."}]) is not handled like the same text sent as a string. The request returns HTTP 200 with an empty or garbled reply. Links below point to main @ 5d9c956f.

  • _handle_last_assistant_message only handles string content (serving_chat.py L442–L443). A parts list is neither converted to a user message (without continue_final_message, as string content is) nor taken as the continuation prefix (with continue_final_message=true).
  • The V4.1 encoder then joins the parts (encoding_dsv41.process_image_messages L617) and renders the message as a finished assistant turn that ends with <|end▁of▁sentence|>. It writes the next <|Assistant|> opener only after user-like messages (L412–L416), so the prompt ends at EOS and the model is asked to generate after the end of its own turn.
  • dsv4 and dsv32 are not affected: their parts lists are flattened to strings before _handle_last_assistant_message (serving_chat.py L1579–L1598). Only dsv41 skips that step, because its encoder consumes parts lists itself.

Example of the request shape (thinking on; with a complete answer in this position, 7/10 replies were empty before this change and 0/10 after, see Accuracy Tests):

{"messages": [
  {"role": "user", "content": "Write a Python function `is_prime(n)` that returns True if n is prime."},
  {"role": "assistant", "content": [{"type": "text", "text": "Here is the function:\n\n```python\ndef is_prime(n):\n    ...\n```"}]}
 ],
 "chat_template_kwargs": {"thinking": true}}

Modifications

  • serving_chat.py: on the dsv41 path, right before _handle_last_assistant_message, a final assistant message whose content is a list of text parts only is joined with "\n\n", the same join the V4.1 encoder applies. The message then gets exactly the handling of string content, and the model sees the same text. Lists that contain non-text parts (for example images) are left unchanged. Other encodings and the Jinja-template path are not touched.
  • Test: test_dsv41_final_assistant_text_parts_match_string_content checks that a two-part list renders exactly like the joined string, without and with continue_final_message, and that the prompt no longer ends at <|end▁of▁sentence|>.

Accuracy Tests

Setup: DeepSeek-V4.1-Flash on 8×H200, image lmsysorg/sglang:nightly-dev-cu13-20261018-b234b5c3 (its serving_chat.py, encoding_dsv41.py and test_serving_chat.py are byte-identical to main @ 5d9c956f), --tp 8 --attention-backend dsv4 --moe-runner-backend marlin --reasoning-parser auto --tool-call-parser auto, DSpark speculative decoding (--speculative-algorithm DSPARK --speculative-num-steps 1 --speculative-eagle-topk 1 --speculative-num-draft-tokens 6 --speculative-dspark-block-size 5). "Before" is the image as is; "after" is the image with this PR. Requests go to /v1/chat/completions with an explicit chat_template_kwargs.thinking, T=1.0, top_p=0.95, max_tokens 2048, 10 requests per cell.

Final assistant message thinking before: empty replies after: empty replies
answered, string (control) on 2 (both hit max_tokens) 3 (all hit max_tokens)
answered, one text part on 7 0
answered, two text parts on 8 3 (all hit max_tokens)
code prefix, string, continue_final_message (control) on 1 (finish_reason=stop after a reasoning block; string content takes the unchanged path, so this is run-to-run variation) 0
code prefix, one text part, continue_final_message on 10 0 (10/10 continue the code)
code prefix, two text parts, continue_final_message on 10 0 (10/10 continue the code)
code prefix, one / two text parts, continue_final_message off 0 / 0 empty, but 8 / 5 replies start with junk such as @@CODE_0@@ or @@code_placeholder@@, and 1 / 1 have </think> in content 0 / 0 empty, no junk, 10 / 10 continue the code
  • With thinking off, the list prefix cases were not empty before this change, but most replies were garbled: 13 of 20 started with junk such as @@CODE_0@@, the same symptom as other prompts that end at EOS. After this change, the list cases behave like the string controls in every row.
  • Before this change, the empty list replies stopped with finish_reason=stop after a short reasoning block (sometimes only a few characters). After it, all 3 remaining empty replies stopped with finish_reason=length (the 2048-token budget was spent in reasoning), as did the 3 empty string-control replies.
  • Requests that end with a user message, and string content, take the unchanged path, so benchmarks such as GSM8K are not affected by this change.

Unit tests, test/registered/unit/entrypoints/openai/test_serving_chat.py on the same image: before 159 passed (119 subtests); after 160 passed (121 subtests), no failures. Control: the new test run against the unchanged serving_chat.py fails in both subtests (without and with continue_final_message).

Speed Tests and Profiling

Not applicable: the change joins the text parts of one message on the host before encoding.

Checklist

Notes

  • [Fix] openai: Handle empty messages after final assistant stripping #28657 (an empty message list with continue_final_message) and [DSV4] Fix continue_final_message prefill rendering #26176 (the dsv4 string-prefill path) also edit _handle_last_assistant_message. Both are independent of this change, which only touches the dsv41 caller.
  • On the Jinja-template path, a parts-list final assistant message is also not rewritten, while string content is. That inconsistency does not garble output there (the template adds a generation prompt), so it is left out of this PR. Joining inside _handle_last_assistant_message would also change the Jinja-template path, so the change is kept to dsv41.

This PR was prepared with AI assistance (Claude Code). The submitter reviewed every changed line and the validation above.

🤖 Generated with Claude Code


CI States

Latest PR Test (Base): ❌ Run #39238811860
Latest PR Test (Extra): ❌ Run #39238811990
Latest PR Test (AMD ROCm 10): ❌ Run #39238811949

…ring content

On the dsv41 chat-encoding path, a final assistant message whose content is a
list of text parts was neither converted to a user message (as string content
is) nor used as the continue_final_message prefix, so the V4.1 encoder rendered
it as a finished turn ending at EOS and the model replied with empty or garbled
text. Join a text-only parts list with "\n\n" (the encoder's own join) before
_handle_last_assistant_message so it is handled exactly like string content.

Co-Authored-By: Claude Opus 5.5 (1M context) <[email protected]>
@JohnZhao01

Copy link
Copy Markdown
Contributor Author

Closing this as superseded. Since #45315 (merged), DeepSeek-V4.1 prompts render through deepseek-recipe. With deepseek-recipe 0.2.0, a final assistant message whose content is a list of text parts renders exactly like the same text as a string: without continue_final_message a new assistant turn is opened, and with it the text is continued. So this fix is no longer needed. Thanks!

@JohnZhao01 JohnZhao01 closed this Oct 24, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant