Repository navigation
[Fix] dsv41: Handle a final assistant message with text parts like string content - #45152
Closed
JohnZhao01 wants to merge 1 commit into
Closed
JohnZhao01 wants to merge 1 commit into
JohnZhao01 wants to merge 1 commit into
Conversation
…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]>
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 |
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
On the DeepSeek-V4.1 chat-encoding path (
chat_encoding_spec == "dsv41"), a final assistant message whosecontentis 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 tomain@5d9c956f._handle_last_assistant_messageonly handles string content (serving_chat.pyL442–L443). A parts list is neither converted to a user message (withoutcontinue_final_message, as string content is) nor taken as the continuation prefix (withcontinue_final_message=true).encoding_dsv41.process_image_messagesL617) 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._handle_last_assistant_message(serving_chat.pyL1579–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_dsv41_final_assistant_text_parts_match_string_contentchecks that a two-part list renders exactly like the joined string, without and withcontinue_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(itsserving_chat.py,encoding_dsv41.pyandtest_serving_chat.pyare byte-identical tomain@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/completionswith an explicitchat_template_kwargs.thinking, T=1.0, top_p=0.95,max_tokens2048, 10 requests per cell.max_tokens)max_tokens)max_tokens)continue_final_message(control)finish_reason=stopafter a reasoning block; string content takes the unchanged path, so this is run-to-run variation)continue_final_messagecontinue_final_messagecontinue_final_message@@CODE_0@@or@@code_placeholder@@, and 1 / 1 have</think>incontent@@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.finish_reason=stopafter a short reasoning block (sometimes only a few characters). After it, all 3 remaining empty replies stopped withfinish_reason=length(the 2048-token budget was spent in reasoning), as did the 3 empty string-control replies.Unit tests,
test/registered/unit/entrypoints/openai/test_serving_chat.pyon the same image: before 159 passed (119 subtests); after 160 passed (121 subtests), no failures. Control: the new test run against the unchangedserving_chat.pyfails in both subtests (without and withcontinue_final_message).Speed Tests and Profiling
Not applicable: the change joins the text parts of one message on the host before encoding.
Checklist
Notes
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._handle_last_assistant_messagewould 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