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): reject non-scalar choice labels exactly, not by deny-list

thread3de60e56615396
diff --git a/laya/agent.py b/laya/agent.py
index 82342ade..abe8d045 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -884,13 +884,15 @@ class Agent(HookRegistry):
"label -> description, or a list of labels" % (qid,))
if not crit:
raise ValueError("question %r: a choice question needs at least one criterion" % (qid,))
- # A label is used as a dict key when a list of labels is normalised, so a list, dict
- # or set label raised `TypeError: unhashable type: 'list'` from 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.
- # Labels are rendered as option text, so a nested structure has no meaning here.
+ # A label is used as a dict key when a list of labels is normalised, so an unhashable
+ # label raised `TypeError` from 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. Requiring the documented scalars exactly
+ # also catches the hashable-but-not-scalar shapes (`tuple`, `frozenset`, `bytes`,
+ # `complex`) that the old deny-list let through to the same 500. Labels are rendered as
+ # option text, so a nested structure has no meaning here.
for i, label in enumerate(crit if isinstance(crit, list) else crit.keys()):
- if isinstance(label, (list, dict, set, bytearray)):
+ if label is not None and not isinstance(label, (str, int, float, bool)):
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 "
diff --git a/tests/test_criteria.py b/tests/test_criteria.py
index e2a42f8d..3ff59b25 100644
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@ -353,6 +353,18 @@ for label, qdef in [
"criteria": [True, 1]}),
("choice with an unhashable label", {"type": "choice", "instructions": "Which team?",
"criteria": [("billing", ["tech"]), "sales"]}),
+ # Hashable but not a scalar: the old deny-list (`list`, `dict`, `set`, `bytearray`) let these
+ # through to `_to_internal`, whose `{label: None}` key survived to `json.dumps` and raised
+ # `TypeError: keys must be str, int, float, bool or None, not tuple` -- a 500 "inference failed"
+ # over HTTP for what is a caller error, and an unusable answer key in-process.
+ ("choice with a tuple label", {"type": "choice", "instructions": "Which team?",
+ "criteria": [("billing", "tech"), "sales"]}),
+ ("choice with a frozenset label", {"type": "choice", "instructions": "Which team?",
+ "criteria": [frozenset({"billing", "tech"}), "sales"]}),
+ ("choice with a bytes label", {"type": "choice", "instructions": "Which team?",
+ "criteria": [b"billing", "sales"]}),
+ ("choice with a complex label", {"type": "choice", "instructions": "Which team?",
+ "criteria": [1 + 2j, "sales"]}),
]:
try:
agent.system_one(STATE, {"q": qdef})