Principle:Sgl project Sglang Generation Function Definition
| Knowledge Sources | |
|---|---|
| Domains | Frontend_DSL, Programming_Model, LLM_Programming |
| Last Updated | 2026-02-10 00:00 GMT |
Overview
A domain-specific language (DSL) pattern that defines structured LLM interaction programs using decorated Python functions with role annotations and generation primitives.
Description
Generation function definition is the core programming model of SGLang's frontend DSL. Users define Python functions decorated with @sgl.function that use a special ProgramState object (conventionally named s) to build up prompts and trigger generation. The DSL provides role markers (sgl.user(), sgl.assistant(), sgl.system()) for chat formatting, generation primitives (sgl.gen()) for text generation with optional constraints, and append semantics (s += text) for building prompts incrementally.
Usage
Define generation functions when building complex LLM programs that involve multi-turn conversations, structured output, conditional logic, or branching. The DSL approach is more expressive than raw API calls for programs with multiple generation steps.
Theoretical Basis
The DSL follows an imperative prompt programming paradigm:
- Program state accumulates text and metadata
- Role markers apply chat template formatting
- Generation primitives trigger LLM calls and capture outputs
- Control flow uses standard Python (if/else, loops, function calls)
Key primitives:
- s += text — Append text to the prompt
- sgl.gen("name") — Generate text and store in variable
- sgl.user()/assistant()/system() — Apply chat role formatting
- sgl.select("name", choices) — Select from fixed options