Drop the trailing space after the last system text part

#53
by qgallouedec HF Staff - opened

A system message whose content is a list of text parts renders with a space after every part, including the last one:

tok.apply_chat_template([{"role": "system", "content": "SYS."}, {"role": "user", "content": "U"}], tokenize=False)
# '<bos><|turn>system\nSYS.<turn|>\n<|turn>user\nU<turn|>\n'
tok.apply_chat_template([{"role": "system", "content": [{"type": "text", "text": "SYS."}]}, {"role": "user", "content": "U"}], tokenize=False)
# '<bos><|turn>system\nSYS. <turn|>\n<|turn>user\nU<turn|>\n'

The same message gives two different token sequences. This shows up in fine-tuning. Trainers that convert content to parts for processors (TRL does this for every vision-language model) train on SYS. <turn|>, while inference with a plain string sees SYS.<turn|>. User and model turns don't have the problem, because they only trim and don't append anything.

This change emits the space only between parts. A string and a single part now render identically, and two parts still render as A. B.. Every Gemma 4 -it template (E2B, E4B, 12B, 26B-A4B, 31B) has the same line.

Ready to merge
This branch is ready to get merged automatically.

Sign up or log in to comment