env-var-conventions
Environment Variables — Conventions
Apply this skill when adding, renaming, or reviewing any sglang-owned environment variable (SGLANG_*, or a legacy SGL_* alias being phased out), or when touching python/sglang/srt/environ.py.
Rule 1 — Define in the Envs class in python/sglang/srt/environ.py
All sglang-owned env vars live as EnvField descriptors on the Envs class. Never add a new os.getenv("SGLANG_..."), get_bool_env_var("SGLANG_..."), or get_int_env_var("SGLANG_...") call site — the helpers in python/sglang/srt/utils/common.py carry an explicit FIXME: move your environment variable to sglang.srt.environ and exist only for pre-existing call sites.
Group the new entry under an existing section comment (e.g. # Logging Options, # Scheduler: recv interval, # Flashinfer). Add a new section comment only when none fits — never drop a new entry at the bottom of an unrelated block.
Decision table: register in Envs or use os.getenv?
| Variable | Owner | Goes through Envs? |
|---|---|---|
| SGLANG_* | sglang | Always. The canonical prefix for all new entries. |
| MOONCAKE_*, ASCEND_*, DEEP_NORMAL_*, IS_H200, USE_TRITON_W8A8_FP8_KERNEL, HF_HUB_DISABLE_XET, DISABLE_OPENAPI_DOC | Upstream/vendor alias that sglang wants to centralize | Yes — register in Envs so .get() / .override() work uniformly. Keep the upstream prefix. |
| CUDA_*, NCCL_*, TORCH_*, OMP_*, HF_HUB_* (raw upstream) | External tooling | No. Read with os.getenv — they're set by the launcher / driver, not by sglang. |
| RANK, LOCAL_RANK, WORLD_SIZE, MASTER_ADDR, MASTER_PORT, HOME, PATH | Distributed launcher / OS | No. os.getenv only. |
| Test runner internals (PYTEST_CURRENT_TEST, etc.) | Test framework | No. os.getenv only. |
SGL_* is not a valid prefix — the legacy alias was removed in SGLang 1.0, and server startup stops when an SGL_<X> is set for a registered SGLANG_<X> (validate_removed_options). Never define a new SGL_* descriptor or os.getenv("SGL_...") call site; if you see one in code, it's tech debt to migrate.
The rule of thumb: if the value's lifecycle is owned by sglang code (we read it, we may want to override it in tests, we may want to rename it), put it in Envs. If the value is set by something outside sglang and we only consume it as-is, use os.getenv.
Rule 2 — Pick the typed descriptor
| Type | Use for |
|---|---|
| EnvBool(default) | boolean flag |
| EnvInt(default) | integer |
| EnvFloat(default) | float |
| EnvStr(default) | string |
| EnvTuple(()) | comma-separated list, parsed via s.split(",") and stripped |
Default value:
- For a knob whose "unset" state must be distinguishable from any concrete value, use
Noneas the default (e.g.EnvStr(None),EnvInt(None)). The descriptor handles set-to-None correctly via_set_to_none. - For a feature flag, the default encodes the production behavior.
ENABLE_FOO = EnvBool(False)means foo is off in prod;DISABLE_FOO = EnvBool(False)means foo is on in prod. See Rule 4 on picking the verb.
IntEnum for multi-state knobs
When a knob has more than two discrete states (e.g. an off / soft / strict ladder), define an IntEnum next to Envs and pass the enum member as the EnvInt default. Callers compare against the enum, not raw integers:
class ToolStrictLevel(IntEnum):
OFF = 0
FUNCTION = 1
PARAMETER = 2
SGLANG_TOOL_STRICT_LEVEL = EnvInt(ToolStrictLevel.OFF)
# At the call site:
if envs.SGLANG_TOOL_STRICT_LEVEL.get() >= ToolStrictLevel.PARAMETER:
...Don't pile SGLANG_ENABLE_FOO_STRICT / SGLANG_ENABLE_FOO_OFF boolean knobs that fight each other — one ordered integer is cleaner.
Rule 3 — Access via the EnvField API, never raw os.environ
from sglang.srt.environ import envs
if envs.SGLANG_FOO.get():
...envs.SGLANG_FOO is the descriptor itself; its __bool__ and __len__ raise on purpose so that if envs.SGLANG_FOO: fails loudly instead of silently reading as truthy. The .get() is mandatory at every read site.
Full API surface
| Method | Use |
|---|---|
| .get() | Read value (parsed). Returns default if unset, or None if explicitly set to None. |
| .set(value) | Set value. set(None) flips the internal _set_to_none flag so the next .get() returns None, not default. |
| .clear() | Unset entirely. Next .get() returns default. |
| .is_set() | True iff the key is present in os.environ (regardless of value, including the explicit-None case). |
| .override(value) | Context manager — set on enter, restore exactly what was there on exit. Use this in tests. |
The _set_to_none distinction matters: clear() → is_set()=False, get()=default; set(None) → is_set()=True, get()=None. A few descriptors (e.g. SGLANG_TEST_MAX_RETRY = EnvInt(None), SGLANG_DISAGGREGATION_THREAD_POOL_SIZE = EnvInt(None)) rely on this to distinguish "user opted out" from "default applies".
Test overrides
with envs.SGLANG_TEST_RETRACT.override(True):
...Don't mutate os.environ directly in tests — override restores the original cleanly, including the explicit-None state, even if the block raises.
For multiple overrides composed dynamically, use ExitStack:
with ExitStack() as stack:
for name, value in test_envs.items():
stack.enter_context(getattr(envs, name).override(value))
run_test()Subprocess inheritance
override mutates the real os.environ, so child processes spawned inside the with block inherit the override. This is the
