qgallouedec HF Staff commited on
Commit
227620d
·
verified ·
1 Parent(s): ee0ef60

Drop the trailing space after the last system text part

Browse files

Same change as https://huggingface.co/google/gemma-4-E2B-it/discussions/53.

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

```python
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.

Files changed (1) hide show
  1. chat_template.jinja +2 -1
chat_template.jinja CHANGED
@@ -199,7 +199,8 @@
199
  {{- messages[0]['content'] | trim -}}
200
  {%- elif messages[0]['content'] is sequence -%}
201
  {%- for item in messages[0]['content'] -%}
202
- {{- item['text'] | trim + ' '-}}
 
203
  {%- endfor -%}
204
  {%- endif -%}
205
  {%- set loop_messages = messages[1:] -%}
 
199
  {{- messages[0]['content'] | trim -}}
200
  {%- elif messages[0]['content'] is sequence -%}
201
  {%- for item in messages[0]['content'] -%}
202
+ {{- item['text'] | trim -}}
203
+ {%- if not loop.last -%}{{- ' ' -}}{%- endif -%}
204
  {%- endfor -%}
205
  {%- endif -%}
206
  {%- set loop_messages = messages[1:] -%}