fix(server): hoist system messages to front for templates requiring system-first - #487
Open
rafaeldrincon wants to merge 2 commits into
Open
rafaeldrincon wants to merge 2 commits into
rafaeldrincon wants to merge 2 commits into
Conversation
…ystem-first Some chat templates (e.g. Qwen3.6) raise 'System message must be at the beginning' when the system message is not at index 0. render_messages() now reorders system messages to the front before passing to the template. This is a no-op when system is already first or absent.
Qwen3.6 chat template raises 'System message must be at the beginning' when a second system message appears after loop.first. Merging all system messages into a single one at index 0 fixes this. Tested with: system+user+system, user+system+user (hoisted+merged).
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.
Problem
Some chat templates (e.g. Qwen3.6) validate message order and raise:
when the system message is not at index 0. This causes a
400 Bad Requestfor callers that send system messages in non-first positions (e.g. OpenAI-compatible API gateways that reorder messages, or multi-turn conversations where a system message appears after a user message).Fix
render_messages()ingeneration.pynow hoists system messages to the front of the list before returning. This is a no-op when:Verification
Tested with Qwen3.6-35B-A3B (NVFP4):
POST /v1/chat/completionswith[user, system, user]→400 "System message must be at the beginning"200 OK, correct response200 OKTests
Three unit tests added to
tests/server/test_mm_input.py:test_render_messages_hoists_system_to_front— system not first → hoistedtest_render_messages_preserves_system_first— system already first → no-optest_render_messages_no_system_unchanged— no system → order preservedImpact