chat/completionscompletions
Launch A Server
Launch the server in your terminal and wait for it to initialize.Example
Chat Completions
Usage
The server fully implements the OpenAI API. It will automatically apply the chat template specified in the Hugging Face tokenizer, if one is available. You can also specify a custom chat template with--chat-template when launching the server.
Example
Model Thinking/Reasoning Support
Some models support internal reasoning or thinking processes that can be exposed in the API response. SGLang provides unified support for various reasoning models through thechat_template_kwargs parameter and compatible reasoning parsers.
Supported Models and Configuration
| Model Family | Chat Template Parameter | Reasoning Parser | Notes |
|---|---|---|---|
| DeepSeek-R1 (R1, R1-0528, R1-Distill) | enable_thinking | --reasoning-parser deepseek-r1 | Standard reasoning models |
| DeepSeek-V3.1 | thinking | --reasoning-parser deepseek-v3 | Hybrid model (thinking/non-thinking modes) |
| Qwen3 (standard) | enable_thinking | --reasoning-parser qwen3 | Hybrid model (thinking/non-thinking modes) |
| Qwen3-Thinking | N/A (always enabled) | --reasoning-parser qwen3-thinking | Always generates reasoning |
| Kimi | N/A (always enabled) | --reasoning-parser kimi | Kimi thinking models |
| Gpt-Oss | N/A (always enabled) | --reasoning-parser gpt-oss | Gpt-Oss thinking models |
Basic Usage
To use reasoning output:- Launch the server with the appropriate reasoning parser.
- Use the model-specific parameter in
chat_template_kwargswhen you need to override the model’s chat-template default. - Optionally use
separate_reasoning: Falseto keep reasoning in the response content instead of returning it separately (defaults toTrue).
Note for Qwen3-Thinking models: These models always generate thinking content and do not support the
enable_thinking parameter. Use --reasoning-parser qwen3-thinking or --reasoning-parser qwen3 to parse the thinking content.Example: Qwen3 Models
Example
Output
Setting
"enable_thinking": False disables reasoning for Qwen3. Omitting the parameter follows the model’s chat-template default; Qwen3 enables thinking by default. Qwen3-Thinking models always generate reasoning content and do not support the enable_thinking parameter.Set a Default for OpenAI Chat Completions
If most OpenAI chat-completion requests should use the same chat-template mode, set a default when launching the server:Command
--default-chat-template-kwargs accepts a JSON object and merges it into each OpenAI chat-completion request’s chat_template_kwargs. For ordinary template keys such as enable_thinking, the precedence is:
- An explicit value in the request’s
chat_template_kwargs - The value in
--default-chat-template-kwargs - The model’s chat-template default
reasoning_effort follows a separate normalization path, so the precedence list above does not apply to it. Configure reasoning_effort in either the server defaults or individual requests, not both.
The accepted keys are model-specific. For example, Qwen3 uses enable_thinking, while other model families may use thinking or may not expose a reasoning toggle. The reasoning parser controls how reasoning tokens are separated from the response; it does not by itself enable reasoning in the chat template.
Logit Bias Support
SGLang supports thelogit_bias parameter for both chat completions and completions APIs. This parameter allows you to modify the likelihood of specific tokens being generated by adding bias values to their logits. The bias values can range from -100 to 100, where:
- Positive values (0 to 100) increase the likelihood of the token being selected
- Negative values (-100 to 0) decrease the likelihood of the token being selected
- -100 effectively prevents the token from being generated
logit_bias parameter accepts a dictionary where keys are token IDs (as strings) and values are the bias amounts (as floats).
Getting Token IDs
To uselogit_bias effectively, you need to know the token IDs for the words you want to bias. Here’s how to get token IDs:
Example
Example: DeepSeek-V3 Models
DeepSeek-V3 models support thinking mode through thethinking parameter:
Example
Output
DeepSeek-V3 models use the
thinking parameter (not enable_thinking) to control reasoning output.Example
Parameters
The chat completions API accepts OpenAI Chat Completions API’s parameters. Refer to OpenAI Chat Completions API for more details. SGLang extends the standard API with theextra_body parameter, allowing for additional customization. One key option within extra_body is chat_template_kwargs, which can be used to pass arguments to the chat template processor.
Example
Logit Bias Support
The completions API also supports thelogit_bias parameter with the same functionality as described in the chat completions section above.
Example
Returning Routed Experts (MoE Models)
For MoE models, setreturn_routed_experts: true in extra_body to return expert routing data. Requires --enable-return-routed-experts server flag. By default, routed_experts is returned in the response-level sglext object as base64-encoded int32 expert IDs in a flattened array with logical shape [num_tokens, num_layers, top_k]. For non-streaming chat responses with return_meta_info: true, routed_experts is returned only in each choice’s meta_info object and omitted from response-level sglext to avoid duplicating the routing data. Other requested extension fields, such as cached_tokens_details and spec_tokens_details, remain in response-level sglext. By default, routed experts cover [0, seqlen - 1), the full available sequence, because RL workflows need them for the full sequence. Set routed_experts_start_len in extra_body to an absolute 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 causes bottlenecks.
Example
Completions
Usage
Completions API is similar to Chat Completions API, but without themessages parameter or chat templates.
Example
Parameters
The completions API accepts OpenAI Completions API’s parameters. Refer to OpenAI Completions API for more details. Here is an example of a detailed completions request:Example
Returning Routed Experts (MoE Models)
For MoE models, setreturn_routed_experts: true in extra_body to return expert routing data. Requires --enable-return-routed-experts server flag. The routed_experts field will be returned in the sgl_ext object on each choice, containing base64-encoded int32 expert IDs as a flattened array with logical shape [num_tokens, num_layers, top_k]. By default this returns [0, seqlen - 1), the full available sequence, because RL workflows need routed experts for the full sequence. Set routed_experts_start_len in extra_body to an absolute 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.
Structured Outputs (JSON, Regex, EBNF)
For OpenAI compatible structured outputs API, refer to Structured Outputs for more details.Using LoRA Adapters
SGLang supports LoRA (Low-Rank Adaptation) adapters with OpenAI-compatible APIs. You can specify which adapter to use directly in themodel parameter using the base-model:adapter-name syntax.
Server Setup:
Command
model:adapter syntax to specify which adapter to use:
Example
extra_body
The old extra_body method is still supported for backward compatibility:
Example
model:adapter and extra_body["lora_path"] are specified, the model:adapter syntax takes precedence.
Example
