Jump to content

Connect Leeroopedia MCP: Equip your AI agents to search best practices, build plans, verify code, diagnose failures, and look up hyperparameter defaults.

Implementation:Sgl project Sglang Sgl Function Decorator

From Leeroopedia


Knowledge Sources
Domains Frontend_DSL, Programming_Model, LLM_Programming
Last Updated 2026-02-10 00:00 GMT

Overview

Concrete tool for defining SGLang generation programs using the @sgl.function decorator and DSL primitives.

Description

The @sgl.function decorator wraps a Python function into an SglFunction object that can be executed via .run() or .run_batch(). The decorated function must take s (ProgramState) as its first argument. Inside the function, sgl.gen() creates generation expressions, sgl.user()/assistant()/system() create role markers, and s += expr appends content to the program state.

Usage

Decorate any function with @sgl.function to create an executable LLM program. Use sgl.gen("name") to generate and capture text, sgl.user()/assistant() for chat formatting.

Code Reference

Source Location

  • Repository: sglang
  • File: python/sglang/lang/api.py
  • Lines: L23-32 (function decorator), L75-139 (gen), L253-262 (user/assistant/system)
  • SglFunction class: python/sglang/lang/ir.py:L141-158

Signature

# Decorator
@sgl.function
def my_program(s, arg1, arg2, ...):
    """s is ProgramState, remaining args are user-defined."""
    s += sgl.system("System prompt")
    s += sgl.user("User message")
    s += sgl.assistant(sgl.gen("response", max_tokens=128))

# Equivalently:
def my_program(s, arg1, arg2, ...):
    ...
my_program = sgl.function(my_program)

Import

import sglang as sgl

@sgl.function
def my_program(s):
    s += sgl.gen("output")

I/O Contract

Inputs

Name Type Required Description
func Callable Yes Python function with 's' as first parameter
num_api_spec_tokens Optional[int] No Tokens for speculative execution

Outputs

Name Type Description
SglFunction SglFunction Executable program with .run() and .run_batch() methods

Usage Examples

Simple Chat Program

import sglang as sgl

@sgl.function
def simple_chat(s, question):
    s += sgl.system("You are a helpful assistant.")
    s += sgl.user(question)
    s += sgl.assistant(sgl.gen("answer", max_tokens=256))

runtime = sgl.Runtime(model_path="meta-llama/Llama-3.1-8B-Instruct")
sgl.set_default_backend(runtime)

state = simple_chat.run(question="What is machine learning?")
print(state["answer"])

Multi-Step Program

@sgl.function
def multi_step(s, topic):
    s += sgl.user(f"Give me 3 key facts about {topic}.")
    s += sgl.assistant(sgl.gen("facts", max_tokens=200))

    s += sgl.user("Now summarize those facts in one sentence.")
    s += sgl.assistant(sgl.gen("summary", max_tokens=100))

state = multi_step.run(topic="quantum computing")
print("Facts:", state["facts"])
print("Summary:", state["summary"])

Constrained Output

@sgl.function
def extract_info(s, text):
    s += sgl.user(f"Extract the person's name and age from: {text}")
    s += sgl.assistant(sgl.gen("json", json_schema=schema_str, max_tokens=128))

Related Pages

Implements Principle

Requires Environment

Page Connections

Double-click a node to navigate. Hold to expand connections.
Principle
Implementation
Heuristic
Environment