Expose the declared sglang env vars of a scheduler in its internal state (#35928)

This commit is contained in:
fzyzcjy
2026-08-24 20:20:29 +08:00
committed by GitHub
parent c56cee0f80
commit 6dd79576cd
3 changed files with 158 additions and 5 deletions
+32 -4
View File
@@ -1,3 +1,4 @@
import base64
import functools
import json
import os
@@ -24,6 +25,9 @@ def _default_hip() -> bool:
return False
_NON_UTF8_PREFIX = "base64:"
def _default_cache_subdir(name: str) -> str:
"""A directory under SGLANG_CACHE_DIR, for env defaults that track it.
@@ -36,11 +40,12 @@ def _default_cache_subdir(name: str) -> str:
class EnvField:
_allow_set_name = True
def __init__(self, default: Any):
def __init__(self, default: Any, secret: bool = False):
self.default = default
# NOTE: environ can only accept str values, so we need a flag to indicate
# whether the env var is explicitly set to None.
self._set_to_none = False
self.secret = secret
def __set_name__(self, owner, name):
assert EnvField._allow_set_name, "Usage like `a = envs.A` is not allowed"
@@ -156,8 +161,8 @@ class _DeprecatedEnvFallback:
SGLANG_DSA_FUSE_TOPK = EnvBoolWithAlias(True, deprecated_name="SGLANG_NSA_FUSE_TOPK")
"""
def __init__(self, default: Any, deprecated_name: str):
super().__init__(default)
def __init__(self, default: Any, deprecated_name: str, secret: bool = False):
super().__init__(default, secret=secret)
self.deprecated_name = deprecated_name
def get(self) -> Any:
@@ -317,6 +322,7 @@ class Envs:
# too short when many workers cold-start and load tokenizers in parallel.
SGLANG_UVICORN_WORKER_HEALTHCHECK_TIMEOUT = EnvInt(10)
SGLANG_ENABLE_HEALTH_ENDPOINT_GENERATION = EnvBool(True)
SGLANG_EXPOSE_OWN_ENV_VARS = EnvBool(False)
# ===================================================================
# Logging
@@ -670,7 +676,7 @@ class Envs:
# Native web search (Exa). EXA_API_KEY is the vendor BYOK credential
# (kept as-is, not renamed to SGLANG_*); the SGLANG_EXA_* knobs tune the
# request defaults for the built-in GPT-OSS web_search tool.
EXA_API_KEY = EnvStr(None)
EXA_API_KEY = EnvStr(None, secret=True)
SGLANG_EXA_NUM_RESULTS = EnvInt(10)
SGLANG_EXA_SEARCH_TYPE = EnvStr("auto")
SGLANG_EXA_INCLUDE_HIGHLIGHTS = EnvBool(True)
@@ -1539,6 +1545,28 @@ envs = Envs()
EnvField._allow_set_name = False
def exportable_env_vars() -> dict[str, str]:
return {
field.name: _exportable_value(os.environ[field.name])
for field in sorted(
(value for value in vars(Envs).values() if isinstance(value, EnvField)),
key=lambda field: field.name,
)
if not field.secret and field.name in os.environ
}
def _exportable_value(value: str) -> str:
try:
value.encode()
except UnicodeEncodeError:
return (
_NON_UTF8_PREFIX
+ base64.b64encode(value.encode(errors="surrogateescape")).decode()
)
return value
class _DeprecatedEnv:
"""One deprecated env var: warn if it is set, and optionally forward its
(possibly transformed) value to a replacement env var."""
+4 -1
View File
@@ -102,7 +102,7 @@ from sglang.srt.distributed import get_pp_group, get_world_group
from sglang.srt.distributed.parallel_state import get_tp_group
from sglang.srt.distributed.parallel_state_wrapper import ParallelState
from sglang.srt.dllm.mixin.scheduler import SchedulerDllmMixin
from sglang.srt.environ import envs
from sglang.srt.environ import envs, exportable_env_vars
from sglang.srt.eplb.expert_distribution import get_global_expert_distribution_recorder
from sglang.srt.hardware_backend.mlx.runtime import use_mlx
from sglang.srt.layers.dp_attention import compute_dp_attention_world_info
@@ -4463,6 +4463,9 @@ class Scheduler(
if info_record is not None:
ret["dspark_info_record"] = info_record
if envs.SGLANG_EXPOSE_OWN_ENV_VARS.get():
ret["env_vars"] = exportable_env_vars()
# These fields are not msgpack-serializable (a config object and a bound
# signal handler); no reader consumes them.
ret.pop("model_config", None)