sgl-project/sglang · error · ValueError

invalid predicate {expr!r}: {e}

Error message

invalid predicate {expr!r}: {e}

What it means

parse_predicate raises this when the predicate expression fails to compile as a Python expression (SyntaxError). Predicates are small boolean expressions like 'rel <= 0.0085' compiled in eval mode, so any Python syntax error (unbalanced quotes, invalid tokens, '=>') triggers this at parse time.

Source

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

    default_predicate: str,
) -> str:
    if not diff_threshold_rules:
        return default_predicate
    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. Fix the expression to be valid Python syntax: one comparison over names like rel, abs, max_abs
  2. Check shell quoting — wrap each predicate in single quotes
  3. Test standalone: python -c "compile('rel <= 0.1','<p>','eval')"

Example fix

# before
parse_diff_threshold_rules(['.*','rel => 0.1'])
# after
parse_diff_threshold_rules(['.*','rel <= 0.1'])
Defensive patterns

Strategy: validation

Validate before calling

def predicate_compiles(expr):
    try:
        compile(expr, '<predicate>', 'eval')
        return True
    except SyntaxError:
        return False

Try / catch

try:
    code = parse_predicate(expr)
except ValueError as e:
    print(e); sys.exit(2)

Prevention

When it happens

Trigger: Calling parse_predicate('rel <= ') or parse_diff_threshold_rules with a malformed predicate token; also via compute_diff which parses predicates lazily.

Common situations: Typos like '=>' instead of '<=', missing operand, stray characters, or shell mangling of quotes/<= operators in the predicate string.

Related errors


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