Instructions to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with libraries, inference providers, notebooks, and local apps. Follow these links to get started.
- Libraries
- Transformers
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with Transformers:
# Use a pipeline as a high-level helper from transformers import pipeline pipe = pipeline("image-text-to-text", model="HuggingFaceTB/SmolVLM2-2.2B-Instruct") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] pipe(text=messages)# pip install -U transformers accelerate # Load model directly from transformers import AutoProcessor, AutoModelForMultimodalLM processor = AutoProcessor.from_pretrained("HuggingFaceTB/SmolVLM2-2.2B-Instruct") model = AutoModelForMultimodalLM.from_pretrained("HuggingFaceTB/SmolVLM2-2.2B-Instruct", device_map="auto") messages = [ { "role": "user", "content": [ {"type": "image", "url": "https://huggingface.co/datasets/huggingface/documentation-images/resolve/main/p-blog/candy.JPG"}, {"type": "text", "text": "What animal is on the candy?"} ] }, ] inputs = processor.apply_chat_template( messages, add_generation_prompt=True, tokenize=True, return_dict=True, return_tensors="pt", ).to(model.device) outputs = model.generate(**inputs, max_new_tokens=256) print(processor.decode(outputs[0][inputs["input_ids"].shape[-1]:])) - Notebooks
- Google Colab
- Kaggle
- Local Apps Settings
- vLLM
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with vLLM:
Install from pip and serve model
# Install vLLM from pip: pip install vllm # Start the vLLM server: vllm serve "HuggingFaceTB/SmolVLM2-2.2B-Instruct" # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:8000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolVLM2-2.2B-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker
docker model run hf.co/HuggingFaceTB/SmolVLM2-2.2B-Instruct
- SGLang
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with SGLang:
Install from pip and serve model
# Install SGLang from pip: pip install sglang # Start the SGLang server: python3 -m sglang.launch_server \ --model-path "HuggingFaceTB/SmolVLM2-2.2B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolVLM2-2.2B-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }'Use Docker images
docker run --gpus all \ --shm-size 32g \ -p 30000:30000 \ -v ~/.cache/huggingface:/root/.cache/huggingface \ --env "HF_TOKEN=<secret>" \ --ipc=host \ lmsysorg/sglang:latest \ python3 -m sglang.launch_server \ --model-path "HuggingFaceTB/SmolVLM2-2.2B-Instruct" \ --host 0.0.0.0 \ --port 30000 # Call the server using curl (OpenAI-compatible API): curl -X POST "http://localhost:30000/v1/chat/completions" \ -H "Content-Type: application/json" \ --data '{ "model": "HuggingFaceTB/SmolVLM2-2.2B-Instruct", "messages": [ { "role": "user", "content": [ { "type": "text", "text": "Describe this image in one sentence." }, { "type": "image_url", "image_url": { "url": "https://cdn.britannica.com/61/93061-050-99147DCE/Statue-of-Liberty-Island-New-York-Bay.jpg" } } ] } ] }' - Docker Model Runner
How to use HuggingFaceTB/SmolVLM2-2.2B-Instruct with Docker Model Runner:
docker model run hf.co/HuggingFaceTB/SmolVLM2-2.2B-Instruct
Chat template: render string content instead of dropping it
Browse filesIf 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.*
- chat_template.json +1 -1
- tokenizer_config.json +1 -1
|
@@ -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 |
}
|
|
@@ -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>",
|