Skip to main content

AutoProcessor.from_pretrained

Load the processor for tokenizing text and preprocessing images/videos.

Parameters

string
required
Model identifier from Hugging Face Hub or local path. Should match the model being used.

Returns

Returns a Qwen3VLProcessor instance that handles both text tokenization and image/video preprocessing.

processor.apply_chat_template

Format conversation messages into model inputs with proper tokenization and vision preprocessing.

Parameters

list[dict]
required
Conversation messages in chat format. Each message contains role and content.Message Format:
Content Types:
  • {"type": "text", "text": "..."}
  • {"type": "image", "image": "url|path|base64"}
  • {"type": "video", "video": "url|path"}
Image Sources:
  • URL: "https://example.com/image.jpg"
  • Local file: "file:///path/to/image.jpg"
  • Base64: "data:image;base64,/9j/..."
Video Sources:
  • URL: "https://example.com/video.mp4"
  • Local file: "file:///path/to/video.mp4"
  • Frame list: ["file:///frame1.jpg", "file:///frame2.jpg", ...]
bool
default:"True"
Whether to tokenize the text into input IDs.
  • True: Returns tokenized tensors ready for model input
  • False: Returns formatted text string
bool
default:"False"
Whether to add the assistant prompt token at the end for generation.Should be True when generating responses, False when formatting training data.
bool
default:"False"
Whether to return a dictionary with named fields.
  • True: Returns dict with input_ids, attention_mask, pixel_values, etc.
  • False: Returns tuple
string
Type of tensors to return.
  • "pt": PyTorch tensors (most common)
  • "np": NumPy arrays
  • None: Python lists
bool | string
default:"False"
Padding strategy for batch processing.
  • True: Pad to longest sequence in batch
  • False: No padding
  • "max_length": Pad to model’s max length
Required for batch generation.
bool
default:"False"
Whether to add labels (“Picture 1:”, “Video 1:”) to visual inputs.Helpful when processing multiple images/videos for better reference.
float
default:"2"
Frame sampling rate for videos (frames per second).
int
Exact number of frames to sample from video. Overrides fps if set.

Returns

torch.Tensor
Tokenized input text.Shape: (batch_size, sequence_length)
torch.Tensor
Mask indicating which tokens should be attended to.Shape: (batch_size, sequence_length)
torch.Tensor
Preprocessed image data (if images present in messages).Shape: (num_images, channels, height, width)
torch.Tensor
Video grid dimensions: temporal, height, width (if videos present).

Complete Examples

Advanced: Controlling Visual Token Budget

Control the number of visual tokens by adjusting pixel budgets:

Notes

Message Format Requirements:
  • Each message must have "role" ("user", "assistant", or "system")
  • Content can be a string or list of content items
  • Content items must specify "type" ("text", "image", or "video")
For batch generation:
  1. Set processor.tokenizer.padding_side = 'left'
  2. Include padding=True in apply_chat_template()
When using add_vision_id=True, images and videos are labeled as “Picture 1:”, “Picture 2:”, “Video 1:”, etc. This helps the model reference specific visual inputs in its response.