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.

Merge pull request #508 from PerryLink/fix/null-choice-label

Nandakishora468a3b42648
diff --cc laya/agent.py
index 450ecd62,feef2ca1..9d3b0791
--- a/laya/agent.py
+++ b/laya/agent.py
@@@ -600,33 -587,23 +600,49 @@@ 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))
+ # `_to_internal` normalises the list form to `{label: None}`, so those labels become
+ # the answer keys. Two entries that land on one key made the model score fewer options
+ # than the caller wrote, and the response carry fewer probabilities than their list,
+ # without a word -- the same silent-shape class as the two checks above. Python
+ # treats values as one key whenever they compare equal, so `[1, 1.0]` and `[True, 1]`
+ # collapse as well as an exact repeat. An unhashable label raised
+ # `TypeError: cannot use 'tuple' as a dict key` from `_to_internal`, three frames down,
+ # which names neither the question nor the label -- and which `serve` cannot classify
+ # as a caller error, so over HTTP it became a 500 "inference failed" instead of a 422.
+ if isinstance(crit, list):
+ keys: Dict[Any, int] = {}
+ for i, label in enumerate(crit):
+ try:
+ first = keys[label]
+ except TypeError as exc:
+ raise ValueError(
+ "question %r: choice label %d (%r) cannot be an answer key because it is "
+ "unhashable; a label is rendered as option text and used as the answer "
+ "key" % (qid, i, label)) from exc
+ except KeyError:
+ keys[label] = i
+ else:
+ raise ValueError(
+ "question %r: choice label %d (%r) repeats label %d; the labels are the "
+ "answer keys, so every option needs its own (1, 1.0 and True are one "
+ "key)" % (qid, i, label, first))
elif t == "score":
if not isinstance(crit, list):
raise ValueError("question %r: a score question takes 'criteria' as a list of level "
diff --cc tests/test_criteria.py
index 44f25c8a,901a2b8a..445f51c2
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@@ -352,23 -349,30 +362,47 @@@ try
except ValueError as e:
check_true("rejected/score null level names the level", "level 1" in str(e), str(e))
+# a colliding label names the repeat and the label it repeats, so a twenty-option question can be
+# fixed without guessing which pair collided
+try:
+ agent.system_one(STATE, {"q": {"type": "choice", "instructions": "Which team?",
+ "criteria": ["billing", "tech", "billing"]}})
+ FAIL.append("rejected/duplicate choice label names both: no error raised")
+except ValueError as e:
+ check_true("rejected/duplicate choice label names both",
+ "label 2" in str(e) and "label 0" in str(e) and "billing" in str(e), str(e))
+
+# the option count the caller wrote is preserved, so the guard must not reject distinct labels
+try:
+ out = agent.system_one(STATE, {"q": {"type": "choice", "instructions": "Which team?",
+ "criteria": ["billing", "tech", "sales"]}})
+ check("rejected/distinct labels still answer", len(out["answers"]["q"]["probabilities"]), 3)
+except Exception as e: # noqa: BLE001
+ FAIL.append("rejected/distinct labels still answer: %s" % 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()