Image-Text-to-Text
Transformers
ONNX
Safetensors
English
idefics3
conversational

Chat template: render string `content` instead of dropping it

#37
by arjhinety - opened

Same change as SmolVLM2-2.2B-Instruct#32, which was merged; context in transformers#50613.

If a message's content is a string rather than a list of typed parts, the chat template drops its text without an error, so a string system prompt renders as System: <end_of_utterance>. This PR adds a string branch before the existing list handling, in chat_template.json and tokenizer_config.json (they hold the same template, identical to the one changed there):

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

Checked with transformers 5.22.2 by loading this repo's files with the patched templates. Through AutoProcessor, a string system prompt now renders as System: Be brief., the same as [{"type": "text", "text": "Be brief."}], and list-content output is unchanged (text only, image first, text then image, two images, multi-turn; with and without add_generation_prompt). Through AutoTokenizer, a string user turn renders like the typed-part form. I haven't tested other template engines.

Written with help from an AI coding assistant, which made the change and ran the checks. I reviewed the diff and the check output.

RaushanTurganbay changed pull request status to merged

Sign up or log in to comment