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 #716 from Swatantra-66/fix/input-validation-error-handling

Nandakishor626bedbe87ee
diff --cc tests/test_criteria.py
index 6a4a5be0,20ddd1ab..e2a42f8d
--- a/tests/test_criteria.py
+++ b/tests/test_criteria.py
@@@ -430,22 -440,17 +440,33 @@@ 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))
+
+ # every question id is validated (must be non-empty string)
+ for bad_qid in (None, "", " "):
+ name = "rejected/question id %r" % (bad_qid,)
+ try:
+ agent.system_one(STATE, {bad_qid: {"type": "noul", "instructions": "Is it urgent?"}})
+ FAIL.append("%s: no error raised" % name)
+ except ValueError as e:
+ check_true("%s names question id" % name, "question id" in str(e), str(e))
+ 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 --cc tests/test_onnx_batch.py
index ffd6541b,ef80e508..a9f6cedf
--- a/tests/test_onnx_batch.py
+++ b/tests/test_onnx_batch.py
@@@ -176,17 -176,15 +176,24 @@@ check_raises("empty/bare dict rejected"
check_raises("empty/invalid question rejected before any run", ValueError,
lambda: _bare_onnx().predict_batch(STATES, {"bad": {"type": "nope", "instructions": "?"}}))
for bad_type in ([], {}):
- check_raises("invalid/question type %r rejected" % (bad_type,), ValueError,
- lambda bt=bad_type: _bare_onnx().predict_batch(STATES, {"q": {"type": bt, "instructions": "?"}}))
+ 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, [])
+ for bad_ins in (None, "", " ", []):
+ check_raises("invalid/instructions %r rejected" % (bad_ins,), ValueError,
+ lambda bi=bad_ins: _bare_onnx().predict_batch(STATES, {"q": {"type": "noul", "instructions": bi}}))
+ check_raises("invalid/questions not a dict", TypeError,
+ lambda: _bare_onnx().predict_batch(STATES, ["not", "a", "dict"]))
+ check_raises("invalid/state is None in batch", TypeError,
+ lambda: _bare_onnx().predict_batch([None], QUESTIONS))
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 --cc tests/test_serve.py
index 8764d39f,67a4a19f..093ef30f
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@@ -690,14 -690,33 +690,36 @@@ def test_malformed_question_type_is_a_n
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 "unknown type" in response.text, response.text
+ assert response.json()["detail"] == (
+ "question 'refund': unknown type %r; use one of ['choice', 'noul', 'score']" % (bad_type,))
+ @pytest.mark.parametrize("bad_ins", [None, "", " ", []])
+ def test_malformed_instructions_is_a_named_422(monkeypatch, bad_ins):
+ monkeypatch.delenv("LAYA_API_KEY", raising=False)
+ client = TestClient(create_app(router=ValidatingRouter()), raise_server_exceptions=False)
+ body = dict(REQ)
+ body["questions"] = {"refund": {"type": "noul", "instructions": bad_ins}}
+ response = client.post("/v1/systemone", json=body)
+ assert response.status_code == 422, response.text
+ assert "instructions" in response.text, response.text
+
+
+ @pytest.mark.parametrize("bad_qid", ["", " "])
+ def test_malformed_question_id_is_a_named_422(monkeypatch, bad_qid):
+ monkeypatch.delenv("LAYA_API_KEY", raising=False)
+ client = TestClient(create_app(router=ValidatingRouter()), raise_server_exceptions=False)
+ body = dict(REQ)
+ body["questions"] = {bad_qid: {"type": "noul", "instructions": "Is it urgent?"}}
+ response = client.post("/v1/systemone", json=body)
+ assert response.status_code == 422, response.text
+ assert "question id" in response.text, response.text
+
+
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.