Skip to content

armgit / laya

Public Imported from GitHub
0

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.

fix(integrations): fall back for non-finite score guard values

Jin Long2ea505c4cc4e
diff --git a/laya/integrations/_guard.py b/laya/integrations/_guard.py
index e5bdb475..1988debc 100644
--- a/laya/integrations/_guard.py
+++ b/laya/integrations/_guard.py
@@ -35,18 +35,26 @@ def score_violation_probability(answer: Dict[str, Any], levels: int = 0) -> floa
usable = False
break
try:
- total += float(value)
- except (TypeError, ValueError):
+ probability = float(value)
+ except (TypeError, ValueError, OverflowError):
usable = False
break
- if usable:
+ if not math.isfinite(probability):
+ usable = False
+ break
+ total += probability
+ if usable and math.isfinite(total):
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)
+ try:
+ normalized = score / (k - 1)
+ except OverflowError:
+ return 0.0
+ return normalized if math.isfinite(normalized) else 0.0
#: The error every wrapper raises for a decision body it cannot read. Kept in one place so the
diff --git a/tests/test_crewai.py b/tests/test_crewai.py
index 176b7014..8c73a031 100644
--- a/tests/test_crewai.py
+++ b/tests/test_crewai.py
@@ -285,6 +285,12 @@ _BAD_DISTRIBUTIONS = {
"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},
+ "value_nan_string": {"probabilities": {"0": 0.1, "1": 0.1, "2": "NaN", "3": 0.4}, "score": 1.5},
+ "value_inf_string": {"probabilities": {"0": 0.1, "1": 0.1, "2": "Infinity", "3": 0.4}, "score": 1.5},
+ "value_neg_inf_string": {"probabilities": {"0": 0.1, "1": 0.1, "2": "-Infinity", "3": 0.4}, "score": 1.5},
+ "value_overflow_string": {"probabilities": {"0": 0.1, "1": 0.1, "2": "1e10000", "3": 0.4}, "score": 1.5},
+ "value_wide_int": {"probabilities": {"0": 0.1, "1": 0.1, "2": 10 ** 1000, "3": 0.4}, "score": 1.5},
+ "sum_overflow": {"probabilities": {"0": 0.1, "1": 0.1, "2": 1e308, "3": 1e308}, "score": 1.5},
}
for _label, _ans in _BAD_DISTRIBUTIONS.items():
try:
@@ -294,7 +300,9 @@ for _label, _ans in _BAD_DISTRIBUTIONS.items():
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")):
+for _label, _bad_score in (("bool_score_is_zero", True), ("nonnumeric_score_is_zero", "1.5"),
+ ("nan_score_is_zero", float("nan")), ("inf_score_is_zero", float("inf")),
+ ("neg_inf_score_is_zero", float("-inf")), ("wide_int_score_is_zero", 10 ** 1000)):
try:
check("guardprob/" + _label, score_violation_probability({"score": _bad_score}, 4), 0.0)
except Exception as _e: