Skip to content

vllm.renderers.params

Classes:

  • ChatParams

    Configuration to control how to parse chat messages.

  • TokenizeParams

    Configuration to control how prompts are tokenized.

ChatParams dataclass

Configuration to control how to parse chat messages.

Methods:

Attributes:

Source code in vllm/renderers/params.py
@dataclass(frozen=True)
class ChatParams:
    """Configuration to control how to parse chat messages."""

    chat_template: str | None = None
    """The chat template to apply."""

    chat_template_content_format: "ChatTemplateContentFormatOption" = "auto"
    """The format of the chat template."""

    chat_template_kwargs: dict[str, Any] = field(default_factory=dict)
    """The kwargs to pass to the chat template."""

    media_io_kwargs: dict[str, dict[str, Any]] | None = None
    """Per-modality kwargs for media I/O (loading/decoding images, videos, etc.)."""

    mm_processor_kwargs: dict[str, Any] | None = None
    """The kwargs to pass to the multi-modal processor."""

    return_assistant_tokens_mask: bool = False
    """Request a per-token assistant mask from apply_chat_template."""

    tool_choice: Any | None = None
    """Request-level tool choice for renderers that need API metadata."""

    response_format: Any | None = None
    """Request-level response format for renderers that need API metadata."""

    def with_defaults(
        self,
        default_chat_template_kwargs: dict[str, Any] | None = None,
        default_media_io_kwargs: dict[str, dict[str, Any]] | None = None,
        default_mm_processor_kwargs: dict[str, Any] | None = None,
    ):
        if (
            not default_chat_template_kwargs
            and not default_media_io_kwargs
            and not default_mm_processor_kwargs
        ):
            return self

        return ChatParams(
            chat_template=self.chat_template,
            chat_template_content_format=self.chat_template_content_format,
            chat_template_kwargs=merge_kwargs(
                default_chat_template_kwargs,
                self.chat_template_kwargs,
            ),
            media_io_kwargs=merge_media_io_kwargs(
                default_media_io_kwargs,
                self.media_io_kwargs,
            ),
            mm_processor_kwargs=recursively_merge_kwargs(
                default_mm_processor_kwargs,
                self.mm_processor_kwargs,
            ),
            return_assistant_tokens_mask=self.return_assistant_tokens_mask,
            tool_choice=self.tool_choice,
            response_format=self.response_format,
        )

    def get_apply_chat_template_kwargs(self) -> dict[str, Any]:
        """The arguments to pass to `tokenizer.apply_chat_template`."""
        return merge_kwargs(
            self.chat_template_kwargs,
            dict(chat_template=self.chat_template, return_dict=False),
        )

chat_template = None class-attribute instance-attribute

The chat template to apply.

chat_template_content_format = 'auto' class-attribute instance-attribute

The format of the chat template.

chat_template_kwargs = field(default_factory=dict) class-attribute instance-attribute

The kwargs to pass to the chat template.

media_io_kwargs = None class-attribute instance-attribute

Per-modality kwargs for media I/O (loading/decoding images, videos, etc.).

mm_processor_kwargs = None class-attribute instance-attribute

The kwargs to pass to the multi-modal processor.

response_format = None class-attribute instance-attribute

Request-level response format for renderers that need API metadata.

return_assistant_tokens_mask = False class-attribute instance-attribute

Request a per-token assistant mask from apply_chat_template.

tool_choice = None class-attribute instance-attribute

Request-level tool choice for renderers that need API metadata.

get_apply_chat_template_kwargs()

The arguments to pass to tokenizer.apply_chat_template.

Source code in vllm/renderers/params.py
def get_apply_chat_template_kwargs(self) -> dict[str, Any]:
    """The arguments to pass to `tokenizer.apply_chat_template`."""
    return merge_kwargs(
        self.chat_template_kwargs,
        dict(chat_template=self.chat_template, return_dict=False),
    )

TokenizeParams dataclass

Configuration to control how prompts are tokenized.

Methods:

Attributes:

Source code in vllm/renderers/params.py
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
@dataclass(frozen=True)
class TokenizeParams:
    """Configuration to control how prompts are tokenized."""

    max_total_tokens: int | None
    """
    Maximum allowed number of input + output tokens.

    Usually, this refers to the model's context length.
    """

    max_output_tokens: int = 0
    """Maximum requested number of output tokens."""

    pad_prompt_tokens: int | None = None
    """
    Number of tokens to pad to:
    - `None` means no padding.
    - `-1` maps to `max_input_tokens`.
    """

    truncate_prompt_tokens: int | None = None
    """
    Number of tokens to keep:
    - `None` means no truncation.
    - `-1` maps to `max_input_tokens`.
    """

    truncation_side: Literal["left", "right"] | None = None
    """
    Which side to truncate from when ``truncate_prompt_tokens`` is active:
    - ``"right"`` keeps the first N tokens (truncate from the end).
    - ``"left"``  keeps the last  N tokens (truncate from the start).
    - ``None``    falls back to the tokenizer default.
    """

    do_lower_case: bool = False
    """Whether to normalize text to lower case before tokenization."""

    add_special_tokens: bool = True
    """Whether to add special tokens."""

    return_token_offsets: bool = False
    """If true, request char-level (start, end) offsets per token. Honored
    only for Fast (Rust-backed) tokenizers with text input and no multimodal
    data; otherwise silently ignored."""

    needs_detokenization: bool = False
    """
    Whether the tokenized prompt needs to contain the original text.

    Not to be confused with `SamplingParams.detokenize` which deals
    with the output generated by the model.
    """

    max_total_tokens_param: str = "max_total_tokens"
    """Override this to edit the message for validation errors."""

    max_output_tokens_param: str = "max_output_tokens"
    """Override this to edit the message for validation errors."""

    truncate_prompt_tokens_param: str = "truncate_prompt_tokens"
    """Override this to edit the message for validation errors."""

    @property
    def max_input_tokens(self) -> int | None:
        """Maximum allowed number of input tokens."""
        if self.max_total_tokens is None:
            return None

        return self.max_total_tokens - self.max_output_tokens

    def __post_init__(self) -> None:
        max_total_tokens = self.max_total_tokens
        max_output_tokens = self.max_output_tokens
        max_input_tokens = self.max_input_tokens
        truncate_prompt_tokens = self.truncate_prompt_tokens

        if self.truncation_side not in (None, "left", "right"):
            raise VLLMValidationError(
                "`truncation_side` must be either 'left' or 'right'.",
                parameter="truncation_side",
                value=self.truncation_side,
            )

        if (
            max_output_tokens is not None
            and max_total_tokens is not None
            and max_output_tokens > max_total_tokens
        ):
            raise VLLMValidationError(
                f"{self.max_output_tokens_param}={max_output_tokens} "
                f"cannot be greater than "
                f"{self.max_total_tokens_param}={max_total_tokens=}. "
                f"Please request fewer output tokens.",
                parameter=self.max_output_tokens_param,
                value=max_output_tokens,
            )

        if (
            max_input_tokens is not None
            and truncate_prompt_tokens is not None
            and truncate_prompt_tokens > max_input_tokens
        ):
            raise VLLMValidationError(
                f"{self.truncate_prompt_tokens_param}={truncate_prompt_tokens} "
                f"cannot be greater than {self.max_total_tokens_param} - "
                f"{self.max_output_tokens_param} = {max_input_tokens}. "
                f"Please request a smaller truncation size.",
                parameter=self.truncate_prompt_tokens_param,
                value=truncate_prompt_tokens,
            )

    def with_kwargs(self, **tokenization_kwargs: Any):
        max_length = tokenization_kwargs.pop("max_length", self.max_input_tokens)
        pad_prompt_tokens = tokenization_kwargs.pop(
            "pad_prompt_tokens", self.pad_prompt_tokens
        )
        truncate_prompt_tokens = tokenization_kwargs.pop(
            "truncate_prompt_tokens", self.truncate_prompt_tokens
        )
        truncation_side = tokenization_kwargs.pop(
            "truncation_side", self.truncation_side
        )
        do_lower_case = tokenization_kwargs.pop("do_lower_case", self.do_lower_case)
        add_special_tokens = tokenization_kwargs.pop(
            "add_special_tokens", self.add_special_tokens
        )
        needs_detokenization = tokenization_kwargs.pop(
            "needs_detokenization", self.needs_detokenization
        )

        # https://huggingface.co/docs/transformers/en/pad_truncation
        if padding := tokenization_kwargs.pop("padding", None):
            if padding == "max_length":
                pad_prompt_tokens = max_length
            elif padding in (False, "do_not_pad"):
                pad_prompt_tokens = None
            else:
                # To emit the below warning
                tokenization_kwargs["padding"] = padding

        if truncation := tokenization_kwargs.pop("truncation", None):
            if truncation in (True, "longest_first"):
                truncate_prompt_tokens = max_length
            elif truncation in (False, "do_not_truncate"):
                truncate_prompt_tokens = None
            else:
                # To emit the below warning
                tokenization_kwargs["truncation"] = truncation

        if tokenization_kwargs:
            logger.warning(
                "The following tokenization arguments are not supported "
                "by vLLM Renderer and will be ignored: %s",
                tokenization_kwargs,
            )

        max_total_tokens = self.max_total_tokens

        return TokenizeParams(
            max_total_tokens=max_total_tokens,
            max_output_tokens=(
                0
                if max_total_tokens is None or max_length is None
                else max_total_tokens - max_length
            ),
            pad_prompt_tokens=pad_prompt_tokens,
            truncate_prompt_tokens=truncate_prompt_tokens,
            truncation_side=truncation_side,
            do_lower_case=do_lower_case,
            add_special_tokens=add_special_tokens,
            needs_detokenization=needs_detokenization,
        )

    def get_encode_kwargs(self) -> dict[str, Any]:
        """The arguments to pass to `tokenizer.encode`."""
        max_length = self.truncate_prompt_tokens
        if max_length is not None and max_length < 0:
            max_length = self.max_input_tokens
        elif max_length is None and self.max_input_tokens is not None:
            # This prevents tokenization from taking up more resources than necessary
            # while still failing `self._token_len_check` as expected by users
            max_length = self.max_input_tokens + 1

        # Explicit truncation-side overrides require the full token sequence
        # so we can slice from the requested side in _token_truncation.
        # Disable tokenizer-level truncation because its default side may
        # differ from the requested side.  The defense against unbounded
        # tokenization lives in _text_len_check (character-level pre-trim).
        if self.truncation_side is not None and self.truncate_prompt_tokens is not None:
            return dict(
                truncation=False,
                add_special_tokens=self.add_special_tokens,
            )

        return dict(
            truncation=max_length is not None,
            max_length=max_length,
            add_special_tokens=self.add_special_tokens,
        )

    def _text_len_check(self, tokenizer: TokenizerLike | None, text: str) -> str:
        """Apply length checks to prompt text if necessary."""
        max_input_tokens = self.max_input_tokens
        if max_input_tokens is None or tokenizer is None:
            return text

        max_input_chars = max_input_tokens * tokenizer.max_chars_per_token

        if self.truncate_prompt_tokens is None:
            if len(text) > max_input_chars:
                raise VLLMValidationError(
                    f"This model's maximum context length is "
                    f"{self.max_total_tokens} tokens. However, you requested "
                    f"{self.max_output_tokens} output tokens and your prompt "
                    f"contains {len(text)} characters (more than "
                    f"{max_input_chars} characters, which is the upper bound "
                    f"for {max_input_tokens} input tokens). "
                    f"Please reduce the length of the input prompt or the "
                    f"number of requested output tokens.",
                    parameter="input_text",
                    value=len(text),
                )
        elif self.truncation_side is not None and len(text) > max_input_chars:
            if self.truncation_side == "left":
                text = text[-max_input_chars:]
            else:
                text = text[:max_input_chars]

        return text

    def _get_text_truncation_offset(
        self, tokenizer: TokenizerLike | None, text: str
    ) -> int:
        """Return the number of source characters removed from the left.

        ``_text_len_check`` pre-truncates long text before tokenization when
        an explicit truncation side is requested. Fast-tokenizer offsets are
        then relative to that shortened string, so callers need this prefix
        length to map them back to the original prompt.
        """
        max_input_tokens = self.max_input_tokens
        if (
            max_input_tokens is None
            or tokenizer is None
            or self.truncate_prompt_tokens is None
            or self.truncation_side != "left"
        ):
            return 0

        max_input_chars = max_input_tokens * tokenizer.max_chars_per_token
        if max_input_chars <= 0:
            return 0

        return max(len(text) - max_input_chars, 0)

    def _text_lowercase(self, tokenizer: TokenizerLike | None, text: str) -> str:
        """Apply lowercase to prompt text if necessary."""
        return text.lower() if self.do_lower_case else text

    def _validate_text(self, tokenizer: TokenizerLike | None, text: str) -> str:
        """Apply all validators to prompt text."""
        for validator in (
            self._text_len_check,
            self._text_lowercase,
        ):
            text = validator(tokenizer, text)

        return text

    def apply_pre_tokenization(
        self,
        tokenizer: TokenizerLike | None,
        prompt: TextPrompt,
    ) -> TextPrompt:
        """
        Ensure that the prompt meets the requirements set out by this config.
        If that is not possible, raise a `VLLMValidationError`.

        This method is run before tokenization occurs.
        """
        prompt["prompt"] = self._validate_text(tokenizer, prompt["prompt"])

        return prompt

    def _token_padding(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
        """Apply padding to prompt tokens if necessary."""
        pad_length = self.pad_prompt_tokens
        if pad_length is not None and pad_length < 0:
            pad_length = self.max_input_tokens

        if pad_length is None or pad_length <= len(tokens):
            return tokens

        if tokenizer is None:
            raise VLLMValidationError(
                "Cannot pad tokens when `skip_tokenizer_init=True`",
                parameter="pad_prompt_tokens",
            )
        if not isinstance(tokens, list):
            raise VLLMValidationError(
                "Cannot pad tokens for embedding inputs",
                parameter="pad_prompt_tokens",
            )

        return tokens + [tokenizer.pad_token_id] * (pad_length - len(tokens))

    def _truncation_slice(
        self, tokenizer: TokenizerLike | None, length: int
    ) -> slice | None:
        """The slice truncation applies to a sequence of `length` tokens.

        `None` means no truncation. Anything parallel to the prompt tokens
        must be reduced with this same slice to stay aligned with them; list
        such keys in `_PARALLEL_TO_PROMPT_TOKENS`.
        """
        max_length = self.truncate_prompt_tokens
        if max_length is not None and max_length < 0:
            max_length = self.max_input_tokens

        if max_length is None or max_length >= length:
            return None
        if max_length == 0:
            return slice(0, 0)

        side = self.truncation_side or (
            tokenizer.truncation_side if tokenizer is not None else None
        )
        if side == "left":
            return slice(-max_length, None)

        return slice(0, max_length)

    def _token_truncation(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
        """Apply truncation to prompt tokens if necessary."""
        truncation = self._truncation_slice(tokenizer, len(tokens))
        if truncation is None:
            return tokens

        return tokens[truncation]

    def _token_len_check(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
        """Apply length checks to prompt tokens if necessary."""
        max_input_tokens = self.max_input_tokens
        if max_input_tokens is None:
            return tokens

        if len(tokens) > max_input_tokens:
            token_count = len(tokens)
            # The tokenizer may have truncated the prompt to
            # max_input_tokens + 1 (see get_encode_kwargs), so the
            # actual prompt length could be larger.
            qualifier = "at least " if token_count == max_input_tokens + 1 else ""
            total = token_count + self.max_output_tokens
            raise VLLMValidationError(
                f"This model's maximum context length is "
                f"{self.max_total_tokens} tokens. However, you requested "
                f"{self.max_output_tokens} output tokens and your prompt "
                f"contains {qualifier}{token_count} input tokens, "
                f"for a total of {qualifier}{total} tokens. "
                f"Please reduce the length of the input prompt or the "
                f"number of requested output tokens.",
                parameter="input_tokens",
                value=token_count,
            )

        return tokens

    def _validate_tokens(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
        """Apply all validators to a token sequence."""
        # Truncation runs before padding, matching the Transformers pipeline
        # these parameters are named after. Padding first would let a
        # subsequent left-side truncation keep only the pad tokens it just
        # appended, discarding the prompt entirely.
        for validator in (
            self._token_truncation,
            self._token_padding,
            self._token_len_check,
        ):
            tokens = validator(tokenizer, tokens)

        return tokens

    def apply_post_tokenization(
        self,
        tokenizer: TokenizerLike | None,
        prompt: TokensPrompt | EmbedsPrompt,
    ) -> TokensPrompt | EmbedsPrompt:
        """
        Ensure that the prompt meets the requirements set out by this config.
        If that is not possible, raise a `VLLMValidationError`.

        This method is run after tokenization occurs.
        """
        if "prompt_token_ids" in prompt:
            prompt["prompt_token_ids"] = self._validate_tokens(  # type: ignore[typeddict-unknown-key]
                tokenizer,
                prompt["prompt_token_ids"],  # type: ignore[typeddict-item]
            )
        if "prompt_embeds" in prompt:
            prompt["prompt_embeds"] = self._validate_tokens(  # type: ignore[typeddict-unknown-key]
                tokenizer,
                prompt["prompt_embeds"],  # type: ignore[typeddict-item]
            )
        prompt_dict = cast(dict, prompt)
        for key in _PARALLEL_TO_PROMPT_TOKENS:
            parallel = prompt_dict.get(key)
            if parallel is None:
                continue
            truncation = self._truncation_slice(tokenizer, len(parallel))
            if truncation is not None:
                prompt_dict[key] = parallel[truncation]

        return prompt

add_special_tokens = True class-attribute instance-attribute

Whether to add special tokens.

do_lower_case = False class-attribute instance-attribute

Whether to normalize text to lower case before tokenization.

max_input_tokens property

Maximum allowed number of input tokens.

max_output_tokens = 0 class-attribute instance-attribute

Maximum requested number of output tokens.

max_output_tokens_param = 'max_output_tokens' class-attribute instance-attribute

Override this to edit the message for validation errors.

max_total_tokens instance-attribute

Maximum allowed number of input + output tokens.

Usually, this refers to the model's context length.

max_total_tokens_param = 'max_total_tokens' class-attribute instance-attribute

Override this to edit the message for validation errors.

needs_detokenization = False class-attribute instance-attribute

Whether the tokenized prompt needs to contain the original text.

Not to be confused with SamplingParams.detokenize which deals with the output generated by the model.

pad_prompt_tokens = None class-attribute instance-attribute

Number of tokens to pad to: - None means no padding. - -1 maps to max_input_tokens.

return_token_offsets = False class-attribute instance-attribute

If true, request char-level (start, end) offsets per token. Honored only for Fast (Rust-backed) tokenizers with text input and no multimodal data; otherwise silently ignored.

truncate_prompt_tokens = None class-attribute instance-attribute

Number of tokens to keep: - None means no truncation. - -1 maps to max_input_tokens.

truncate_prompt_tokens_param = 'truncate_prompt_tokens' class-attribute instance-attribute

Override this to edit the message for validation errors.

truncation_side = None class-attribute instance-attribute

Which side to truncate from when truncate_prompt_tokens is active: - "right" keeps the first N tokens (truncate from the end). - "left" keeps the last N tokens (truncate from the start). - None falls back to the tokenizer default.

_get_text_truncation_offset(tokenizer, text)

Return the number of source characters removed from the left.

_text_len_check pre-truncates long text before tokenization when an explicit truncation side is requested. Fast-tokenizer offsets are then relative to that shortened string, so callers need this prefix length to map them back to the original prompt.

Source code in vllm/renderers/params.py
def _get_text_truncation_offset(
    self, tokenizer: TokenizerLike | None, text: str
) -> int:
    """Return the number of source characters removed from the left.

    ``_text_len_check`` pre-truncates long text before tokenization when
    an explicit truncation side is requested. Fast-tokenizer offsets are
    then relative to that shortened string, so callers need this prefix
    length to map them back to the original prompt.
    """
    max_input_tokens = self.max_input_tokens
    if (
        max_input_tokens is None
        or tokenizer is None
        or self.truncate_prompt_tokens is None
        or self.truncation_side != "left"
    ):
        return 0

    max_input_chars = max_input_tokens * tokenizer.max_chars_per_token
    if max_input_chars <= 0:
        return 0

    return max(len(text) - max_input_chars, 0)

_text_len_check(tokenizer, text)

Apply length checks to prompt text if necessary.

Source code in vllm/renderers/params.py
def _text_len_check(self, tokenizer: TokenizerLike | None, text: str) -> str:
    """Apply length checks to prompt text if necessary."""
    max_input_tokens = self.max_input_tokens
    if max_input_tokens is None or tokenizer is None:
        return text

    max_input_chars = max_input_tokens * tokenizer.max_chars_per_token

    if self.truncate_prompt_tokens is None:
        if len(text) > max_input_chars:
            raise VLLMValidationError(
                f"This model's maximum context length is "
                f"{self.max_total_tokens} tokens. However, you requested "
                f"{self.max_output_tokens} output tokens and your prompt "
                f"contains {len(text)} characters (more than "
                f"{max_input_chars} characters, which is the upper bound "
                f"for {max_input_tokens} input tokens). "
                f"Please reduce the length of the input prompt or the "
                f"number of requested output tokens.",
                parameter="input_text",
                value=len(text),
            )
    elif self.truncation_side is not None and len(text) > max_input_chars:
        if self.truncation_side == "left":
            text = text[-max_input_chars:]
        else:
            text = text[:max_input_chars]

    return text

_text_lowercase(tokenizer, text)

Apply lowercase to prompt text if necessary.

Source code in vllm/renderers/params.py
def _text_lowercase(self, tokenizer: TokenizerLike | None, text: str) -> str:
    """Apply lowercase to prompt text if necessary."""
    return text.lower() if self.do_lower_case else text

_token_len_check(tokenizer, tokens)

Apply length checks to prompt tokens if necessary.

Source code in vllm/renderers/params.py
def _token_len_check(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
    """Apply length checks to prompt tokens if necessary."""
    max_input_tokens = self.max_input_tokens
    if max_input_tokens is None:
        return tokens

    if len(tokens) > max_input_tokens:
        token_count = len(tokens)
        # The tokenizer may have truncated the prompt to
        # max_input_tokens + 1 (see get_encode_kwargs), so the
        # actual prompt length could be larger.
        qualifier = "at least " if token_count == max_input_tokens + 1 else ""
        total = token_count + self.max_output_tokens
        raise VLLMValidationError(
            f"This model's maximum context length is "
            f"{self.max_total_tokens} tokens. However, you requested "
            f"{self.max_output_tokens} output tokens and your prompt "
            f"contains {qualifier}{token_count} input tokens, "
            f"for a total of {qualifier}{total} tokens. "
            f"Please reduce the length of the input prompt or the "
            f"number of requested output tokens.",
            parameter="input_tokens",
            value=token_count,
        )

    return tokens

_token_padding(tokenizer, tokens)

Apply padding to prompt tokens if necessary.

Source code in vllm/renderers/params.py
def _token_padding(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
    """Apply padding to prompt tokens if necessary."""
    pad_length = self.pad_prompt_tokens
    if pad_length is not None and pad_length < 0:
        pad_length = self.max_input_tokens

    if pad_length is None or pad_length <= len(tokens):
        return tokens

    if tokenizer is None:
        raise VLLMValidationError(
            "Cannot pad tokens when `skip_tokenizer_init=True`",
            parameter="pad_prompt_tokens",
        )
    if not isinstance(tokens, list):
        raise VLLMValidationError(
            "Cannot pad tokens for embedding inputs",
            parameter="pad_prompt_tokens",
        )

    return tokens + [tokenizer.pad_token_id] * (pad_length - len(tokens))

_token_truncation(tokenizer, tokens)

Apply truncation to prompt tokens if necessary.

Source code in vllm/renderers/params.py
def _token_truncation(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
    """Apply truncation to prompt tokens if necessary."""
    truncation = self._truncation_slice(tokenizer, len(tokens))
    if truncation is None:
        return tokens

    return tokens[truncation]

_truncation_slice(tokenizer, length)

The slice truncation applies to a sequence of length tokens.

None means no truncation. Anything parallel to the prompt tokens must be reduced with this same slice to stay aligned with them; list such keys in _PARALLEL_TO_PROMPT_TOKENS.

Source code in vllm/renderers/params.py
def _truncation_slice(
    self, tokenizer: TokenizerLike | None, length: int
) -> slice | None:
    """The slice truncation applies to a sequence of `length` tokens.

    `None` means no truncation. Anything parallel to the prompt tokens
    must be reduced with this same slice to stay aligned with them; list
    such keys in `_PARALLEL_TO_PROMPT_TOKENS`.
    """
    max_length = self.truncate_prompt_tokens
    if max_length is not None and max_length < 0:
        max_length = self.max_input_tokens

    if max_length is None or max_length >= length:
        return None
    if max_length == 0:
        return slice(0, 0)

    side = self.truncation_side or (
        tokenizer.truncation_side if tokenizer is not None else None
    )
    if side == "left":
        return slice(-max_length, None)

    return slice(0, max_length)

_validate_text(tokenizer, text)

Apply all validators to prompt text.

Source code in vllm/renderers/params.py
def _validate_text(self, tokenizer: TokenizerLike | None, text: str) -> str:
    """Apply all validators to prompt text."""
    for validator in (
        self._text_len_check,
        self._text_lowercase,
    ):
        text = validator(tokenizer, text)

    return text

_validate_tokens(tokenizer, tokens)

Apply all validators to a token sequence.

Source code in vllm/renderers/params.py
def _validate_tokens(self, tokenizer: TokenizerLike | None, tokens: _S) -> _S:
    """Apply all validators to a token sequence."""
    # Truncation runs before padding, matching the Transformers pipeline
    # these parameters are named after. Padding first would let a
    # subsequent left-side truncation keep only the pad tokens it just
    # appended, discarding the prompt entirely.
    for validator in (
        self._token_truncation,
        self._token_padding,
        self._token_len_check,
    ):
        tokens = validator(tokenizer, tokens)

    return tokens

apply_post_tokenization(tokenizer, prompt)

Ensure that the prompt meets the requirements set out by this config. If that is not possible, raise a VLLMValidationError.

This method is run after tokenization occurs.

Source code in vllm/renderers/params.py
def apply_post_tokenization(
    self,
    tokenizer: TokenizerLike | None,
    prompt: TokensPrompt | EmbedsPrompt,
) -> TokensPrompt | EmbedsPrompt:
    """
    Ensure that the prompt meets the requirements set out by this config.
    If that is not possible, raise a `VLLMValidationError`.

    This method is run after tokenization occurs.
    """
    if "prompt_token_ids" in prompt:
        prompt["prompt_token_ids"] = self._validate_tokens(  # type: ignore[typeddict-unknown-key]
            tokenizer,
            prompt["prompt_token_ids"],  # type: ignore[typeddict-item]
        )
    if "prompt_embeds" in prompt:
        prompt["prompt_embeds"] = self._validate_tokens(  # type: ignore[typeddict-unknown-key]
            tokenizer,
            prompt["prompt_embeds"],  # type: ignore[typeddict-item]
        )
    prompt_dict = cast(dict, prompt)
    for key in _PARALLEL_TO_PROMPT_TOKENS:
        parallel = prompt_dict.get(key)
        if parallel is None:
            continue
        truncation = self._truncation_slice(tokenizer, len(parallel))
        if truncation is not None:
            prompt_dict[key] = parallel[truncation]

    return prompt

apply_pre_tokenization(tokenizer, prompt)

Ensure that the prompt meets the requirements set out by this config. If that is not possible, raise a VLLMValidationError.

This method is run before tokenization occurs.

Source code in vllm/renderers/params.py
def apply_pre_tokenization(
    self,
    tokenizer: TokenizerLike | None,
    prompt: TextPrompt,
) -> TextPrompt:
    """
    Ensure that the prompt meets the requirements set out by this config.
    If that is not possible, raise a `VLLMValidationError`.

    This method is run before tokenization occurs.
    """
    prompt["prompt"] = self._validate_text(tokenizer, prompt["prompt"])

    return prompt

get_encode_kwargs()

The arguments to pass to tokenizer.encode.

Source code in vllm/renderers/params.py
def get_encode_kwargs(self) -> dict[str, Any]:
    """The arguments to pass to `tokenizer.encode`."""
    max_length = self.truncate_prompt_tokens
    if max_length is not None and max_length < 0:
        max_length = self.max_input_tokens
    elif max_length is None and self.max_input_tokens is not None:
        # This prevents tokenization from taking up more resources than necessary
        # while still failing `self._token_len_check` as expected by users
        max_length = self.max_input_tokens + 1

    # Explicit truncation-side overrides require the full token sequence
    # so we can slice from the requested side in _token_truncation.
    # Disable tokenizer-level truncation because its default side may
    # differ from the requested side.  The defense against unbounded
    # tokenization lives in _text_len_check (character-level pre-trim).
    if self.truncation_side is not None and self.truncate_prompt_tokens is not None:
        return dict(
            truncation=False,
            add_special_tokens=self.add_special_tokens,
        )

    return dict(
        truncation=max_length is not None,
        max_length=max_length,
        add_special_tokens=self.add_special_tokens,
    )