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(evals): refuse a boolean confidence in _answer_confidence

Aashishc5be0c871810
diff --git a/laya/evals.py b/laya/evals.py
index 992ee695..059be9b7 100644
--- a/laya/evals.py
+++ b/laya/evals.py
@@ -484,10 +484,15 @@ def _answer_confidence(answer: Dict[str, Any]) -> Optional[float]:
and the gap between them; `docs/evals.md` says so where an operator reads the metrics.
"""
confidence = answer.get("answer_confidence")
- if isinstance(confidence, (int, float)):
+ # `bool` is a subclass of `int`, so `isinstance(True, (int, float))` is true: a JSON
+ # `"answer_confidence": true` would be read as a confidence of 1.0 and mask the numeric
+ # `confidence` fallback below. A yes/no flag is not a probability, so it is skipped exactly
+ # like an absent or non-numeric field -- the same exclusion `confidence.py` and the schema
+ # guards apply to every other caller-supplied probability.
+ if isinstance(confidence, (int, float)) and not isinstance(confidence, bool):
return float(confidence)
confidence = answer.get("confidence")
- if isinstance(confidence, (int, float)):
+ if isinstance(confidence, (int, float)) and not isinstance(confidence, bool):
return float(confidence)
if answer.get("type") == "noul":
p = float(answer.get("noul", 0.0))
diff --git a/tests/test_evals.py b/tests/test_evals.py
index 13967ddd..c67bc906 100644
--- a/tests/test_evals.py
+++ b/tests/test_evals.py
@@ -92,6 +92,21 @@ def test_calibration_uses_answer_confidence():
assert report.overall["mean_confidence"] == pytest.approx(0.9)
+def test_a_boolean_confidence_is_not_a_probability():
+ # `bool` is a subclass of `int`, so a bare `isinstance(x, (int, float))` reads a JSON
+ # `"answer_confidence": true` as a confidence of 1.0 and hides the numeric `confidence`
+ # fallback below it. A yes/no flag is not a probability, so it is skipped exactly like an
+ # absent field -- the same exclusion the schema guards apply to every other probability.
+ assert evals._answer_confidence({"answer_confidence": True, "confidence": 0.3}) == pytest.approx(0.3)
+ assert evals._answer_confidence({"answer_confidence": False, "confidence": 0.3}) == pytest.approx(0.3)
+ assert evals._answer_confidence({"confidence": True}) is None
+ assert evals._answer_confidence({"confidence": False}) is None
+ # The numeric paths are unchanged: a real `answer_confidence` still wins, and a bound of 0.0
+ # is a genuine confidence, not a missing one.
+ assert evals._answer_confidence({"answer_confidence": 0.9, "confidence": 0.2}) == pytest.approx(0.9)
+ assert evals._answer_confidence({"answer_confidence": 0, "confidence": 0.5}) == pytest.approx(0.0)
+
+
# ---------------------------------------------------------------- which confidence a metric reads
# `_answer_confidence` prefers `answer_confidence`, and an answer that carries none gets the entropy
# `confidence` -- a different quantity on a different scale (#394), which the docstring used to call