Non-autoregressive System 1 decision engine. Typed choice, score and yes/no decisions over any text in a single forward pass, in 100+ languages, with a router that picks the right checkpoint per request.
Python calibrationclassificationdecision-modelhuggingfacejevmodernbertmultilingualnlppythonpytorchroutingtyped-decisionszero-shot
fix(integrations): guard the score-rail against a malformed probabilities block
Aashishfeef99c69a59
diff --git a/laya/integrations/_guard.py b/laya/integrations/_guard.pyindex c8d482e9..892dacac 100644--- a/laya/integrations/_guard.py+++ b/laya/integrations/_guard.py@@ -10,10 +10,39 @@ def score_violation_probability(answer: Dict[str, Any], levels: int = 0) -> floaused when the answer carries no `probabilities`, and then the normalised expected level standsin for the distribution."""- probs = answer.get("probabilities") or {}+ probs = answer.get("probabilities")+ # `probabilities` is read straight from a remote or caller decision answer, so both its outer+ # shape and its values are arbitrary JSON: a non-mapping (a list, string or number) raises at+ # `len`/`.get`, and a value that is not a real number raises inside `float` -- while a boolean+ # would read as 0.0/1.0 silently because `bool` subclasses `int`, turning a spurious `true` into+ # a full-probability violation. Normalise anything that is not a non-empty mapping to the empty+ # distribution, and sum a distribution only when every level in range holds a real number;+ # otherwise fall through to the same normalised expected level the empty path already uses, so a+ # malformed body degrades the guardrail screen instead of crashing it. A well-formed numeric+ # distribution -- including numeric strings, which `float()` already accepted -- sums unchanged.+ if not isinstance(probs, dict) or not probs:+ probs = {}k = len(probs) or levelsif k < 2:return 0.0if probs:- return sum(float(probs.get(str(i), 0.0)) for i in range(k // 2, k))- return answer.get("score", 0.0) / (k - 1)+ total = 0.0+ usable = True+ for i in range(k // 2, k):+ value = probs.get(str(i), 0.0)+ if isinstance(value, bool):+ usable = False+ break+ try:+ total += float(value)+ except (TypeError, ValueError):+ usable = False+ break+ if usable:+ return total+ # No usable distribution: stand in with the normalised expected level. `score` rides the same+ # arbitrary-answer path, so a non-number or a bool cannot be divided -- read it as 0.0 instead.+ score = answer.get("score", 0.0)+ if isinstance(score, bool) or not isinstance(score, (int, float)):+ return 0.0+ return score / (k - 1)diff --git a/tests/test_crewai.py b/tests/test_crewai.pyindex b2626c40..7489da86 100644--- a/tests/test_crewai.py+++ b/tests/test_crewai.py@@ -259,6 +259,57 @@ check_true("guard/score_fallback_flagged",harm_result({"type": "score", "score": 1.5, "confidence": 0.5})["passed"] is False)check_true("guard/score_fallback_passes", harm_result({"type": "score", "score": 0.53, "confidence": 0.5})["passed"])+# --------------------------------------------------------------- 3b. Guard probabilities are robust to a malformed answer+#+# `score_violation_probability` reads `probabilities` (and `score`) straight from a decision answer,+# so both are arbitrary remote/caller JSON. Before the guard, a non-mapping `probabilities` raised at+# `len`/`.get`; a value that is not a real number raised inside `float`; and a boolean read as+# 0.0/1.0 because `bool` subclasses `int` -- each of which crashed or mis-scored the guardrail screen.+# A distribution that is not a mapping of real numbers now falls back to the same normalised expected+# level the empty path uses; a numeric (and numeric-string) distribution sums exactly as before.+from laya.integrations._guard import score_violation_probability++_VALID = {"0": 0.10, "1": 0.15, "2": 0.30, "3": 0.45}+check_true("guardprob/valid_distribution",+ abs(score_violation_probability({"probabilities": _VALID}, 4) - 0.75) < 1e-9)+_STRINGS = {"0": "0.10", "1": "0.15", "2": "0.30", "3": "0.45"}+check_true("guardprob/numeric_strings_still_sum",+ abs(score_violation_probability({"probabilities": _STRINGS}, 4) - 0.75) < 1e-9)++# Each malformed distribution has no usable probabilities, so the answer's own score of 1.5 stands in:+# score / (k - 1) = 1.5 / 3 = 0.5. None of these may raise.+_BAD_DISTRIBUTIONS = {+ "list": {"probabilities": [0.1, 0.2, 0.3, 0.4], "score": 1.5},+ "str": {"probabilities": "0.3,0.45", "score": 1.5},+ "int": {"probabilities": 5, "score": 1.5},+ "value_str": {"probabilities": {"0": 0.1, "1": 0.1, "2": "high", "3": 0.4}, "score": 1.5},+ "value_none": {"probabilities": {"0": 0.1, "1": 0.1, "2": None, "3": 0.4}, "score": 1.5},+ "value_bool": {"probabilities": {"0": 0.1, "1": 0.1, "2": True, "3": False}, "score": 1.5},+}+for _label, _ans in _BAD_DISTRIBUTIONS.items():+ try:+ _got = score_violation_probability(_ans, 4)+ check_true("guardprob/%s_uses_score_fallback" % _label, abs(_got - 0.5) < 1e-9)+ except Exception as _e:+ check_true("guardprob/%s_uses_score_fallback" % _label, False, "%s: %s" % (type(_e).__name__, _e))++# A bool score is not a level, and a non-numeric score is not one either: both read as 0.0.+for _label, _bad_score in (("bool_score_is_zero", True), ("nonnumeric_score_is_zero", "1.5")):+ try:+ check("guardprob/" + _label, score_violation_probability({"score": _bad_score}, 4), 0.0)+ except Exception as _e:+ check_true("guardprob/" + _label, False, "%s: %s" % (type(_e).__name__, _e))++# The same malformed answer must also survive the live screen path rather than raising out of+# guard.screen() -- the guardrail degrades to a decision instead of dying.+_bad_screen = {"type": "score", "score": 1.5, "confidence": 0.5,+ "probabilities": {"0": 0.1, "1": 0.1, "2": True, "3": False}}+try:+ check_true("guardprob/screen_survives_bad_probabilities",+ isinstance(harm_result(_bad_screen)["passed"], bool))+except Exception as _e:+ check_true("guardprob/screen_survives_bad_probabilities", False, "%s: %s" % (type(_e).__name__, _e))+# `threshold` is a probability: above 1 no question could ever be flagged, so reject it.for bad in (1.5, -0.1):rejected = False