Skip to main content
This doc describes the sampling parameters of the SGLang Runtime. It is the low-level endpoint of the runtime. If you want a high-level endpoint that can automatically handle chat templates, consider using the OpenAI Compatible API.

/generate Endpoint

The /generate endpoint accepts the following parameters in JSON format. For detailed usage, see the native API doc. The object is defined at io_struct.py::GenerateReqInput. You can also read the source code to find more arguments and docs.
ArgumentType/DefaultDescription
textOptional[Union[List[str], str]] = NoneThe input prompt. Can be a single prompt or a batch of prompts.
input_idsOptional[Union[List[List[int]], List[int]]] = NoneThe token IDs for text; one can specify either text or input_ids.
input_embedsOptional[Union[List[List[List[float]]], List[List[float]]]] = NoneThe embeddings for input_ids; one can specify either text, input_ids, or input_embeds.
image_dataOptional[Union[List[List[ImageDataItem]], List[ImageDataItem], ImageDataItem]] = NoneThe image input. Supports three formats: (1) Raw images: PIL Image, file path, URL, or base64 string; (2) Processor output: Dict with format: "processor_output" containing HuggingFace processor outputs; (3) Precomputed embeddings: Dict with format: "precomputed_embedding" and feature containing pre-calculated visual embeddings. Can be a single image, list of images, or list of lists of images. See Multimodal Input Formats for details.
audio_dataOptional[Union[List[AudioDataItem], AudioDataItem]] = NoneThe audio input. Can be a file name, URL, or base64 encoded string.
sampling_paramsOptional[Union[List[Dict], Dict]] = NoneThe sampling parameters as described in the sections below.
ridOptional[Union[List[str], str]] = NoneThe request ID.
return_logprobOptional[Union[List[bool], bool]] = NoneWhether to return log probabilities for tokens.
logprob_start_lenOptional[Union[List[int], int]] = NoneIf return_logprob, the start location in the prompt for returning logprobs. Default is “-1”, which returns logprobs for output tokens only.
top_logprobs_numOptional[Union[List[int], int]] = NoneIf return_logprob, the number of top logprobs to return at each position.
token_ids_logprobOptional[Union[List[List[int]], List[int]]] = NoneIf return_logprob, the token IDs to return logprob for.
return_text_in_logprobsbool = FalseWhether to detokenize tokens in text in the returned logprobs.
streambool = FalseWhether to stream output.
lora_pathOptional[Union[List[Optional[str]], Optional[str]]] = NoneThe path to the LoRA.
custom_logit_processorOptional[Union[List[Optional[str]], str]] = NoneCustom logit processor for advanced sampling control. Must be a serialized instance of CustomLogitProcessor using its to_str() method. For usage see below.
return_hidden_statesUnion[List[bool], bool] = FalseWhether to return hidden states.
return_routed_expertsbool = FalseWhether to return routed experts for MoE models. Requires --enable-return-routed-experts server flag. With the default routed_experts_start_len=0, returns the full available sequence [0, seqlen - 1) because RL workflows need routed experts for the full sequence. The result is base64-encoded int32 expert IDs as a flattened array with logical shape [num_tokens, num_layers, top_k].
routed_experts_start_lenint = 0If return_routed_experts, the absolute start position for returned routed-experts rows. 0 preserves the default full sequence; set it to an accumulated prefix length to return only [routed_experts_start_len, seqlen - 1). For example, in multi-turn RL rollouts, routed experts for tokens from previous turns have already been collected, so setting this value avoids unnecessary transfer that cause bottlenecks. Must be in [0, prompt_tokens].

Sampling parameters

The object is defined at sampling_params.py::SamplingParams. You can also read the source code to find more arguments and docs.

Note on defaults

By default, SGLang initializes several sampling parameters from the model’s generation_config.json (when the server is launched with --sampling-defaults model, which is the default). To use SGLang/OpenAI constant defaults instead, start the server with --sampling-defaults openai. You can always override any parameter per request via sampling_params.
Command

Core parameters

ArgumentType/DefaultDescription
max_new_tokensint = 128The maximum output length measured in tokens.
stopOptional[Union[str, List[str]]] = NoneOne or multiple stop words. Generation will stop if one of these words is sampled.
stop_token_idsOptional[List[int]] = NoneProvide stop words in the form of token IDs. Generation will stop if one of these token IDs is sampled.
stop_regexOptional[Union[str, List[str]]] = NoneStop when hitting any of the regex patterns in this list
temperaturefloat (model default; fallback 1.0)Temperature when sampling the next token. temperature = 0 corresponds to greedy sampling, a higher temperature leads to more diversity.
top_pfloat (model default; fallback 1.0)Top-p selects tokens from the smallest sorted set whose cumulative probability exceeds top_p. When top_p = 1, this reduces to unrestricted sampling from all tokens.
top_kint (model default; fallback -1)Top-k randomly selects from the k highest-probability tokens.
min_pfloat (model default; fallback 0.0)Min-p samples from tokens with probability larger than min_p * highest_token_probability.

Penalizers

ArgumentType/DefaultDescription
frequency_penaltyfloat = 0.0Penalizes tokens based on their frequency in generation so far. Must be between -2 and 2 where negative numbers encourage repeatment of tokens and positive number encourages sampling of new tokens. The scaling of penalization grows linearly with each appearance of a token.
presence_penaltyfloat = 0.0Penalizes tokens if they appeared in the generation so far. Must be between -2 and 2 where negative numbers encourage repeatment of tokens and positive number encourages sampling of new tokens. The scaling of the penalization is constant if a token occurred.
repetition_penaltyfloat = 1.0Scales the logits of previously generated tokens to discourage (values > 1) or encourage (values < 1) repetition. Valid range is (0, 2]; 1.0 leaves probabilities unchanged.
min_new_tokensint = 0Forces the model to generate at least min_new_tokens until a stop word or EOS token is sampled. Note that this might lead to unintended behavior, for example, if the distribution is highly skewed towards these tokens.

Constrained decoding

Please refer to our dedicated guide on constrained decoding for the following parameters.
ArgumentType/DefaultDescription
json_schemaOptional[str] = NoneJSON schema for structured outputs.
regexOptional[str] = NoneRegex for structured outputs.
ebnfOptional[str] = NoneEBNF for structured outputs.
structural_tagOptional[str] = NoneThe structal tag for structured outputs.

Other options

ArgumentType/DefaultDescription
nint = 1Specifies the number of output sequences to generate per request. (Generating multiple outputs in one request (n > 1) is discouraged; repeating the same prompts several times offers better control and efficiency.)
ignore_eosbool = FalseDon’t stop generation when EOS token is sampled.
skip_special_tokensbool = TrueRemove special tokens during decoding.
spaces_between_special_tokensbool = TrueWhether or not to add spaces between special tokens during detokenization.
no_stop_trimbool = FalseDon’t trim stop words or EOS token from the generated text.
custom_paramsOptional[List[Optional[Dict[str, Any]]]] = NoneUsed when employing CustomLogitProcessor. For usage, see below.

Examples

Normal

Launch a server:
Command
Send a request:
Example
Detailed example in send request.

Streaming

Send a request and stream the output:
Example
Detailed example in openai compatible api.

Multimodal

Launch a server:
Command
Download an image:
Command
Send a request:
Example
The image_data can be a file name, a URL, or a base64 encoded string. See also python/sglang/srt/utils.py:load_image. Streaming is supported in a similar manner as above. Detailed example in OpenAI API Vision.

Structured Outputs (JSON, Regex, EBNF)

You can specify a JSON schema, regular expression or EBNF to constrain the model output. The model output will be guaranteed to follow the given constraints. Only one constraint parameter (json_schema, regex, or ebnf) can be specified for a request. SGLang supports two grammar backends:
  • XGrammar (default): Supports JSON schema, regular expression, and EBNF constraints.
  • Outlines: Supports JSON schema and regular expression constraints.
If instead you want to initialize the Outlines backend, you can use --grammar-backend outlines flag:
Command
Example
Detailed example in structured outputs.

Custom logit processor

Launch a server with --enable-custom-logit-processor flag on.
Command
Define a custom logit processor that will always sample a specific token id.
Example
Send a request:
Example
Send an OpenAI chat completion request:
Example