apply_chat_template()
Format a prompt (or conversation history) into the string that the model’s tokenizer expects. Internally looks up the model type from the config, builds message objects in the correct format for that model, then applies the tokenizer’s chat template.
Signature
Parameters
The processor returned by
load(). Used to apply the tokenizer’s chat template.The model configuration dict or config object. Must contain a
model_type key. Retrieve with model.config or load_config().The user input. Three forms are accepted:
str— A single text prompt.dict— A single message withroleandcontentkeys.list— A conversation history: a list of strings, dicts, or objects with.role/.contentattributes.
Append the model’s generation-prompt suffix (e.g.
<|im_start|>assistant) so the model knows to generate a response.When
True, return the intermediate list of message dicts instead of the fully rendered chat template string. Useful for inspection or custom post-processing.Number of images in the input. Image tokens are injected into the first user message.
Number of audio files in the input. Audio tokens are injected into the first user message.
Pass
enable_thinking=True to the underlying chat template for reasoning models such as Qwen3.5.Path or URL to a video file. When provided, a video message is constructed instead of image tokens. Supported by Qwen2-VL, Qwen2.5-VL, Qwen3-VL, and related models.
Maximum pixels per video frame (
224 * 224 = 50176 by default). Used with video.Frames per second to extract from the video. Used with
video.Returns
The chat-template-rendered string ready to pass to
generate(). When return_messages=True, returns the list of message dicts before template rendering. For paligemma, molmo, and florence2 model types, returns only the last message dict.Examples
- Text only
- With images
- With audio
- With video
- Multi-turn conversation
get_message_json()
Build a single message dict in the format expected by a specific model. This is called internally by apply_chat_template() but is also available for constructing messages manually.
Signature
Parameters
The model type string (the
model_type value from the config, e.g. "qwen2_vl", "llava", "gemma3").The text content of the message.
Message role:
"user", "assistant", or "system".Skip injecting image tokens even when
num_images > 0. Used for all but the first turn in multi-turn conversations.Skip injecting audio tokens even when
num_audios > 0.Number of image tokens to inject.
Number of audio tokens to inject.
Video path or URL. When provided, returns a video message dict instead of an image message.
Returns
A message dict (e.g.
{"role": "user", "content": [...]}) or a plain string, depending on the model’s expected format.Example
MessageFormat enum
MessageFormat defines the message-structure strategy for each model family. apply_chat_template and get_message_json look up the appropriate format from the MODEL_CONFIG mapping.
| Value | Description | Example models |
|---|---|---|
LIST_WITH_IMAGE | Content is a list; {"type": "image"} appended after text | qwen2_vl, llava, mllama, idefics2 |
LIST_WITH_IMAGE_FIRST | Image tokens placed before text in the list | qwen2_5_vl, qwen3_vl, smolvlm, mistral3 |
LIST_WITH_IMAGE_URL_FIRST | Uses {"type": "image_url"} objects, image first | ernie4_5_moe_vl |
LIST_WITH_IMAGE_TYPE | Content items use {"type": "content"} text nodes | internvl_chat |
LIST_WITH_IMAGE_TYPE_TEXT | Content items use {"type": "text"} nodes | gemma3n, pixtral |
IMAGE_TOKEN | Image token <image> prepended to text string | multi_modality, minicpmo |
IMAGE_TOKEN_PIPE | Image token <|image|> prepended to text string | jina_vlm |
START_IMAGE_TOKEN | <start_of_image> appended after text | gemma3 |
IMAGE_TOKEN_NEWLINE | <image>\n prepended to text | deepseek_vl_v2, phi4-siglip |
NUMBERED_IMAGE_TOKENS | <|image_1|>, <|image_2|> … prepended | phi3_v, phi4mm |
PROMPT_ONLY | No image tokens; returns prompt string as-is | florence2, molmo |
PROMPT_WITH_IMAGE_TOKEN | <image> × N prepended to prompt string | paligemma |
VIDEO_WITH_TEXT | Video message dict with text | Qwen2/2.5/3-VL |
You rarely need to use
MessageFormat directly. apply_chat_template() selects the correct format automatically based on model_type.