Skip to content

vllm.sampling_params

Sampling parameters for text generation.

Classes:

Functions:

Attributes:

MAX_LOGPROB_TOKEN_IDS = 256 module-attribute

Upper bound on SamplingParams.logprob_token_ids list length. Must match the per-request row width allocated by the sampler's LogprobTokenIdsState.

MAX_STRUCTURED_OUTPUT_JSON_NESTING = 64 module-attribute

Upper bound on nested objects and arrays in a structured-output JSON schema or structural tag. Grammar conversion runs on the API server and its cost grows steeply with nesting. OpenAI allows 10 levels of schema nesting.

STRUCTURAL_TAG_WRAPPER_NESTING = 16 module-attribute

Extra nesting allowed for a structural tag, which wraps its JSON schemas in tag structure (up to 15 levels for tool-call tags generated by tool parsers).

BeamSearchParams

Bases: Struct

Beam search parameters for text generation.

Attributes:

  • length_penalty (float) –

    Exponent on the number of generated tokens (including a final EOS) that

length_penalty = 1.0 class-attribute instance-attribute

Exponent on the number of generated tokens (including a final EOS) that normalizes a beam's cumulative log-probability when ranking beams, as in Transformers. Values above 0 favor longer beams, below 0 shorter ones.

RepetitionDetectionParams

Parameters for detecting repetitive N-gram patterns in output tokens.

Attributes:

  • max_pattern_size (int) –

    Maximum size of N-gram pattern to detect for sequence repetition.

  • min_count (int) –

    Minimum number of times an N-gram pattern must repeat to trigger

  • min_pattern_size (int) –

    Minimum N-gram pattern size to check for sequence repetition.

max_pattern_size = 0 class-attribute instance-attribute

Maximum size of N-gram pattern to detect for sequence repetition. Set to 0 to disable. Must be used together with min_count.

min_count = 0 class-attribute instance-attribute

Minimum number of times an N-gram pattern must repeat to trigger detection. Must be >= 2. Example: 3 for detecting a phrase repeated 3 times. Must be used together with max_pattern_size.

min_pattern_size = 0 class-attribute instance-attribute

Minimum N-gram pattern size to check for sequence repetition. If set to 0, it defaults to 1. Must be <= max_pattern_size.

SamplingParams

Bases: PydanticMsgspecMixin, Struct

Sampling parameters for text generation.

Overall, we follow the sampling parameters from the OpenAI text completion API (https://platform.openai.com/docs/api-reference/completions/create). In addition, we support beam search, which is not supported by OpenAI.

Methods:

Attributes:

allowed_token_ids = None class-attribute instance-attribute

If provided, the engine will construct a logits processor which only retains scores for the given token ids.

bad_words = None class-attribute instance-attribute

Words that are not allowed to be generated. More precisely, only the last token of a corresponding token sequence is not allowed when the next generated token can complete the sequence.

detokenize = True class-attribute instance-attribute

Whether to detokenize the output.

extra_args = None class-attribute instance-attribute

Arbitrary additional args, that can be used by custom sampling implementations, plugins, etc. Not used by any in-tree sampling implementations.

flat_logprobs = False class-attribute instance-attribute

Whether to return logprobs in flatten format (i.e. FlatLogprob) for better performance. NOTE: GC costs of FlatLogprobs is significantly smaller than list[dict[int, Logprob]]. After enabled, PromptLogprobs and SampleLogprobs would populated as FlatLogprobs.

frequency_penalty = 0.0 class-attribute instance-attribute

Penalizes new tokens based on their frequency in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens.

ignore_eos = False class-attribute instance-attribute

Whether to ignore the EOS token and continue generating tokens after the EOS token is generated.

include_stop_str_in_output = False class-attribute instance-attribute

Whether to include the stop strings in output text.

logit_bias = None class-attribute instance-attribute

If provided, the engine will construct a logits processor that applies these logit biases.

logprob_token_ids = None class-attribute instance-attribute

Specific token IDs to return logprobs for. More efficient than logprobs=-1 when you only need logprobs for a small set of tokens. When set, logprobs for exactly these token IDs will be returned, in addition to the sampled token. This is useful for scoring tasks where you want to compare probabilities of specific label tokens.

logprobs = None class-attribute instance-attribute

Number of log probabilities to return per output token. When set to None, no probability is returned. If set to a non-None value, the result includes the log probabilities of the specified number of most likely tokens, as well as the chosen tokens. Note that the implementation follows the OpenAI API: The API will always return the log probability of the sampled token, so there may be up to logprobs+1 elements in the response. When set to -1, return all vocab_size log probabilities.

max_tokens = 16 class-attribute instance-attribute

Maximum number of tokens to generate per output sequence.

min_p = 0.0 class-attribute instance-attribute

Represents the minimum probability for a token to be considered, relative to the probability of the most likely token. Must be in [0, 1]. Set to 0 to disable this.

min_tokens = 0 class-attribute instance-attribute

Minimum number of tokens to generate per output sequence before EOS or stop_token_ids can be generated

n = 1 class-attribute instance-attribute

Number of outputs to return for the given prompt request.

The maximum allowed value is controlled by the VLLM_MAX_N_SEQUENCES environment variable (default: 16384).

NOTE

AsyncLLM streams outputs by default. When n > 1, all n outputs are generated and streamed cumulatively per request. To see all n outputs upon completion, use output_kind=RequestOutputKind.FINAL_ONLY in SamplingParams.

num_logprobs property

Number of sample logprobs to return per output token, or None if no sample logprobs were requested. Takes logprob_token_ids into account: when logprobs is unset but logprob_token_ids is set, returns len(logprob_token_ids).

presence_penalty = 0.0 class-attribute instance-attribute

Penalizes new tokens based on whether they appear in the generated text so far. Values > 0 encourage the model to use new tokens, while values < 0 encourage the model to repeat tokens.

prompt_logprob_start = None class-attribute instance-attribute

First causal prompt row to score with prompt_logprobs and prompt_logprob_token_ids; defaults to the first row. Earlier prompt positions get None prompt logprobs. Set skip_reading_prefix_cache=False to reuse local cached rows before it.

prompt_logprob_token_ids = None class-attribute instance-attribute

Token IDs to score per causal prompt row: an integer array of shape [num_rows, num_ids] or nested lists; -1 entries (padding) score -inf. Row i scores its IDs as predictions of prompt token prompt_logprob_start + i + 1, so the last prompt row is excluded. See docs/training/rollout_artifacts/prompt_token_id_logprobs.md.

prompt_logprobs = None class-attribute instance-attribute

Number of log probabilities to return per prompt token. When set to -1, return all vocab_size log probabilities.

repetition_detection = None class-attribute instance-attribute

Parameters for detecting repetitive N-gram patterns in output tokens. If such repetition is detected, generation will be ended early. LLMs can sometimes generate repetitive, unhelpful token patterns, stopping only when they hit the maximum output length (e.g. 'abcdabcdabcd...' or '\emoji \emoji \emoji ...'). This feature can detect such behavior and terminate early, saving time and tokens.

repetition_penalty = 1.0 class-attribute instance-attribute

Penalizes new tokens based on whether they appear in the prompt and the generated text so far. Values > 1 encourage the model to use new tokens, while values < 1 encourage the model to repeat tokens.

routed_experts_prompt_start = 0 class-attribute instance-attribute

When enable_return_routed_experts is active, skip the first routed_experts_prompt_start prompt tokens from the returned routing data. In multi-turn agent scenarios, set this to the length of the already-returned prefix to avoid duplicating routing for prompt tokens covered by earlier turns. Default 0 returns routing for all prompt tokens.

seed = None class-attribute instance-attribute

Random seed to use for the generation.

skip_clone = False class-attribute instance-attribute

Internal flag indicating that this SamplingParams instance is safe to reuse without cloning. When True, clone() will return self without performing a deep copy. This should only be set when the params object is guaranteed to be dedicated to a single request and won't be modified in ways that would affect other uses.

skip_special_tokens = True class-attribute instance-attribute

Whether to skip special tokens in the output.

spaces_between_special_tokens = True class-attribute instance-attribute

Whether to add spaces between special tokens in the output.

stop = None class-attribute instance-attribute

String(s) that stop the generation when they are generated. The returned output will not contain the stop strings.

stop_token_ids = None class-attribute instance-attribute

Token IDs that stop the generation when they are generated. The returned output will contain the stop tokens unless the stop tokens are special tokens.

stream_interval = None class-attribute instance-attribute

Number of newly generated tokens to batch into each streamed RequestOutput. Raises the interval above the engine-level --stream-interval. Values below engine setting are clamped up to it. The first and final outputs are always emitted immediately.

structured_outputs = None class-attribute instance-attribute

Parameters for configuring structured outputs.

temperature = 1.0 class-attribute instance-attribute

Controls the randomness of the sampling. Lower values make the model more deterministic, while higher values make the model more random. Zero means greedy sampling.

thinking_token_budget = None class-attribute instance-attribute

Maximum number of tokens allowed for thinking operations.

top_k = 0 class-attribute instance-attribute

Controls the number of top tokens to consider. Set to 0 (or -1) to consider all tokens.

top_p = 1.0 class-attribute instance-attribute

Controls the cumulative probability of the top tokens to consider. Must be in (0, 1]. Set to 1 to consider all tokens.

trace_decode_token_ids = None class-attribute instance-attribute

If provided, forces the engine to emit this predetermined sequence of token IDs during decoding instead of sampling randomly. Real logprobs are still computed. Conflict checking is performed at the engine level.

watermarking = None class-attribute instance-attribute

Whether to apply the engine's configured watermark to this request. None defaults to enabled.

_validate_logit_bias(model_config)

Validate logit_bias token IDs are within vocabulary range.

_validate_stop_token_ids(model_config)

Validate stop_token_ids are within vocabulary range.

_validate_trace_replay(model_config, speculative_config)

Validate trace replay request compatibility.

clone()

If skip_clone is True, uses shallow copy instead of deep copy.

for_sampler_warmup() staticmethod

Set parameters to exercise all sampler logic.

update_from_generation_config(generation_config, eos_token_id=None)

Update if there are non-default values from generation_config.

StructuredOutputsParams

Methods:

_backend = field(default=None, init=False) class-attribute instance-attribute

CAUTION: Should only be set by Processor._validate_structured_output

_backend_was_auto = field(default=False, init=False) class-attribute instance-attribute

CAUTION: Should only be set by Processor._validate_structured_output

__post_init__()

Validate that some fields are mutually exclusive.

all_constraints_none()

Returns True if all structured-output constraint fields are None.

all_non_structural_tag_constraints_none()

Returns True if all structured-output constraint fields are None.

_exceeds_json_nesting(spec, limit)

Whether a JSON document nests objects and arrays deeper than limit.

check_json_nesting(spec, *, structural_tag=False)

Reject a JSON schema nested more than MAX_STRUCTURED_OUTPUT_JSON_NESTING levels deep, or a structural tag nested deeper than that plus STRUCTURAL_TAG_WRAPPER_NESTING.

Raises:

validate_thinking_token_budget(value)

Validate thinking_token_budget; return None if unset.