/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.
| Argument | Type/Default | Description |
|---|---|---|
| text | Optional[Union[List[str], str]] = None | The input prompt. Can be a single prompt or a batch of prompts. |
| input_ids | Optional[Union[List[List[int]], List[int]]] = None | The token IDs for text; one can specify either text or input_ids. |
| input_embeds | Optional[Union[List[List[List[float]]], List[List[float]]]] = None | The embeddings for input_ids; one can specify either text, input_ids, or input_embeds. |
| image_data | Optional[Union[List[List[ImageDataItem]], List[ImageDataItem], ImageDataItem]] = None | The 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_data | Optional[Union[List[AudioDataItem], AudioDataItem]] = None | The audio input. Can be a file name, URL, or base64 encoded string. |
| sampling_params | Optional[Union[List[Dict], Dict]] = None | The sampling parameters as described in the sections below. |
| rid | Optional[Union[List[str], str]] = None | The request ID. |
| return_logprob | Optional[Union[List[bool], bool]] = None | Whether to return log probabilities for tokens. |
| logprob_start_len | Optional[Union[List[int], int]] = None | If return_logprob, the start location in the prompt for returning logprobs. Default is “-1”, which returns logprobs for output tokens only. |
| top_logprobs_num | Optional[Union[List[int], int]] = None | If return_logprob, the number of top logprobs to return at each position. Must be an integer in [0, vocab_size], otherwise the request fails with HTTP 400. It is not capped at OpenAI’s limit of 20, and it is ignored when return_logprob is not set. |
| token_ids_logprob | Optional[Union[List[List[int]], List[int]]] = None | If return_logprob, the token IDs to return logprob for. |
| token_ids_logprob_rows | Optional[List[List[int]]] = None | Like token_ids_logprob, but one list of token IDs per scored input token: len(input_ids) - logprob_start_len - 1 lists, where list k is scored at input token logprob_start_len + k + 1 and reported in input_token_ids_logprobs[k + 1] (entry 0, and the entry of an empty list, is None). Requires a single input_ids request with return_logprob, logprob_start_len >= 0, and max_new_tokens=0. |
| return_text_in_logprobs | bool = False | Whether to detokenize tokens in text in the returned logprobs. |
| stream | bool = False | Whether to stream output. |
| lora_path | Optional[Union[List[Optional[str]], Optional[str]]] = None | The path to the LoRA. |
| custom_logit_processor | Optional[Union[List[Optional[str]], str]] = None | Custom logit processor for advanced sampling control. Must be a serialized instance of CustomLogitProcessor using its to_str() method. For usage see below. |
| return_hidden_states | Union[List[bool], bool] = False | Whether to return hidden states. |
| return_routed_experts | bool = False | Whether 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_len | int = 0 | If 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. -1 skips the prompt: it starts at prompt_tokens - 1, so the result has logical shape [num_generated_tokens, num_layers, top_k] and row i holds the experts picked in the forward pass that produced generated token i. The prompt length is taken after multimodal expansion, which the client cannot compute itself. Must be -1 or in [0, prompt_tokens]. |
return_routed_experts, a non-streaming request aborted while it holds no KV cache (queued, retracted or mid-chunk) returns no output tokens, no output logprobs and an empty routed_experts, because the routing of its earlier tokens is gone.
Sampling parameters
The object is defined atsampling_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’sgeneration_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
| Argument | Type/Default | Description |
|---|---|---|
| max_new_tokens | int = 128 | The maximum output length measured in tokens. |
| stop | Optional[Union[str, List[str]]] = None | One or multiple stop words. Generation will stop if one of these words is sampled. |
| stop_token_ids | Optional[List[int]] = None | Provide stop words in the form of token IDs. Generation will stop if one of these token IDs is sampled. IDs outside [0, vocab_size) are ignored because they can never be sampled. |
| stop_regex | Optional[Union[str, List[str]]] = None | Stop when hitting any of the regex patterns in this list. Patterns are checked before generation: compounding repetitions, repeated alternatives, excessive repeat counts or nesting, look-around, backreferences, and conditional groups are rejected. Ordinary patterns such as [.!?]\s*$, and|or, and \d+ are supported. These checks apply to stop_regex; the structured-output regex parameter has a separate grammar engine. |
| temperature | float (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_p | float (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_k | int (model default; fallback -1) | Top-k randomly selects from the k highest-probability tokens. |
| min_p | float (model default; fallback 0.0) | Min-p samples from tokens with probability larger than min_p * highest_token_probability. |
Penalizers
| Argument | Type/Default | Description |
|---|---|---|
| frequency_penalty | float = 0.0 | Penalizes 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_penalty | float = 0.0 | Penalizes 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_penalty | float = 1.0 | Scales 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_tokens | int = 0 | Forces 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.| Argument | Type/Default | Description |
|---|---|---|
| json_schema | Optional[str] = None | JSON schema for structured outputs. |
| regex | Optional[str] = None | Regex for structured outputs. |
| ebnf | Optional[str] = None | EBNF for structured outputs. |
| structural_tag | Optional[str] = None | The structal tag for structured outputs. |
Other options
| Argument | Type/Default | Description |
|---|---|---|
| n | int = 1 | Specifies 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.) Must be an integer from 1 to 4096; SGLANG_MAX_BATCH_REQS_PER_HTTP_REQ sets the upper bound, and a negative value removes it. Larger, zero, negative, non-integer, or boolean values are rejected before the request is expanded (see Validation). The OpenAI-compatible APIs keep OpenAI’s limit of 128, which a setting below 128 lowers further. |
| logit_bias | Optional[Dict[str, float]] = None | Maps token IDs to a bias added to their logits. Each key must be a token ID in [0, vocab_size) and each value a finite number with magnitude at most 1e30 (the sampler divides logits by a temperature of at least 1e-6, so larger values overflow float32); otherwise the request fails with HTTP 400. Values outside OpenAI’s [-100, 100] range are accepted. |
| ignore_eos | bool = False | Don’t stop generation when EOS token is sampled. A length finish’s meta_info.finish_reason.ignored_stop.position is the output length at which a stop token (EOS, the tokenizer’s additional stop tokens, or stop_token_ids) would have ended the request without ignore_eos. ignored_stop is null if none would have, and a beam’s length finish always reports null. |
| sampling_seed | Optional[int] = None | Seed for sampling this request. The same seed gives the same tokens when the logits are the same. Logits can differ slightly with the other requests in the batch, or with whether the prompt hits the prefix cache, unless --enable-deterministic-inference is set, so a seed alone does not make outputs reproducible across runs. With n > 1, the first sample uses the seed and each other sample a seed derived from it, so the samples differ and each is reproducible. While a batch contains a seeded request that is not greedy, it samples with the PyTorch sampler, which is slower than FlashInfer. With speculative decoding or on the MLX backend, the seed is used only with --enable-deterministic-inference. |
| skip_special_tokens | bool = True | Remove special tokens during decoding. |
| spaces_between_special_tokens | bool = True | Whether or not to add spaces between special tokens during detokenization. |
| no_stop_trim | bool = False | Don’t trim stop words or EOS token from the generated text. |
| custom_params | Optional[Dict[str, CustomParamValue]] = None | Used when employing CustomLogitProcessor. An object whose values are JSON scalars, lists of scalars, or objects of scalars; any other shape is rejected with HTTP 400. For usage, see below. |
Validation
Invalid sampling parameters are rejected with HTTP 400 before the request is scheduled. Withstream set to true, the error is instead sent as a data: {"error": ...} server-sent event followed by data: [DONE], under HTTP 200. SGLang deliberately does not impose OpenAI’s limits of temperature <= 2 or top_logprobs <= 20.
Examples
Normal
Launch a server:Command
Example
Streaming
Send a request and stream the output:Example
Multimodal
Launch a server:Command
Command
Example
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, Lark)
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. With XGrammar, ebnf also accepts Lark grammars; see Structured Outputs for the supported syntax.
SGLang supports two grammar backends:
- XGrammar (default): Supports JSON schema, regular expression, EBNF, and Lark grammar constraints.
- For EBNF, XGrammar uses the GGML BNF format.
- Outlines: Supports JSON schema and regular expression constraints.
--grammar-backend outlines flag:
Command
Example
Custom logit processor
Launch a server with--enable-custom-logit-processor flag on.
Command
Example
Example
Example
