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(structured): a non-dict answer is omitted from the projection, not fatal

Aashishced37d864e79
diff --git a/laya/structured.py b/laya/structured.py
index 407c5b33..b8899eb7 100644
--- a/laya/structured.py
+++ b/laya/structured.py
@@ -274,7 +274,11 @@ def _project(answers: Dict[str, Any], fields: Sequence[_Field]) -> Dict[str, Any
values: Dict[str, Any] = {}
for f in fields:
answer = answers.get(f.name)
- if answer is None:
+ # A field whose answer is not an answer object carries nothing to project -- exactly the
+ # absent field the `None` branch already omits. `apply_confidence_gate` makes the same call
+ # and skips a non-dict answer (confidence.py:298); reading `.get` off one instead raised a
+ # raw AttributeError here and crashed the whole projection.
+ if not isinstance(answer, dict):
continue
if answer.get("low_confidence"):
values[f.name] = None
@@ -316,6 +320,10 @@ def _details(values: Dict[str, Any], answers: Dict[str, Any], result: Dict[str,
answer_confidence: Dict[str, Optional[float]] = {}
probabilities: Dict[str, Dict[str, Any]] = {}
for name, answer in answers.items():
+ if not isinstance(answer, dict):
+ # Same rule as `_project` and `apply_confidence_gate`: a non-dict answer is no answer,
+ # so it reports no confidence rather than raising AttributeError on `.get`.
+ continue
confidence[name] = float(answer.get("confidence", 0.0))
# Read through the module that owns the definition, so this and the `min_confidence` gate
# cannot disagree about which quantity is being reported.
diff --git a/tests/test_structured.py b/tests/test_structured.py
index a9fe94bb..93371b52 100644
--- a/tests/test_structured.py
+++ b/tests/test_structured.py
@@ -127,6 +127,19 @@ for score, want in ((0, 3), (2.4, 5), (4, 7)):
got = answers_to_json({"x": {"type": "score", "score": score}}, SEVERITY_SCHEMA)["x"]
check("project/score with no probabilities honours minimum (score=%r)" % score, got, want)
+# A runner that reports a field as something other than an answer object -- a bare string, number
+# or list -- gives nothing to project. `apply_confidence_gate` already skips a non-dict answer
+# (confidence.py:298); the projection read `.get` off one and raised AttributeError, crashing the
+# whole result. It now omits the field, exactly as an absent answer does.
+malformed = dict(ANSWERS, department="oops", urgency=None, needs_human=["not", "an", "answer"])
+partial = answers_to_json(malformed, SCHEMA)
+check_true("project/non-dict answers are omitted, not fatal",
+ "department" not in partial and "urgency" not in partial
+ and "needs_human" not in partial)
+check("project/valid fields still project beside a non-dict one", partial["priority"], 2)
+check("project/all answers non-dict yields no values",
+ answers_to_json({"department": 5, "urgency": "x"}, SCHEMA), {})
+
# --------------------------------------------------------------- rejections
def _bad(schema):
@@ -217,6 +230,21 @@ check("decide/details noul probabilities", details.probabilities["needs_human"],
check("decide/details usage", details.usage, {"input_tokens": 1, "output_tokens": 0})
check("decide/details routing", details.routing, {"model": "english"})
+# The same non-dict answer reaches decide through a runner, not only through answers_to_json:
+# `decide` projects onto the schema and builds the DecisionResult from the very same answer objects
+# the confidence gate walks. Before the fix both `decide` and `return_details` raised AttributeError
+# on a field whose answer was not a dict.
+mixed_answers = dict(ANSWERS, department="oops")
+runner = FakeRunner(mixed_answers)
+out = decide(runner, "some state", schema=SCHEMA)
+check_true("decide/non-dict answer omits the field, rest intact",
+ "department" not in out and out["urgency"] == 2 and out["needs_human"] is True)
+details = decide(FakeRunner(mixed_answers), "some state", schema=SCHEMA, return_details=True)
+check_true("decide/details a non-dict answer reports no confidence",
+ "department" not in details.confidence and "department" not in details.probabilities)
+check("decide/details valid answers still report confidence beside it",
+ details.confidence["needs_human"], 0.8)
+
runner = FakeRunner({"a": {"type": "noul", "noul": 0.9, "confidence": 0.9}})
out = decide(runner, "s", questions={"a": {"type": "noul", "instructions": "?"}})
# The pass-through is the raw answer, and no `min_confidence` was passed, so it comes back exactly