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(serve): refuse unhashable tournament choice labels with a 422, not a 500

Aashish898d50f60b9a
diff --git a/laya/serve.py b/laya/serve.py
index c993c5f7..a853ab1d 100644
--- a/laya/serve.py
+++ b/laya/serve.py
@@ -292,7 +292,16 @@ def _tournament_plan(questions: Any, group: int) -> Optional[Dict[str, List[str]
continue
criteria = question.get("criteria")
if isinstance(criteria, (dict, list)) and len(criteria) > group:
- large[qid] = [label for label, _ in _criteria_items(criteria)]
+ # `_criteria_items` de-duplicates its labels through a set, so an unhashable label -- a
+ # `criteria` written as a list of JSON arrays/objects -- raises a bare `TypeError` here.
+ # This plan runs before `router.predict`, so core's `Agent._check_question` (which refuses
+ # a non-scalar label with a `ValueError` naming the question) has not seen it yet, and the
+ # `TypeError` fell through the endpoint's catch-all to a 500. Refuse it as the same
+ # `ValueError` the sibling duplicate/empty-label paths already raise, so it is a 422.
+ try:
+ large[qid] = [label for label, _ in _criteria_items(criteria)]
+ except TypeError as error:
+ raise ValueError("question %r: %s" % (qid, error)) from None
if not large:
return None
unmappable = [qid for qid, labels in large.items() if not all(isinstance(label, str) for label in labels)]
diff --git a/tests/test_serve.py b/tests/test_serve.py
index 2e8d3b6b..637f0cce 100644
--- a/tests/test_serve.py
+++ b/tests/test_serve.py
@@ -376,6 +376,32 @@ def test_large_choice_is_refused_without_a_tournament(monkeypatch):
assert fake.calls == []
+def test_tournament_refuses_unhashable_choice_labels_as_a_422(monkeypatch):
+ # A large choice is narrowed by elimination, and `_tournament_plan` reads the criteria labels
+ # (deduping them through a set) BEFORE `router.predict`, so core's `_check_question` -- which
+ # refuses a non-scalar label with a ValueError naming the question, as the no-tournament group=None
+ # case below already shows -- has not run yet. A `criteria` written as a list of JSON arrays used to
+ # leak the set-dedup `TypeError` through the endpoint's catch-all and answer 500 "inference failed".
+ client, fake = _tournament_client(monkeypatch, "2")
+ req = {"state": "I was charged twice, please refund one.",
+ "questions": {"queue": {"type": "choice", "instructions": "Which queue?",
+ "criteria": [["a", 1], ["b", 2], ["c", 3]]}}}
+ r = client.post("/v1/systemone", json=req)
+ assert r.status_code == 422, r.text
+ assert "queue" in r.json()["detail"]
+ assert fake.calls == [] # refused while planning, before any prediction
+
+
+def test_tournament_plan_names_the_question_on_unhashable_labels():
+ from laya import serve
+ with pytest.raises(ValueError, match="queue"):
+ serve._tournament_plan({"queue": {"type": "choice",
+ "criteria": [["a", 1], ["b", 2], ["c", 3]]}}, 2)
+ # Positive control: string-labelled criteria still plan (the guard is not over-strict).
+ assert serve._tournament_plan(
+ {"queue": {"type": "choice", "criteria": ["a", "b", "c"]}}, 2) == {"queue": ["a", "b", "c"]}
+
+
def test_tournament_answers_a_large_choice_in_the_jev_shape(monkeypatch):
client, fake = _tournament_client(monkeypatch, "16")
r = client.post("/v1/systemone", json=_large_req(128))