Skip to content

vllm.renderers.inputs.preprocess

Schemas and utilites for preprocessing inputs.

DecoderDictPrompt module-attribute

DecoderDictPrompt: TypeAlias = TextPrompt | TokensPrompt

A DecoderPrompt that has been standardized into a dictionary.

DecoderOnlyDictPrompt module-attribute

DecoderOnlyDictPrompt: TypeAlias = (
    TextPrompt | TokensPrompt | EmbedsPrompt
)

A DecoderOnlyPrompt that has been standardized into a dictionary.

DictPrompt module-attribute

A PromptType that has been standardized into a dictionary.

EncoderDictPrompt module-attribute

EncoderDictPrompt: TypeAlias = TextPrompt | TokensPrompt

A EncoderPrompt that has been standardized into a dictionary.

SingletonDictPrompt module-attribute

A SingletonPrompt that has been standardized into a dictionary.

EncoderDecoderDictPrompt

Bases: TypedDict

A EncoderDecoderPrompt that has been standardized into a dictionary.

Source code in vllm/renderers/inputs/preprocess.py
class EncoderDecoderDictPrompt(TypedDict):
    """
    A [`EncoderDecoderPrompt`][vllm.inputs.data.EncoderDecoderPrompt]
    that has been standardized into a dictionary.
    """

    encoder_prompt: EncoderDictPrompt

    decoder_prompt: DecoderDictPrompt | None

decoder_prompt instance-attribute

decoder_prompt: DecoderDictPrompt | None

encoder_prompt instance-attribute

encoder_prompt: EncoderDictPrompt

PromptComponents

Bases: NamedTuple

Source code in vllm/renderers/inputs/preprocess.py
class PromptComponents(NamedTuple):
    text: str | None = None
    token_ids: list[int] | None = None
    embeds: "torch.Tensor | None" = None

embeds class-attribute instance-attribute

embeds: Tensor | None = None

text class-attribute instance-attribute

text: str | None = None

token_ids class-attribute instance-attribute

token_ids: list[int] | None = None

_parse_dec_prompt

_parse_dec_prompt(prompt: object) -> DecoderDictPrompt
Source code in vllm/renderers/inputs/preprocess.py
def _parse_dec_prompt(prompt: object) -> DecoderDictPrompt:
    if isinstance(prompt, str):
        return TextPrompt(prompt=prompt)

    if isinstance(prompt, list):
        if not is_list_of(prompt, int):
            raise TypeError("Token prompt should be a list of integers")

        return TokensPrompt(prompt_token_ids=prompt)

    if isinstance(prompt, dict):
        if "prompt_embeds" in prompt:
            raise TypeError("Cannot pass embeddings prompt to encoder-decoder models")

        if (
            "multi_modal_data" in prompt
            or "mm_processor_kwargs" in prompt
            or "multi_modal_uuids" in prompt
        ):
            raise TypeError("Cannot pass multi-modal inputs to decoder prompt")

        if "prompt" in prompt or "prompt_token_ids" in prompt:
            return prompt  # type: ignore[return-value]

        raise TypeError("Prompt dictionary must contain text or tokens")

    raise TypeError("Prompt should be a string, list of tokens, or dictionary")

_parse_enc_prompt

_parse_enc_prompt(prompt: object) -> EncoderDictPrompt
Source code in vllm/renderers/inputs/preprocess.py
def _parse_enc_prompt(prompt: object) -> EncoderDictPrompt:
    if isinstance(prompt, str):
        return TextPrompt(prompt=prompt)

    if isinstance(prompt, list):
        if not is_list_of(prompt, int):
            raise TypeError("Token prompt should be a list of integers")

        return TokensPrompt(prompt_token_ids=prompt)

    if isinstance(prompt, dict):
        if "prompt_embeds" in prompt:
            raise TypeError("Cannot pass embeddings prompt to encoder-decoder models")

        if "prompt" in prompt or "prompt_token_ids" in prompt:
            return prompt  # type: ignore[return-value]

        raise TypeError("Prompt dictionary must contain text or tokens")

    raise TypeError("Prompt should be a string, list of tokens, or dictionary")

conversation_to_seq

conversation_to_seq(
    conversation_or_conversations: list[
        ChatCompletionMessageParam
    ]
    | Sequence[list[ChatCompletionMessageParam]],
) -> Sequence[list[ChatCompletionMessageParam]]
Source code in vllm/renderers/inputs/preprocess.py
def conversation_to_seq(
    conversation_or_conversations: list["ChatCompletionMessageParam"]
    | Sequence[list["ChatCompletionMessageParam"]],
) -> Sequence[list["ChatCompletionMessageParam"]]:
    if len(conversation_or_conversations) > 0 and is_list_of(
        conversation_or_conversations, dict
    ):
        return [conversation_or_conversations]  # type: ignore[list-item]

    return conversation_or_conversations  # type: ignore[return-value]

extract_prompt_components

extract_prompt_components(
    model_config: ModelConfig, prompt: object
) -> PromptComponents
Source code in vllm/renderers/inputs/preprocess.py
def extract_prompt_components(
    model_config: "ModelConfig",
    prompt: object,
) -> PromptComponents:
    target_prompt = extract_target_prompt(model_config, prompt)

    return PromptComponents(
        text=target_prompt.get("prompt"),
        token_ids=target_prompt.get("prompt_token_ids"),  # type: ignore[arg-type]
        embeds=target_prompt.get("prompt_embeds"),
    )

extract_prompt_len

extract_prompt_len(
    model_config: ModelConfig, prompt: object
)
Source code in vllm/renderers/inputs/preprocess.py
def extract_prompt_len(model_config: "ModelConfig", prompt: object):
    target_prompt = extract_target_prompt(model_config, prompt)

    return length_from_prompt_token_ids_or_embeds(
        target_prompt.get("prompt_token_ids"),  # type: ignore[arg-type]
        target_prompt.get("prompt_embeds"),
    )

extract_target_prompt

extract_target_prompt(
    model_config: ModelConfig, prompt: object
)
Source code in vllm/renderers/inputs/preprocess.py
def extract_target_prompt(model_config: "ModelConfig", prompt: object):
    return (
        parse_enc_dec_prompt(prompt)["encoder_prompt"]
        if model_config.is_encoder_decoder
        else parse_dec_only_prompt(prompt)
    )

parse_dec_only_prompt

parse_dec_only_prompt(
    prompt: object,
) -> DecoderOnlyDictPrompt

Parse a prompt for a decoder-only model and normalize it to a dictionary.

Source code in vllm/renderers/inputs/preprocess.py
def parse_dec_only_prompt(prompt: object) -> DecoderOnlyDictPrompt:
    """
    Parse a prompt for a decoder-only model and normalize it to a dictionary.
    """
    if isinstance(prompt, str):
        return TextPrompt(prompt=prompt)

    if isinstance(prompt, list):
        if not is_list_of(prompt, int):
            raise TypeError("Token prompt should be a list of integers")

        return TokensPrompt(prompt_token_ids=prompt)

    if isinstance(prompt, dict):
        if "encoder_prompt" in prompt:
            raise TypeError("Cannot pass encoder-decoder prompt to decoder-only models")

        if (
            "prompt" in prompt
            or "prompt_token_ids" in prompt
            or "prompt_embeds" in prompt
        ):
            return prompt  # type: ignore[return-value]

        raise TypeError("Prompt dictionary must contain text, tokens, or embeddings")

    raise TypeError("Prompt should be a string, list of tokens, or dictionary")

parse_enc_dec_prompt

parse_enc_dec_prompt(
    prompt: object,
) -> EncoderDecoderDictPrompt

Parse a prompt for an encoder-decoder model and normalize it to a dictionary.

Source code in vllm/renderers/inputs/preprocess.py
def parse_enc_dec_prompt(prompt: object) -> EncoderDecoderDictPrompt:
    """
    Parse a prompt for an encoder-decoder model and normalize it to a dictionary.
    """
    if isinstance(prompt, dict) and "encoder_prompt" in prompt:
        enc_prompt: object = prompt["encoder_prompt"]  # type: ignore[typeddict-item]
        dec_prompt: object | None = prompt["decoder_prompt"]  # type: ignore[typeddict-item]
    else:
        enc_prompt = prompt
        dec_prompt = None

    return EncoderDecoderDictPrompt(
        encoder_prompt=_parse_enc_prompt(enc_prompt),
        decoder_prompt=None if dec_prompt is None else _parse_dec_prompt(dec_prompt),
    )

parse_model_prompt

parse_model_prompt(
    model_config: ModelConfig, prompt: object
)
Source code in vllm/renderers/inputs/preprocess.py
def parse_model_prompt(model_config: "ModelConfig", prompt: object):
    if model_config.is_encoder_decoder:
        return parse_enc_dec_prompt(prompt)

    return parse_dec_only_prompt(prompt)

prompt_to_seq

prompt_to_seq(
    prompt_or_prompts: SingletonPrompt
    | bytes
    | Sequence[SingletonPrompt | bytes],
) -> Sequence[SingletonPrompt]
prompt_to_seq(
    prompt_or_prompts: PromptType | Sequence[PromptType],
) -> Sequence[PromptType]
prompt_to_seq(
    prompt_or_prompts: PromptType
    | bytes
    | Sequence[PromptType | bytes],
) -> Sequence[PromptType]
Source code in vllm/renderers/inputs/preprocess.py
def prompt_to_seq(
    prompt_or_prompts: PromptType | bytes | Sequence[PromptType | bytes],
) -> Sequence[PromptType]:
    if isinstance(prompt_or_prompts, (dict, str, bytes)) or (
        len(prompt_or_prompts) > 0 and is_list_of(prompt_or_prompts, int)
    ):
        return [prompt_or_prompts]  # type: ignore[list-item]

    return prompt_or_prompts  # type: ignore[return-value]