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 malformed question types with clear errors

xp34ab3f606c2e
diff --git a/laya/agent.py b/laya/agent.py
index 5ca3861c..ff0b33d3 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -628,7 +628,7 @@ class Agent(HookRegistry):
raise ValueError("question %r: definition must be a dict, got %s"
% (qid, type(qdef).__name__))
t = qdef.get("type")
- if t not in QTYPES:
+ if not isinstance(t, str) or t not in QTYPES:
raise ValueError("question %r: unknown type %r; use one of %s" % (qid, t, sorted(QTYPES)))
if "instructions" not in qdef:
raise ValueError("question %r: no 'instructions'; add the text the model should answer" % (qid,))
diff --git a/tests/test_criteria.py b/tests/test_criteria.py
index 445f51c2..6a4a5be0 100644
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@ -407,7 +407,11 @@ if _empty_label is not None:
# the same questions through the public entry point, not only the method under it
router = Router()
router.attach("english", agent)
-for label, qdef in (("choice without criteria", {"type": "choice", "instructions": "x"}),):
+for label, qdef in (
+ ("choice without criteria", {"type": "choice", "instructions": "x"}),
+ ("list type", {"type": [], "instructions": "Is it spam?"}),
+ ("dict type", {"type": {}, "instructions": "Is it spam?"}),
+):
try:
router.predict(STATE, {"q": qdef}, model="english")
FAIL.append("rejected/router %s: no error raised" % label)
@@ -426,6 +430,22 @@ except ValueError as e:
except Exception as e:
FAIL.append("rejected/second question: %s instead of ValueError: %s" % (type(e).__name__, e))
+# A malformed type must name the question and the allowed values, including when it is second.
+# Lists and dicts used to fail in the type lookup with a bare "unhashable type" TypeError (#707).
+for bad_type in ([], ["noul"], {}, {"name": "noul"}, None, 7, True, "bogus"):
+ name = "rejected/question type %r" % (bad_type,)
+ try:
+ agent.system_one(STATE, {
+ "ok": {"type": "noul", "instructions": "Is it urgent?"},
+ "refund": {"type": bad_type, "instructions": "Is a refund requested?"},
+ })
+ FAIL.append("%s: no error raised" % name)
+ except ValueError as e:
+ check(name, str(e), "question 'refund': unknown type %r; use one of ['choice', 'noul', 'score']"
+ % (bad_type,))
+ except Exception as e:
+ FAIL.append("%s: %s instead of ValueError: %s" % (name, type(e).__name__, e))
+
# ...and the shapes that are valid still answer, so this is not validation-only coverage
GOOD = {
"choice": {"type": "choice", "instructions": "Which team?",
diff --git a/tests/test_onnx_batch.py b/tests/test_onnx_batch.py
index 77686ec6..ffd6541b 100644
--- a/tests/test_onnx_batch.py
+++ b/tests/test_onnx_batch.py
@@ -175,6 +175,18 @@ check_raises("empty/bare dict rejected", TypeError,
lambda: _bare_onnx().predict_batch({"body": "x"}, QUESTIONS))
check_raises("empty/invalid question rejected before any run", ValueError,
lambda: _bare_onnx().predict_batch(STATES, {"bad": {"type": "nope", "instructions": "?"}}))
+for bad_type in ([], {}):
+ invalid = _bare_onnx()
+ name = "invalid/question type %r" % (bad_type,)
+ try:
+ invalid.predict_batch(STATES, {"refund": {"type": bad_type, "instructions": "Refund requested?"}})
+ FAIL.append("%s: no error raised" % name)
+ except ValueError as e:
+ check(name, str(e), "question 'refund': unknown type %r; use one of ['choice', 'noul', 'score']"
+ % (bad_type,))
+ except Exception as e:
+ FAIL.append("%s: %s instead of ValueError: %s" % (name, type(e).__name__, e))
+ check("%s does not run the session" % name, invalid.session.calls, [])
check("budget/max_len and head_max_len forward through the batch path",
_bare_onnx().predict_batch(STATES, QUESTIONS, max_len=32, head_max_len=16),
[_bare_onnx().system_one(s, QUESTIONS, max_len=32, head_max_len=16) for s in STATES])
diff --git a/tests/test_serve.py b/tests/test_serve.py
index dab066ab..beed5d2d 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -684,6 +684,20 @@ class ValidatingRouter:
raise AssertionError("validation should have rejected this before predict()")
[email protected]("bad_type", [[], {}], ids=["list", "dict"])
+def test_malformed_question_type_is_a_named_422(monkeypatch, bad_type):
+ monkeypatch.delenv("LAYA_API_KEY", raising=False)
+ client = TestClient(create_app(router=ValidatingRouter()), raise_server_exceptions=False)
+ body = dict(REQ)
+ body["questions"] = {"refund": {"type": bad_type, "instructions": "Is a refund requested?"}}
+
+ response = client.post("/v1/systemone", json=body)
+
+ assert response.status_code == 422, response.text
+ assert response.json()["detail"] == (
+ "question 'refund': unknown type %r; use one of ['choice', 'noul', 'score']" % (bad_type,))
+
+
def test_a_nested_choice_label_is_a_caller_error_not_a_server_fault(monkeypatch):
"""A `criteria` list containing a list/dict label is the caller's mistake, so it must be 422.