sgl-project/sglang · error · ValueError

invalid predicate {expr!r}: {e}; allowed names are {ALLOWED_

Error message

invalid predicate {expr!r}: {e}; allowed names are {ALLOWED_NAMES}.

What it means

parse_predicate raises this when the predicate compiles but evaluating it against a dummy environment raises — typically because it references a name not in ALLOWED_NAMES (the DSL's whitelist such as rel/abs/max_abs). The message lists allowed names so the developer knows exactly what is in scope.

Source

Thrown at python/sglang/srt/debug_utils/comparator/threshold_dsl.py:71

    for rule in diff_threshold_rules:
        if re.fullmatch(rule.pattern, name):
            return rule.predicate
    raise ValueError(
        f"tensor {name!r} matched no --diff-threshold pattern "
        f"({[rule.pattern for rule in diff_threshold_rules]}); add a catch-all '.*' rule or a matching pattern."
    )


@lru_cache(maxsize=None)
def parse_predicate(expr: str) -> CodeType:
    try:
        code = compile(expr, "<predicate>", "eval")
    except SyntaxError as e:
        raise ValueError(f"invalid predicate {expr!r}: {e}") from e
    try:
        eval(code, _EVAL_GLOBALS, dict(_DUMMY_ENV))
    except Exception as e:
        raise ValueError(
            f"invalid predicate {expr!r}: {e}; allowed names are {ALLOWED_NAMES}."
        ) from e
    return code


def evaluate_predicate(
    code: CodeType, *, rel: float, max_abs: float, mean_abs: float
) -> bool:
    return bool(
        eval(
            code, _EVAL_GLOBALS, {"rel": rel, "max_abs": max_abs, "mean_abs": mean_abs}
        )
    )

View on GitHub (pinned to 0132848349)

Solutions

  1. Use only the names listed in the error's 'allowed names are [...]' message, e.g. rel, abs, max_abs
  2. Re-check spelling/case of the metric name
  3. If a genuinely needed metric is missing, extend _EVAL_GLOBALS/ALLOWED_NAMES in threshold_dsl.py

Example fix

# before
parse_predicate('diff <= 0.1')
# after
parse_predicate('rel <= 0.1')  # 'rel' is in ALLOWED_NAMES
Defensive patterns

Strategy: validation

Validate before calling

from sglang.srt.debug_utils.comparator.threshold_dsl import ALLOWED_NAMES, parse_predicate

def predicate_is_valid(expr):
    try:
        parse_predicate(expr)
        return True
    except ValueError:
        return False

Try / catch

try:
    parse_predicate(expr)
except ValueError as e:
    print(e); sys.exit(2)  # message lists allowed names

Prevention

When it happens

Trigger: Calling parse_predicate('tolerance <= 0.1') where 'tolerance' is not in ALLOWED_NAMES; reached from compute_diff, parse_diff_threshold_rules, or _ev.

Common situations: User guesses a variable name ('diff', 'value', 'epsilon') that the whitelist doesn't provide, or uses a field only present in another version of the DSL.

Related errors


AI-assisted analysis of sgl-project/sglang@0132848349 (2026-08-28). Data as JSON: /api/errors/474cf2b6c6b86a92. Report an issue: GitHub.