Image-Text-to-Text
Transformers
ONNX
Safetensors
English
idefics3
conversational
arjhinety commited on
Commit
c8e6bed
·
verified ·
1 Parent(s): 81cd9a7

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
@@ -176,7 +176,7 @@
176
  "processor_class": "Idefics3Processor",
177
  "tokenizer_class": "GPT2Tokenizer",
178
  "truncation_side": "left",
179
- "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 %}",
180
  "unk_token": "<|endoftext|>",
181
  "vocab_size": 49152
182
  }
 
176
  "processor_class": "Idefics3Processor",
177
  "tokenizer_class": "GPT2Tokenizer",
178
  "truncation_side": "left",
179
+ "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 %}",
180
  "unk_token": "<|endoftext|>",
181
  "vocab_size": 49152
182
  }