Skip to content

fix(structured): a non-dict answer is omitted from the projection, not fatal - #1368

Merged
NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-structured-non-dict-answer
Oct 25, 2026
Merged

NandhaKishorM merged 1 commit into
NandhaKishorM:mainfrom
aashish254:fix-structured-non-dict-answer

Conversation

@aashish254

Copy link
Copy Markdown
Contributor

What

laya/structured.py::_project and ::_details call answer.get(...) on every value in answers. When a field's answer is not an object -- a runner (or a cached/replayed decision) that reports "department": "oops" or a list instead of {"type": ..., "choice": ...} -- that raised a raw AttributeError: 'str' object has no attribute 'get' and crashed the entire projection, taking down the schema fields that were well-formed.

structured.answers_to_json({"a": "oops", "b": {"type": "score", "score": 1}}, schema)
  -> AttributeError: 'str' object has no attribute 'get'   # _project, structured.py:279

The same happens through the public entry points: decide(runner, state, schema=...), decide(..., return_details=True), and answer_to_pydantic.

Why

apply_confidence_gate walks those exact same answer objects on the same decide code path (structured.py:370, two lines before _project runs) and deliberately skips a non-dict answer -- if not isinstance(a, dict): continue (confidence.py:298). So the module already treats a non-dict answer as "no answer". Only the projection disagreed: it crashed where its sibling skips. _project already omits a field whose answer is absent (answer is None: continue); this extends that same omission to a non-dict answer, so projection and gate agree and one corrupt field no longer sinks the whole result. Well-formed answers project byte-for-byte unchanged (isinstance(answer, dict) is true for all of them).

How it was verified

python -c "from laya import structured as s; \
  s.answers_to_json({'a':'oops','b':{'type':'score','score':1}}, \
  {'type':'object','properties':{'a':{'type':'string','enum':['x']},'b':{'type':'integer','minimum':0,'maximum':2}}})"
# base -> AttributeError: 'str' object has no attribute 'get'   |   fixed -> {'b': 1}
# (same for decide(...), decide(..., return_details=True), and a list-valued answer)

python tests/test_structured.py     # 196 -> 202 passed, 0 failed
python tests/test_structured_docs.py  # 117 passed (no new error message added)
python tests/test_hooks_api.py        # 685 passed (public API unchanged)
# mutation arm: base structured.py + the six new tests abort with AttributeError; restoring -> 202 passed.
ruff check laya/structured.py tests/test_structured.py --select=E9,F63,F7,F82,F401,F811 --line-length=120
python -m compileall -q laya/structured.py tests/test_structured.py

Cases go into the already-registered tests/test_structured.py: three in the projection section (non-dict answers omitted, valid fields still project, all-non-dict yields {}) and three after the decide/details block (scalar answer through decide and through return_details).

Checklist

  • Focused on one change
  • Rebased on latest main
  • Tests pass locally
  • Docs updated when public API changed (no signature change; test_hooks_api.py still green — only the malformed-input behaviour moves from crash to omission, matching the gate)

…t fatal

decide() and answers_to_json() read .get off every answer, so a field whose
answer was not an object -- a bare string/number/list -- raised AttributeError
and crashed the whole result. apply_confidence_gate already skips a non-dict
answer (confidence.py:298); _project and _details now make the same call and
omit the field, exactly as an absent (None) answer already does.
@NandhaKishorM
NandhaKishorM merged commit a732767 into NandhaKishorM:main Oct 25, 2026
51 of 52 checks passed
NandhaKishorM added a commit that referenced this pull request Oct 25, 2026
8 PRs. Rust and .NET batch prediction and router batch APIs (#1358,
#1359), the min_confidence abstention gate and jev_confidence in both
ports (#1362, #1363), and four inputs refused where they enter instead
of failing later (#1365, #1366, #1367, #1368).

Verified before tagging: all 92 suites pass, 612 tests. ruff clean on
both lists. Java fixture, Unicode table and language table gates clean;
three Go tables_gen.go match. Version agrees across pyproject.toml,
laya.__version__ and nix/package.nix. README carries 0.4.16 only.

Security audit of the merge diff, 180330 added lines over 349 files:
zero dynamic exec, zero network calls, zero URLs, zero base64 or
obfuscation, zero secrets in the diff or the tracked tree, zero
laya-max boundary hits, no writes outside the test temp dir, the
dependency surface untouched, no npm install hooks, no binaries, every
new file 100644, and 20 of 20 actions SHA-pinned with codeql-action
init, autobuild and analyze all on 4.39.1.

Held from this release: #1364 (correct in Python but it moves a
cross-port contract Decisions.java reproduces deliberately and
structured.ts shares), #1324 (real table regression), and seven
dependency and action bumps for a pass of their own.

Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
@NandhaKishorM

Copy link
Copy Markdown
Owner

Merged, in 0.4.16.

The argument that settles this is that apply_confidence_gate already skips a non-dict answer, so the projection raising AttributeError on the same input was the two paths disagreeing about what an answer is, not a missing feature. Folding answer is None into not isinstance(answer, dict) is the right generalisation, because a non-dict carries nothing to project and that is precisely the absent field the None branch was already omitting.

Fixing _details in the same PR is what makes it complete rather than half done. _project alone would have moved the crash a few lines later into the confidence and probabilities loop, on the same input, and the next person would have read it as a new bug.

Citing confidence.py:298 in the comment is the one thing I would change, and it is not worth another round trip: a line number goes stale the moment anything is inserted above it, and I have had to repair exactly that kind of citation in this repo this week. The symbol name alone would have held.

NandhaKishorM pushed a commit that referenced this pull request Oct 29, 2026
…the oracle omits it

laya/structured.py skips an answer that is not an object (isinstance(answer, dict), #1368);
the port only skipped null/undefined, so a string, number, boolean or array under a field
name was read field-by-field and invented a value -- "undefined" for a choice, minimum+0 for
a score, false for a noul, plus confidence 0.0 / answerConfidence null / probabilities {} in
the details. Measured across 4 fields x 6 shapes: 40 of 48 rows disagreed with Python, 0 after.

Arrays need their own check because typeof reports them as "object".
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants