mlx-vlm: Qwen2.5-VL failing

I am getting this error when using the new Qwen2.5-VL models.

Command:

python -m mlx_vlm.generate --model mlx-community/Qwen2.5-VL-3B-Instruct-3bit --max-tokens 100 --temp 0.0 --prompt "Describe this image." --image "frames/frame-01.jpg"
Fetching 11 files: 100%|████████████████████████████████████████████████████████████████| 11/11 [00:00<00:00, 55056.50it/s]
Traceback (most recent call last):
  File "<frozen runpy>", line 198, in _run_module_as_main
  File "<frozen runpy>", line 88, in _run_code
  File "/Users/moroneyt/Documents/VLM-Testing/.venv/lib/python3.12/site-packages/mlx_vlm/generate.py", line 156, in <module>
    main()
  File "/Users/moroneyt/Documents/VLM-Testing/.venv/lib/python3.12/site-packages/mlx_vlm/generate.py", line 101, in main
    prompt = apply_chat_template(processor, config, prompt, num_images=len(args.image))
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moroneyt/Documents/VLM-Testing/.venv/lib/python3.12/site-packages/mlx_vlm/prompt_utils.py", line 159, in apply_chat_template
    return processor.apply_chat_template(
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moroneyt/Documents/VLM-Testing/.venv/lib/python3.12/site-packages/transformers/tokenization_utils_base.py", line 1683, in apply_chat_template
    rendered_chat = compiled_template.render(
                    ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Users/moroneyt/Documents/VLM-Testing/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1295, in render
    self.environment.handle_exception()
  File "/Users/moroneyt/Documents/VLM-Testing/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 942, in handle_exception
    raise rewrite_traceback_stack(source=source)
  File "<template>", line 23, in top-level template code
TypeError: can only concatenate str (not "list") to str

About this issue

  • Original URL
  • State: closed
  • Created 2 years ago
  • Reactions: 2
  • Comments: 17 (7 by maintainers)

Most upvoted comments

Thanks to that tip this worked for me:

uv run --with 'numpy<2' \
  --with 'git+https://github.com/huggingface/transformers' \
  --with mlx-vlm \
  python -m mlx_vlm.generate \
    --model mlx-community/Qwen2.5-VL-7B-Instruct-8bit \
    --max-tokens 100 \
    --temp 0.0 \
    --prompt "Describe this image." \
    --image path-to-image.png

Result on my blog: https://simonwillison.net/2025/Jan/27/qwen25-vl-qwen25-vl-qwen25-vl/#qwen-vl-mlx-vlm

v4.48.1 indeed causes the issue.

But if you install transformers from source it should work 👌🏽

Alternately, this seems to work without having to modify downloaded files:

Change the function mlx_vlm.generate.get_model_and_processors to the following:

def get_model_and_processors(model_path, adapter_path):
    path = get_model_path(model_path)
    with open(path / "chat_template.json") as f:
        templ = json.loads(f.read())
        templ = templ["chat_template"]

    config = load_config(model_path, trust_remote_code=True)
    model, processor = load(
        model_path, adapter_path=adapter_path, lazy=False, trust_remote_code=True, chat_template=templ
    )
    return model, processor, config

we explicitly load and specify the chat template when loading the processor so it picks up the correct one.

Just verified working (latest main branch of this repo, transformers==4.48.1)

I have checked and the chat template is included with the downloaded model files, so it seems that something else is causing this error.

Most welcome!

Great article 🔥🙌🏽

I would love if we had a cookbook like that for mlx-vlm.

We already have a few recipes (here) but we definitely need more.

That’s interesting, because if you install transformers from source it works without any changes

v4.49.0.dev0

pip install git+https://github.com/huggingface/transformers

This appears to be because the Qwen2.5-VL models come with a chat_template.json file (c.f. here), which has the jinja2 template as a string value in a json object.

The transformers library expects, by default, to find the chat template as jinja2 (not inside a JSON object) in a file called chat_template.jinja (c.f. here) under the model path. Not finding one, it defaults to a generic template, which doesn’t match the format in which mlx_vlm.generate passes in its messages.

This can be temporarily worked around by copying the chat template from within the JSON file I linked (for example), save it as chat_template.jinja in the same directory as the model (e.g. /Users/<your user>/.cache/huggingface/hub/models--mlx-community--Qwen2.5-VL-3B-Instruct-3bit/snapshots/<some snapshot id>/ – poke around with ls to find the right directory).

I’ll post a minimal reproducible test in a minute