Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion laya/structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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.
Expand Down
28 changes: 28 additions & 0 deletions tests/test_structured.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,19 @@ def check_raises(name, exc, fn):
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):
Expand Down Expand Up @@ -217,6 +230,21 @@ def predict(self, state, questions, **kwargs):
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
Expand Down
Loading