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(agent): a null `choice` label made the answer undecodable

PerryLink30d323412a9e
diff --git a/laya/agent.py b/laya/agent.py
index 1b58de6f..feef2ca1 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -587,7 +587,23 @@ class Agent(HookRegistry):
raise ValueError(
"question %r: choice label %d is a %s; a label is rendered as option text "
"and used as the answer key, so it must be a scalar (a string, number or "
- "None), got %r" % (qid, i, type(label).__name__, label))
+ "bool), got %r" % (qid, i, type(label).__name__, label))
+ if label is None:
+ # The score path rejects a null level with the same argument one question type
+ # over: a label is option text AND the answer key, and a null one can be
+ # neither. `_to_internal` normalises a list of labels to `{label: None}`, so a
+ # null label became the option text "None" (`str(None)`) while the answer key
+ # and the `probabilities` key for that same option were the JSON string "null",
+ # because a dict key has to be a str. A client then cannot tell whether that
+ # option was the *string* `"null"` or JSON `null`, and the answer key is
+ # unreachable: `criteria[answer["choice"]]` yields `None`, and
+ # `answer["choice"] == "null"` is False for it. `""` stays accepted, because
+ # unlike a null it does round-trip.
+ raise ValueError(
+ "question %r: choice label %d is null; a label is rendered as option text "
+ "and used as the answer key, so it must be a string, number or bool -- a "
+ "null label renders as the text \"None\" while its answer key is \"null\""
+ % (qid, i))
elif t == "score":
if not isinstance(crit, list):
raise ValueError("question %r: a score question takes 'criteria' as a list of level "
diff --git a/tests/test_criteria.py b/tests/test_criteria.py
index a5dacea5..901a2b8a 100644
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@ -294,6 +294,16 @@ for label, qdef in [
# value, which a Jev client refuses to parse (#302)
("score with a null level", {"type": "score", "instructions": "How urgent?",
"criteria": ["low", None, "high"]}),
+ # the same defect one question type over (#302): a null *label* is option text AND the answer
+ # key, and `_to_internal` normalises the list to `{label: None}`, so the option read "None"
+ # while its answer key and probabilities key were the JSON string "null" -- a client cannot
+ # tell that apart from the string `"null"`, and the key is unreachable from the response
+ ("choice with a null label", {"type": "choice", "instructions": "Which team?",
+ "criteria": ["billing", None]}),
+ ("choice with a null label first", {"type": "choice", "instructions": "Which team?",
+ "criteria": [None, "billing"]}),
+ ("choice with only a null label", {"type": "choice", "instructions": "Which team?",
+ "criteria": [None]}),
("choice with labels", {"type": "choice", "instructions": "Which team?",
"criteria": ["billing", "tech"],
"labels": {"false": "B", "true": "A"}}),
@@ -339,6 +349,31 @@ try:
except ValueError as e:
check_true("rejected/score null level names the level", "level 1" in str(e), str(e))
+# ...and a null choice label names the label, with the reason it is refused
+try:
+ agent.system_one(STATE, {"q": {"type": "choice", "instructions": "Which team?",
+ "criteria": ["billing", None]}})
+ FAIL.append("rejected/choice null label names the label: no error raised")
+except ValueError as e:
+ check_true("rejected/choice null label names the label", "label 1" in str(e), str(e))
+ check_true("rejected/choice null label says why it is refused",
+ "answer key" in str(e), str(e))
+
+# an empty-string label is NOT the same case: unlike a null it round-trips, so it stays accepted
+# (`render_options` renders it as "", the answer key is "", and `criteria[""]` finds it)
+_empty_label = None
+try:
+ _empty_label = agent.system_one(STATE, {"q": {"type": "choice", "instructions": "Which team?",
+ "criteria": ["billing", ""]}})["answers"]["q"]
+except Exception as e: # noqa: BLE001
+ FAIL.append("accepted/an empty-string label raised %r" % e)
+if _empty_label is not None:
+ check_true("accepted/an empty-string label answers", _empty_label["choice"] in ("billing", ""),
+ str(_empty_label.get("choice")))
+ check_true("accepted/its probabilities keys match the labels it sent",
+ sorted(_empty_label["probabilities"]) == ["", "billing"],
+ str(sorted(_empty_label["probabilities"])))
+
# the same questions through the public entry point, not only the method under it
router = Router()
router.attach("english", agent)