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 nested choice label is a named caller error, not a bare TypeError

PerryLinkea08172303a8
diff --git a/laya/agent.py b/laya/agent.py
index a76041f7..1480929f 100644
--- a/laya/agent.py
+++ b/laya/agent.py
@@ -499,6 +499,17 @@ 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.
+ for i, label in enumerate(crit if isinstance(crit, list) else crit.keys()):
+ if isinstance(label, (list, dict, set, bytearray)):
+ 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))
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_serve.py b/tests/test_serve.py
index f2b80abb..ef4d4459 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -388,6 +388,42 @@ def test_validation_errors_are_not_logged_as_failures(monkeypatch, caplog):
assert not [r for r in caplog.records if r.levelno >= logging.ERROR], caplog.records
+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.
+
+ It used to raise `TypeError: unhashable type: 'list'` from `_to_internal`, three frames below
+ `_check_question`, which names neither the question nor the label -- and `serve` maps only
+ `ValueError` to 422, so the caller got a 500 "inference failed" with the reason discarded.
+ `ValueError` is what carries the message to the client, so the guard has to raise that type.
+ """
+ class ValidatingRouter:
+ """The real guard, without a checkpoint: what `Agent.system_one` runs before encoding.
+
+ The app does not validate `criteria` itself -- the agent does -- so the stub calls the
+ same guard `system_one` calls, and any `ValueError` it raises is what `serve` has to map
+ to 422. `predict` still fails loudly if the guard lets something through.
+ """
+
+ loaded = ["english"]
+
+ def predict(self, state, questions, model=None):
+ from laya.agent import Agent
+ for qid, qdef in questions.items():
+ Agent._check_question(qid, qdef)
+ raise AssertionError("validation should have rejected this before predict()")
+
+ monkeypatch.delenv("LAYA_API_KEY", raising=False)
+ client = TestClient(create_app(router=ValidatingRouter()), raise_server_exceptions=False)
+
+ for label in (["billing"], {"billing": "x"}):
+ body = dict(REQ)
+ body["questions"] = {"dept": {"type": "choice", "instructions": "Which team?",
+ "criteria": [label, "tech"]}}
+ response = client.post("/v1/systemone", json=body)
+ assert response.status_code == 422, (label, response.status_code, response.text)
+ assert "choice label 0" in response.text, response.text
+
+
def test_inference_timing_headers():
"""POST /v1/systemone returns Server-Timing and X-Inference-Time-Ms headers."""
router = FakeRouter()