Skip to content

vllm.tokenizers.detokenizer_utils

Functions:

_restore_leading_spaces(raw_token, token_str, marker)

Restore leading spaces that decode() stripped from a raw vocab piece.

convert_ids_list_to_tokens(tokenizer, token_ids)

Detokenize the input ids individually.

Uses decode() for human-readable output, then checks the raw vocab piece via convert_ids_to_tokens() to restore any leading spaces that decode() stripped (SentencePiece add_dummy_prefix inverse).

Parameters:

  • tokenizer

    (TokenizerLike) –

    tokenizer used by model under test

  • token_ids

    (list[int]) –

    convert these tokens (Python list form)

Returns:

  • list[str] –

    Python list of token string representations

convert_prompt_ids_to_tokens(tokenizer, prompt_ids, skip_special_tokens=False)

Converts the prompt ids to tokens and returns the tokens and offsets for incremental detokenization.

Note that not all tokens are converted to strings. Only the tokens that are necessary for incremental detokenization are converted to strings.

detokenize_incrementally(tokenizer, all_input_ids, prev_tokens, prefix_offset, read_offset, skip_special_tokens=False, spaces_between_special_tokens=True)

Detokenizes the input ids incrementally and returns the new tokens and the new text.

If prev_tokens is None, this function will convert the input ids to tokens and return the tokens and the new text. Otherwise, it will return the new tokens and the new text.

This function will also return the new prefix offset and the new read offset to be used in the next iteration.

The offsets are necessary to defeat cleanup algorithms in the decode which decide to add a space or not depending on the surrounding ids.

Parameters:

  • tokenizer

    (TokenizerLike) –

    The tokenizer to use.

  • all_input_ids

    (list[int]) –

    The input ids. The last id is the new token id.

  • prev_tokens

    (list[str] | None) –

    The previous tokens. If None, this function will convert the input ids to tokens and return the tokens and the new text.

  • prefix_offset

    (int) –

    The prefix offset.

  • read_offset

    (int) –

    The read offset.

  • skip_special_tokens

    (bool, default: False ) –

    Whether to skip special tokens.

  • spaces_between_special_tokens

    (bool, default: True ) –

    Whether to add spaces between special tokens.

get_leading_space_marker(tokenizer)

Read the space marker from the tokenizer's pre_tokenizer config.

Only Metaspace pre_tokenizers (used by SentencePiece-based models like Llama, Mistral, T5) have a replacement character whose leading instance gets stripped by decode(). ByteLevel (GPT-2), BertPreTokenizer (BERT), and others do not have this issue.

Returns the marker character, or None if decode() is safe for single tokens.