Image-Text-to-Text
Transformers
ONNX
Safetensors
English
idefics3
conversational
arjhinety commited on
Commit
da18135
·
verified ·
1 Parent(s): 7e3e67e

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

Browse files

Same change as [SmolVLM2-2.2B-Instruct#32](https://huggingface.co/HuggingFaceTB/SmolVLM2-2.2B-Instruct/discussions/32), which was merged; context in [transformers#50613](https://github.com/huggingface/transformers/issues/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):

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

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

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
  "eos_token": "<end_of_utterance>",
1174
  "legacy": false,
 
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
  "eos_token": "<end_of_utterance>",
1174
  "legacy": false,