arjhinety commited on
Commit
c1e8a52
·
verified ·
1 Parent(s): 482adb5

Chat template: render string content instead of dropping it

Browse files

If a message's `content` is a non-empty string rather than a list of typed parts, the chat template omits its text without an error. For example, a string system prompt next to a normal multimodal user turn renders as an empty turn:

```python
from transformers import AutoProcessor

proc = AutoProcessor.from_pretrained(
"HuggingFaceTB/SmolVLM2-2.2B-Instruct", revision="482adb537c021c86670beed01cd58990d01e72e4"
)
user = {"role": "user", "content": [{"type": "image"}, {"type": "text", "text": "Describe."}]}
for system in ("Be brief.", [{"type": "text", "text": "Be brief."}]):
msgs = [{"role": "system", "content": system}, user]
print(repr(proc.apply_chat_template(msgs, tokenize=False, add_generation_prompt=True)))
```

Output with transformers 5.22.2:

```
'<|im_start|>System: <end_of_utterance>\nUser:<image>Describe.<end_of_utterance>\nAssistant:'
'<|im_start|>System: Be brief.<end_of_utterance>\nUser:<image>Describe.<end_of_utterance>\nAssistant:'
```

Expected: both start with `System: Be brief.`. The same happens to any message with string content, e.g. an assistant reply appended to the history as a string. `AutoTokenizer` (which reads the same template from `tokenizer_config.json`) drops string content the same way (checked on 5.16.1 and 5.22.2).

I ran into this in [small-mind-companion](https://github.com/arjhinety/small-mind-companion), where an evaluation run scored this model 0.0 on every text-only category because its string prompts reached the model as empty turns ([notes](https://github.com/arjhinety/small-mind-companion/blob/2e7c393cb9f82e11f3dc0082800896a66c997d60/docs/model_quirks.md#L56-L71)).

**Cause.** The template reads `message['content'][0]['type']` and loops over `message['content']` expecting dicts. For a string, `[0]` is a character and `line['type']` is undefined, so neither the text branch nor the image branch outputs anything.

**Change.** This PR adds a string branch before the existing list handling, the same pattern as [Qwen2-VL's template](https://huggingface.co/Qwen/Qwen2-VL-2B-Instruct/blob/main/chat_template.json), in both `chat_template.json` and `tokenizer_config.json` (they hold the same template):

```diff
-{{message['role'] | capitalize}}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% endif %}{% endfor %}<end_of_utterance>
+{{message['role'] | capitalize}}{% if message['content'] is string %}{{': ' + message['content']}}{% else %}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% endif %}{% endfor %}{% endif %}<end_of_utterance>
```

With the patched files loaded through `AutoProcessor` (transformers 5.22.2), the script above prints `System: Be brief.` on both lines. List-content output is unchanged on five list-shaped cases (text only, image first, text then image, two images, multi-turn; with and without `add_generation_prompt`), rendered with Jinja using the same sandbox and whitespace settings as transformers. String content renders like the equivalent `[{"type": "text", ...}]`. An empty string, which currently raises, now renders an empty turn, the same as `[{"type": "text", "text": ""}]`; `content: None` and an empty list still raise. Not tested with other template engines.

This doesn't fix conversations that contain a video: for those, transformers' SmolVLM processor substitutes its own built-in template, which has the same problem. I've reported that part at https://github.com/huggingface/transformers/issues/50613.

The same content handling is in SmolVLM2-500M-Video/256M-Video-Instruct and SmolVLM-Instruct/256M-Instruct/500M-Instruct, which use the same template; I can open the same change there if this one looks right. Idefics3-8B-Llama3's `chat_template.json` has it too, in a slightly different template.

*Written with help from an AI coding assistant, which traced the cause and wrote the reproduction, the change and the checks. I reran the reproduction above and reviewed the diff.*

Files changed (2) hide show
  1. chat_template.json +1 -1
  2. tokenizer_config.json +1 -1
chat_template.json CHANGED
@@ -1,3 +1,3 @@
1
  {
2
- "chat_template": "<|im_start|>{% for message in messages %}{{message['role'] | capitalize}}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% endif %}{% endfor %}<end_of_utterance>\n{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}"
3
  }
 
1
  {
2
+ "chat_template": "<|im_start|>{% for message in messages %}{{message['role'] | capitalize}}{% if message['content'] is string %}{{': ' + message['content']}}{% else %}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% endif %}{% endfor %}{% endif %}<end_of_utterance>\n{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}"
3
  }
tokenizer_config.json CHANGED
@@ -1168,7 +1168,7 @@
1168
  "<end_of_utterance>"
1169
  ],
1170
  "bos_token": "<|im_start|>",
1171
- "chat_template": "<|im_start|>{% for message in messages %}{{message['role'] | capitalize}}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% endif %}{% endfor %}<end_of_utterance>\n{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}",
1172
  "clean_up_tokenization_spaces": false,
1173
  "end_of_utterance_token": "<end_of_utterance>",
1174
  "eos_token": "<end_of_utterance>",
 
1168
  "<end_of_utterance>"
1169
  ],
1170
  "bos_token": "<|im_start|>",
1171
+ "chat_template": "<|im_start|>{% for message in messages %}{{message['role'] | capitalize}}{% if message['content'] is string %}{{': ' + message['content']}}{% else %}{% if message['content'][0]['type'] == 'image' %}{{':'}}{% else %}{{': '}}{% endif %}{% for line in message['content'] %}{% if line['type'] == 'text' %}{{line['text']}}{% elif line['type'] == 'image' %}{{ '<image>' }}{% endif %}{% endfor %}{% endif %}<end_of_utterance>\n{% endfor %}{% if add_generation_prompt %}{{ 'Assistant:' }}{% endif %}",
1172
  "clean_up_tokenization_spaces": false,
1173
  "end_of_utterance_token": "<end_of_utterance>",
1174
  "eos_token": "<end_of_utterance>",