Skip to content

fix(server): stop merging consecutive user and system messages - #980

Open
NVolcz wants to merge 1 commit into
ROCm:mainfrom
NVolcz:separate-merged-messages
Open

NVolcz wants to merge 1 commit into
ROCm:mainfrom
NVolcz:separate-merged-messages

Conversation

@NVolcz

@NVolcz NVolcz commented Nov 7, 2026

Copy link
Copy Markdown

normalize_messages merges consecutive user messages, and consecutive system messages, into one message, and puts nothing between their texts, so the words of two messages run together:

<|turn>user
Hi.What is 2+2?<turn|>

Clients do send such messages. For example, Pi drops a reply the user aborted, which leaves two user messages in a row.

The messages are now passed on as they are, and the chat template decides how to render them. This is what llama.cpp, vLLM, MLX-LM, TGI and transformers do; Ollama merges them only for models on its older template path, and renders them as separate turns for Gemma 4. Gemma 4's template renders separate turns:

<|turn>user
Hi.<turn|>
<|turn>user
What is 2+2?<turn|>

A template that requires the roles to alternate, such as Gemma 3's, now rejects such a request, as it does on llama.cpp and vLLM.

How to reproduce

Send two user messages in a row, then ask about the second one:

curl -s http://localhost:52625/v1/chat/completions -H 'Content-Type: application/json' -d '{
  "model": "gemma4-it:e4b", "temperature": 0, "think": false, "max_tokens": 20,
  "messages": [
    {"role": "user", "content": "Apples are red."},
    {"role": "user", "content": "Bananas are yellow."},
    {"role": "user", "content": "What is the first word of my second message? Answer with that word only."}
  ]}' | jq -r '.choices[0].message.content'

On main the answer is What: the model sees one message, "Apples are red.Bananas are yellow.What is the first word…". On this branch it is Bananas.

I found this while reading the prompts flm renders, with a small debug branch that logs every prompt to a file (log-prompt, FLM_LOG_PROMPT=<file>).

normalize_messages merged consecutive user messages, and consecutive system messages, into
one message and put nothing between their texts, so "Hi." followed by "What is 2+2?" reached
the model as "Hi.What is 2+2?". Clients send such messages, for example Pi after dropping a
reply the user aborted.

They are now passed on as separate messages, and the chat template decides how to render
them, as llama.cpp, vLLM, MLX-LM, TGI and transformers do. Gemma 4 renders them as separate
turns. A template that requires the roles to alternate, such as Gemma 3's, now rejects such a
request instead of seeing the messages merged.
@tawei-amd
tawei-amd requested a review from zaneni6 November 7, 2026 21:54

This branch has not been deployed

No deployments
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