Chat template: render string content instead of dropping it

#32

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:

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, 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).

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, in both chat_template.json and tokenizer_config.json (they hold the same template):

-{{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.

Hugging Face Smol Models Research org

yeah, lgtm, tested locally and works for dict and string inputs along with mm-data

RaushanTurganbay changed pull request status to merged

Sign up or log in to comment