sgl-project/sglang · warning · ValueError
temp_set_env should not be used for sglang env vars
Error message
temp_set_env should not be used for sglang env vars
What it means
SGLang's test/helper utility temp_set_env refuses to set environment variables whose names start with SGLANG_ or SGL_. SGLang env vars are managed centrally through python/sglang/srt/environ.py, and ad-hoc mutation would bypass parsing, defaults, and deprecation handling.
Source
Thrown at python/sglang/srt/utils/common.py:1284
return default
try:
return int(value)
except ValueError:
return default
@contextmanager
def temp_set_env(*, allow_sglang: bool = False, **env_vars: Any):
"""Temporarily set environment variables, restoring originals on exit.
By default, SGLANG_*/SGL_* keys are rejected — use ``Envs`` descriptors
for those. Pass ``allow_sglang=True`` only for special env vars that
intentionally bypass ``environ.py``.
"""
if not allow_sglang:
for key in env_vars:
if key.startswith("SGLANG_") or key.startswith("SGL_"):
raise ValueError("temp_set_env should not be used for sglang env vars")
backup = {key: os.environ.get(key) for key in env_vars}
try:
for key, value in env_vars.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = str(value)
yield
finally:
for key, value in backup.items():
if value is None:
os.environ.pop(key, None)
else:
os.environ[key] = value
def support_triton(backend: str) -> bool:View on GitHub (pinned to 0132848349)
Solutions
- Register/define the variable in python/sglang/srt/environ.py and set it via the proper mechanism instead of temp_set_env
- If the var is intentionally outside environ.py, pass allow_sglang=True explicitly
- Prefer injecting configuration through ServerArgs/function parameters rather than env mutation
Example fix
# before
temp_set_env({"SGLANG_DISABLE_TOKENIZER_BATCH": "1"})
# after
temp_set_env({"SGLANG_DISABLE_TOKENIZER_BATCH": "1"}, allow_sglang=True)
# or better: use the environ.py accessor in test Defensive patterns
Strategy: validation
Validate before calling
def safe_temp_set_env(env_vars: dict) -> None:
bad = [k for k in env_vars if k.startswith(('SGLANG_', 'SGL_'))]
if bad and not getattr(safe_temp_set_env, 'allow_sglang', False):
raise ValueError(f'register {bad} in environ.py instead')
temp_set_env(env_vars, allow_sglang=getattr(safe_temp_set_env, 'allow_sglang', False)) Prevention
- Define all SGLANG_* vars in python/sglang/srt/environ.py
- Use allow_sglang=True only with a comment explaining the bypass
- Prefer ServerArgs parameters over env toggles in tests
When it happens
Trigger: Calling temp_set_env({'SGLANG_X': '1'}) or with any SGL_/SGLANG_-prefixed key without allow_sglang=True. Commonly hit in tests (test_from_env_bool, test_from_env_str) and in weight-loading or eval helpers that try to toggle features via env.
Common situations: Writing new tests that flip an SGLANG_ feature flag; refactoring code that previously used os.environ directly; adding a genuinely special env var that intentionally bypasses environ.py.
Related errors
- Unknown serve backend {name!r}. Available values: {available
- Multiple distributions register serve backend {name!r}: {pro
- Failed to load serve backend {name!r} from {self._entry_poin
- Serve backend {name!r} factory returned {type(backend).__nam
- Serve backend {name!r} uses API version {backend.api_version
AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28).
Data as JSON: /api/errors/758856fbe52a1044.
Report an issue: GitHub.