transformers - ✅(Solved) Fix TypeError: Can't compile non template nodes [1 pull requests, 1 comments, 1 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
huggingface/transformers#45084Fetched 2026-04-08 01:45:18
View on GitHub
Comments
1
Participants
1
Timeline
7
Reactions
0
Participants
Timeline (top)
mentioned ×2subscribed ×2commented ×1cross-referenced ×1

Error Message

@Z590M-GAMING-X:~/Documents/Code//api$ /home//Documents/Code//api/.venv/bin/python /home//Documents/Code//api/main.py bcn_weather.mp3: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 353k/353k [00:01<00:00, 292kB/s] Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads. Traceback (most recent call last): File "/home//Documents/Code//api/main.py", line 23, in <module> inputs = processor.apply_chat_template(conversation) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/transformers/models/voxtral/processing_voxtral.py", line 175, in apply_chat_template template_kwargs = _get_template_variables(chat_template) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 396, in _get_template_variables compiled = _compile_jinja_template(chat_template) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 421, in _compile_jinja_template return _cached_compile_jinja_template(chat_template) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 495, in _cached_compile_jinja_template return jinja_env.from_string(chat_template) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1111, in from_string return cls.from_code(self, self.compile(source), gs, None) ^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 764, in compile source = self._generate(source, name, filename, defer_init=defer_init) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 694, in _generate return generate( # type: ignore ^^^^^^^^^^^^^^^^^^^^^^^^^ File "/home//Documents/Code//api/.venv/lib/python3.12/site-packages/jinja2/compiler.py", line 112, in generate raise TypeError("Can't compile non template nodes") TypeError: Can't compile non template nodes

Fix Action

Fixed

PR fix notes

PR #45090: Fix TypeError when chat_template is None in VoxtralProcessor

Description (problem / solution / changelog)

Description

Fixes #45084

The VoxtralProcessor.apply_chat_template method was calling _get_template_variables(chat_template) without first checking if chat_template was None. This caused a TypeError: Can't compile non template nodes when users called apply_chat_template without explicitly providing a chat_template, even when the processor had a default chat template configured.

Changes

This PR adds the same chat_template resolution logic that exists in the base ProcessingMixin.apply_chat_template method, ensuring that:

  1. If chat_template is None, it uses the processor's default template
  2. If the processor has multiple templates, it uses the 'default' one
  3. Proper error messages are shown if no template is available

Testing

  • Verified the fix with a test script that confirms the resolution logic is in place
  • The fix follows the same pattern used in the base ProcessingMixin class

Changed files

  • src/transformers/models/voxtral/processing_voxtral.py (modified, +17/-0)

Code Example

from transformers import VoxtralForConditionalGeneration, VoxtralProcessor, BitsAndBytesConfig
from huggingface_hub import hf_hub_download
from transformers.audio_utils import load_audio_as

audio_url = "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3"
audio_path = hf_hub_download(repo_id="hf-internal-testing/dummy-audio-samples", filename="bcn_weather.mp3", repo_type="dataset")
audio_base64 = load_audio_as(audio_path, return_format="base64", force_mono=True)

# audio + text
conversation = [
    {
        "role": "user",
        "content": [
            {"type": "audio", "url": audio_url},
            {"type": "audio", "path": audio_path},
            {"type": "audio", "base64": audio_base64},
            {"type": "text", "text": "How many audio do you hear?"},
        ],
    },
]

processor = VoxtralProcessor.from_pretrained("mistralai/Voxtral-Mini-3B-2507")
inputs = processor.apply_chat_template(conversation)

---

***@Z590M-GAMING-X:~/Documents/Code/***/api$ /home/***/Documents/Code/***/api/.venv/bin/python /home/***/Documents/Code/***/api/main.py
bcn_weather.mp3: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 353k/353k [00:01<00:00, 292kB/s]
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
Traceback (most recent call last):
  File "/home/***/Documents/Code/***/api/main.py", line 23, in <module>
    inputs = processor.apply_chat_template(conversation)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/models/voxtral/processing_voxtral.py", line 175, in apply_chat_template
    template_kwargs = _get_template_variables(chat_template)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 396, in _get_template_variables
    compiled = _compile_jinja_template(chat_template)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 421, in _compile_jinja_template
    return _cached_compile_jinja_template(chat_template)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 495, in _cached_compile_jinja_template
    return jinja_env.from_string(chat_template)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1111, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
                               ^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 764, in compile
    source = self._generate(source, name, filename, defer_init=defer_init)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 694, in _generate
    return generate(  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/compiler.py", line 112, in generate
    raise TypeError("Can't compile non template nodes")
TypeError: Can't compile non template nodes
RAW_BUFFERClick to expand / collapse

System Info

  • transformers version: 5.4.0
  • Platform: Linux-6.17.0-19-generic-x86_64-with-glibc2.39
  • Python version: 3.12.3
  • Huggingface_hub version: 1.8.0
  • Safetensors version: 0.7.0
  • Accelerate version: 1.13.0
  • Accelerate config: not found
  • DeepSpeed version: not installed
  • PyTorch version (accelerator?): 2.11.0+cu126 (CUDA)
  • Using distributed or parallel set-up in script?: no
  • Using GPU in script?: yes
  • GPU type: NVIDIA GeForce GTX 1080

Who can help?

@eustlb

Information

  • The official example scripts
  • My own modified scripts

Tasks

  • An officially supported task in the examples folder (such as GLUE/SQuAD, ...)
  • My own task or dataset (give details below)

Reproduction

from transformers import VoxtralForConditionalGeneration, VoxtralProcessor, BitsAndBytesConfig
from huggingface_hub import hf_hub_download
from transformers.audio_utils import load_audio_as

audio_url = "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3"
audio_path = hf_hub_download(repo_id="hf-internal-testing/dummy-audio-samples", filename="bcn_weather.mp3", repo_type="dataset")
audio_base64 = load_audio_as(audio_path, return_format="base64", force_mono=True)

# audio + text
conversation = [
    {
        "role": "user",
        "content": [
            {"type": "audio", "url": audio_url},
            {"type": "audio", "path": audio_path},
            {"type": "audio", "base64": audio_base64},
            {"type": "text", "text": "How many audio do you hear?"},
        ],
    },
]

processor = VoxtralProcessor.from_pretrained("mistralai/Voxtral-Mini-3B-2507")
inputs = processor.apply_chat_template(conversation)
***@Z590M-GAMING-X:~/Documents/Code/***/api$ /home/***/Documents/Code/***/api/.venv/bin/python /home/***/Documents/Code/***/api/main.py
bcn_weather.mp3: 100%|██████████████████████████████████████████████████████████████████████████████████████████████████████████████████████| 353k/353k [00:01<00:00, 292kB/s]
Warning: You are sending unauthenticated requests to the HF Hub. Please set a HF_TOKEN to enable higher rate limits and faster downloads.
Traceback (most recent call last):
  File "/home/***/Documents/Code/***/api/main.py", line 23, in <module>
    inputs = processor.apply_chat_template(conversation)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/models/voxtral/processing_voxtral.py", line 175, in apply_chat_template
    template_kwargs = _get_template_variables(chat_template)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 396, in _get_template_variables
    compiled = _compile_jinja_template(chat_template)
               ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 421, in _compile_jinja_template
    return _cached_compile_jinja_template(chat_template)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/transformers/utils/chat_template_utils.py", line 495, in _cached_compile_jinja_template
    return jinja_env.from_string(chat_template)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 1111, in from_string
    return cls.from_code(self, self.compile(source), gs, None)
                               ^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 764, in compile
    source = self._generate(source, name, filename, defer_init=defer_init)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/environment.py", line 694, in _generate
    return generate(  # type: ignore
           ^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/home/***/Documents/Code/***/api/.venv/lib/python3.12/site-packages/jinja2/compiler.py", line 112, in generate
    raise TypeError("Can't compile non template nodes")
TypeError: Can't compile non template nodes

Expected behavior

apply_chat_template should applies the model's chat completion template given a conversation without error

extent analysis

Fix Plan

The error occurs because the apply_chat_template method expects a template string, but the conversation is passed as a list of dictionaries.

To fix this issue, you need to modify the conversation format to match the expected template string.

Here are the steps to fix the issue:

  • Modify the conversation format to a string that can be used as a template.
  • Use the apply_template method instead of apply_chat_template to apply the template.
from transformers import VoxtralForConditionalGeneration, VoxtralProcessor, BitsAndBytesConfig
from huggingface_hub import hf_hub_download
from transformers.audio_utils import load_audio_as

audio_url = "https://huggingface.co/datasets/hf-internal-testing/dummy-audio-samples/resolve/main/bcn_weather.mp3"
audio_path = hf_hub_download(repo_id="hf-internal-testing/dummy-audio-samples", filename="bcn_weather.mp3", repo_type="dataset")
audio_base64 = load_audio_as(audio_path, return_format="base64", force_mono=True)

# audio + text
conversation = """
User: How many audio do you hear?
Assistant: 
"""

processor = VoxtralProcessor.from_pretrained("mistralai/Voxtral-Mini-3B-2507")
inputs = processor.apply_template(conversation)

Verification

To verify that the fix worked, you can check if the inputs variable is populated correctly after applying the template. You can also use the print function to print the inputs variable and verify its contents.

print(inputs)

Extra Tips

  • Make sure to check the documentation for the apply_template method to understand the expected format of the template string.
  • If you need to use a more complex conversation format, you may need to modify the template string accordingly.
  • You can also use the apply_template method with a custom template string to apply a specific template to the conversation.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

FAQ

Expected behavior

apply_chat_template should applies the model's chat completion template given a conversation without error

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING