Skip to main content

SamplingParams

The SamplingParams class controls the sampling behavior during text generation, including temperature, top-k/top-p sampling, penalties, and structured output constraints.

Usage

Parameters

Generation Length

int
default:"128"
Maximum number of tokens to generate.
int
default:"0"
Minimum number of tokens to generate before stopping.Useful to prevent early termination.

Temperature and Sampling

float
default:"1.0"
Sampling temperature. Controls randomness in generation.
  • 0.0: Greedy decoding (deterministic)
  • < 1.0: Less random (more focused)
  • = 1.0: Neutral
  • > 1.0: More random (more creative)
float
default:"1.0"
Nucleus sampling probability threshold.Only tokens with cumulative probability >= top_p are considered.
int
default:"-1"
Top-k sampling: only consider the k most likely tokens.
  • -1: Disabled (consider all tokens)
  • > 0: Only consider top k tokens
float
default:"0.0"
Minimum probability threshold for token selection.Tokens with probability < min_p are filtered out.

Penalties

float
default:"0.0"
Penalty for tokens based on their frequency in the generated text.Range: [-2.0, 2.0]
  • Positive values: Reduce repetition
  • Negative values: Encourage repetition
float
default:"0.0"
Penalty for tokens that have already appeared in the generated text.Range: [-2.0, 2.0]
  • Positive values: Encourage diversity
  • Negative values: Encourage using the same tokens
float
default:"1.0"
Penalty for repeating tokens from the prompt or previous output.Range: [0.0, 2.0]
  • 1.0: No penalty
  • > 1.0: Discourage repetition
  • < 1.0: Encourage repetition

Stop Conditions

Optional[Union[str, List[str]]]
default:"None"
String(s) that will stop generation when encountered.
Optional[List[int]]
default:"None"
Token IDs that will stop generation when encountered.
Optional[Union[str, List[str]]]
default:"None"
Regular expression(s) that will stop generation when matched.
bool
default:"False"
Ignore the end-of-sequence token and continue generating.Useful when you want to generate exactly max_new_tokens tokens.

Structured Output

Optional[str]
default:"None"
JSON schema for structured output generation.
Optional[str]
default:"None"
Regular expression constraint for output generation.
Optional[str]
default:"None"
EBNF grammar constraint for output generation.
Only one of json_schema, regex, or ebnf can be set at a time.

Output Control

bool
default:"True"
Skip special tokens in the output text.
bool
default:"True"
Add spaces between special tokens in the output.
bool
default:"False"
Don’t trim the stop string from the output.By default, stop strings are removed from output. Set to True to keep them.

Advanced Options

int
default:"1"
Number of completions to generate for each prompt.
Optional[Dict[str, float]]
default:"None"
Bias to add to logits of specific tokens.Keys are token IDs (as strings), values are bias values.
Optional[int]
default:"None"
Random seed for sampling. Enables reproducible generation.
Optional[int]
default:"None"
Token interval for streaming. Return output every N tokens.
Optional[Dict[str, Any]]
default:"None"
Custom parameters for specialized use cases.

Common Patterns

Greedy Decoding (Deterministic)

Balanced Generation

Creative Writing

Structured JSON Output

Format Constraint (Phone Number)

Code Generation

Reproducible Output

Usage Examples

Basic Usage

Multiple Completions

Controlled Repetition

JSON Output

Regex Constraint

Validation

The SamplingParams class includes validation to ensure parameters are within valid ranges:
  • temperature >= 0.0
  • 0.0 < top_p <= 1.0
  • 0.0 <= min_p <= 1.0
  • top_k >= 1 or -1 (disabled)
  • -2.0 <= frequency_penalty <= 2.0
  • -2.0 <= presence_penalty <= 2.0
  • 0.0 <= repetition_penalty <= 2.0
  • 0 <= min_new_tokens <= max_new_tokens

Best Practices

For deterministic output: Use temperature=0.0 or set sampling_seed to a fixed value.
For creative tasks: Use higher temperature (0.8-1.2) with top_p=0.9-0.95.
For structured output: Use json_schema or regex constraints to ensure valid format.
To reduce repetition: Combine frequency_penalty, presence_penalty, and repetition_penalty.
Setting temperature to 0 is converted internally to temperature=1.0 with top_k=1 for greedy sampling.

See Also