Implementation:Sgl project Sglang Sgl Gen Constrained
| Knowledge Sources | |
|---|---|
| Domains | NLP, Structured_Generation, Frontend_DSL |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
Concrete tool for generating constrained text using regex or JSON schema patterns in the SGLang frontend DSL.
Description
The sgl.gen function creates an SglGen expression that, when executed within an @sgl.function, generates text optionally constrained by a regex pattern or json_schema string. For the Engine API, constraints are passed via the regex or json_schema keys in the sampling parameters dict. The constraint backends (outlines or xgrammar) handle the FSM-guided token masking.
Usage
Use sgl.gen(regex=...) in the frontend DSL or sampling_params={"regex": ...} in the Engine API to produce constrained output. Use sgl.gen(json_schema=...) for JSON output conforming to a schema.
Code Reference
Source Location
- Repository: sglang
- File: python/sglang/lang/api.py
- Lines: L75-139
Signature
def gen(
name: Optional[str] = None,
max_tokens: Optional[int] = None,
min_tokens: Optional[int] = None,
stop: Optional[Union[str, List[str]]] = None,
temperature: Optional[float] = None,
top_p: Optional[float] = None,
top_k: Optional[int] = None,
regex: Optional[str] = None,
json_schema: Optional[str] = None,
# ... additional sampling params
) -> SglGen:
"""Generate text with optional constraints."""
Import
import sglang as sgl
# Use within @sgl.function
s += sgl.gen("output", regex=r"\d+", max_tokens=10)
I/O Contract
Inputs
| Name | Type | Required | Description |
|---|---|---|---|
| name | Optional[str] | No | Variable name to store the generated text |
| regex | Optional[str] | No | Regex pattern to constrain output |
| json_schema | Optional[str] | No | JSON schema string to constrain output |
| max_tokens | Optional[int] | No | Maximum tokens to generate |
| temperature | Optional[float] | No | Sampling temperature |
Outputs
| Name | Type | Description |
|---|---|---|
| SglGen | SglGen | Expression node for the frontend DSL execution graph |
Usage Examples
Regex-Constrained Generation (Frontend DSL)
import sglang as sgl
@sgl.function
def extract_date(s, text):
s += "Extract the date from this text: " + text + "\n"
s += "Date: " + sgl.gen("date", regex=r"\d{4}-\d{2}-\d{2}", max_tokens=10)
state = extract_date.run(text="The event is on 2024-03-15.")
print(state["date"]) # "2024-03-15"
JSON Schema Generation (Frontend DSL)
import json
import sglang as sgl
schema = json.dumps({
"type": "object",
"properties": {
"name": {"type": "string"},
"age": {"type": "integer"},
},
"required": ["name", "age"],
})
@sgl.function
def extract_person(s, description):
s += description + "\nExtract person info as JSON:\n"
s += sgl.gen("json_output", json_schema=schema, max_tokens=128)
state = extract_person.run(description="John is 30 years old.")
person = json.loads(state["json_output"])
Engine API Constrained Generation
import sglang as sgl
engine = sgl.Engine(model_path="meta-llama/Llama-3.1-8B-Instruct")
output = engine.generate(
"Generate a valid email address:",
{"regex": r"[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\.[a-zA-Z]{2,}", "max_new_tokens": 32},
)
print(output["text"]) # Guaranteed valid email format