Skip to main content
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 three grammar backends:
  • XGrammar(default): Supports JSON schema, regular expression, and EBNF constraints.
  • Outlines: Supports JSON schema and regular expression constraints.
  • Llguidance: Supports JSON schema, regular expression, and EBNF constraints.
We suggest using XGrammar for its better performance and utility. XGrammar currently uses the GGML BNF format. For more details, see XGrammar technical overview. To use Outlines, simply add --grammar-backend outlines when launching the server. To use llguidance, add --grammar-backend llguidance when launching the server. If no backend is specified, XGrammar will be used as the default. For better output quality, It’s advisable to explicitly include instructions in the prompt to guide the model to generate the desired format. For example, you can specify, ‘Please generate the output in the following JSON format: …’.

OpenAI Compatible API

Example

JSON

you can directly define a JSON schema or use Pydantic to define and validate the response. Using Pydantic
Example
JSON Schema Directly
Example

EBNF

Example

Regular expression

Example

Structural Tag

Example
Example

Native API and SGLang Runtime (SRT)

JSON

Using Pydantic
Example
JSON Schema Directly
Example

EBNF

Example

Regular expression

Example

Structural Tag

Example
Example
Example

Offline Engine API

Example

JSON

Using Pydantic
Example
JSON Schema Directly
Example

EBNF

Example

Regular expression

Example

Structural Tag

Example
Example
Example