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(confidence): read a probability too wide for a double as no number, not as a crash

Aashishedd7f269f5b4
diff --git a/laya/confidence.py b/laya/confidence.py
index 8e8e5114..4c790a02 100644
--- a/laya/confidence.py
+++ b/laya/confidence.py
@@ -64,7 +64,10 @@ def jev_confidence(answer: Dict[str, Any]) -> Optional[float]:
p = [float(probabilities[key]) for key in sorted(probabilities, key=int)]
else:
p = [float(value) for value in probabilities.values()]
- except (TypeError, ValueError):
+ except (TypeError, ValueError, OverflowError):
+ # `OverflowError` is the same "does not read as a number": a JSON integer literal too wide
+ # for a double (a 400-digit probability) overflows `float()`, and an infinite score level
+ # overflows `int()`. `calibrate.py:780` already names it for the same reason.
return None
if (any(isinstance(v, bool) for v in probabilities.values())
or not all(math.isfinite(v) and v >= 0.0 for v in p)):
diff --git a/tests/test_confidence.py b/tests/test_confidence.py
index d11dabf9..a08821c5 100644
--- a/tests/test_confidence.py
+++ b/tests/test_confidence.py
@@ -2476,6 +2476,14 @@ for _name, _answer in (
("all-negative-probabilities", _choice(-0.9, -0.1)),
("negative-total-near-zero", _choice(-0.61, 0.6)),
("negative-score-level", _score(1.5, -0.5, 0.0)),
+ # `float()` and `int()` do not only refuse a value that is not a number, they also refuse one
+ # that is too wide to become one: JSON's grammar has no double limit, so a 400-digit integer
+ # probability arrives as a Python `int` and `float()` raises `OverflowError` out of a function
+ # whose docstring promises no number rather than a made-up one.
+ ("overflowing-integer-probability", _choice(10 ** 400, 1)),
+ ("json-integer-too-wide-for-a-double",
+ _json.loads('{"type":"choice","probabilities":{"a":' + "9" * 400 + ',"b":0.1}}')),
+ ("overflowing-score-level", {"type": "score", "probabilities": {float("inf"): 0.5, 2: 0.5}}),
):
check("jev_confidence/none-for-%s" % _name, jev_confidence(_answer), None)