Nodes/ComfyUI-PDiD-LLM-Nodes/Apply Chat Template
ComfyUI Node

Apply Chat Template

Hand a model its own tokenizer's chat template

By power88·Created 10 months ago·Updated 4 months ago· 0
Apply Chat Template
  • image
  • video
  • audio
  • extra_parameters
  • image
  • video
  • audio
  • formatted_prompt
system_promptYou are a helpful assistant.
user_prompt
chat_template

Most of this pack calls APIs. Apply Chat Template is the odd one out: it doesn't call anything. It formats a prompt. Every serious LLM ships with a chat template - a little Jinja2 script in its tokenizer_config.json that defines exactly how system, user, and image content get wrapped in special tokens before the model sees them. This node takes that template, applies it to your system prompt, user prompt, and any attached image/video/audio, and hands you the formatted string. It's for when the default template a text-generation node uses is wrong for your model, and you want to hand it the model's own.

How it works

You paste the template in, the node builds a standard message list - a system message, then a user message containing the prompt plus placeholders for any attached media - and renders the template with Jinja2. The interesting detail is that attached image/video/audio aren't actually sent anywhere: they're represented as dummy placeholders during rendering (the structure has to exist for the template to expand), then passed straight through on the outputs unchanged, so you keep your assets in the graph. What you actually consume is the formatted_prompt string, which is ready to feed into a text-generation node that takes a pre-formatted prompt - the code comment points at ComfyUI's own TextGenerate node with its default template switched off.

Inputs that matter:

  • chat_template - paste the template from the model's repo on HuggingFace. The tooltip says it plainly: it's in the original repo, in tokenizer_config.json under chat_template. This is the one field beginners get wrong, and the failure is loud (see below).
  • system_prompt / user_prompt - your actual conversation.
  • image / video / audio (optional) - attached media, passed through on the outputs. video is treated as a batch of image frames.
  • extra_parameters (optional) - reasoning effort and thinking get injected as template variables when present.

Outputs: formatted_prompt (the string you use), plus pass-through image, video, and audio.

Why you'd reach for it

Because chat templates are per-model, and a wrong one produces subtly broken text. A Mistral template won't emit Qwen's special tokens, and an old model's template may not handle multimodal content at all. If you're running a model whose template differs from what your text-generation node assumes, this node is the escape hatch: grab the model's own template, format with it, done. It's the kind of node you install, don't touch for months, and then rescue a workflow with at 2 a.m.

Install and gotchas

Same install as the whole pack - ComfyUI Manager search ComfyUI-PDiD-LLM-Nodes, or:

cd ComfyUI/custom_nodes
git clone https://github.com/power88/ComfyUI-PDiD-LLM-Nodes

Restart. You need ComfyUI 0.3.51+ (the pack is built on the new V3 node schema), and there's nothing to download - this node is pure formatting.

The main trap is real and you'll feel it immediately: the template is rendered with StrictUndefined, meaning if the template references a variable the node doesn't provide, rendering errors instead of quietly producing garbage. A template expecting variables like tools or custom fields will fail until you provide them (the node passes messages, tools, add_generation_prompt, and optionally thinking/reasoning_effort). So paste the exact template for your model, not one from a different architecture, and be ready for a loud error if the model's template wants something extra.

CategoryLLM

Inputs (7)

NameTypeDefaultDescription
system_promptSTRINGYou are a helpful assistant.
user_promptSTRING
chat_templateSTRINGPut chat template here. Can be found in original repo of model in huggingface.
imageoptIMAGE
videooptIMAGEVideo frames as image batch.
audiooptAUDIO
extra_parametersoptEXTRA_PARAMETERSThe extra parameters for the LLM model.

Outputs (4)

NameTypeDescription
imageIMAGE
videoIMAGE
audioAUDIO
formatted_promptSTRING