Skip to content

fix(707): raise for a decision property whose type is not its kind's - #720

Open
Markgugg wants to merge 1 commit into
awslabs:devfrom
Markgugg:fix/707-decision-field-type-required
Open

Markgugg wants to merge 1 commit into
awslabs:devfrom
Markgugg:fix/707-decision-field-type-required

Conversation

@Markgugg

@Markgugg Markgugg commented Dec 2, 2026 •

Copy link
Copy Markdown

Pull Request

Issue Reference

Refs #707 (the required half stays open for #724)

Description of Changes

from_json_schema() ignored the type of a property with x-aws-stickler-decision. An integer-typed choice came back as Optional[str] with no warning. It now raises a ValueError that names the field, the same way a conflicting comparator or threshold is already rejected.

required is not touched. A required decision property imports as it does on dev, including the ["string", "null"] plus required that to_json_schema() writes for a field declared with default=....

Changes Made

  • _decision_field raises when the property doesn't import as its kind's answer type (string for choice, integer for score, boolean for noul, alone or with null). It reads the parsed field, so $ref and nested properties are checked too. An enum, a const or a format that imports as another type (such as date-time) is rejected as well; a format that imports as a string (such as email) is not. The error only mentions enum/const/format when the property imported as something other than a plain type.
  • One sentence in the decision-models guide, after the existing hand-written config paragraph, saying the same rule applies to a JSON Schema property.
  • A Changed entry under [Unreleased].

Rebased on current dev (9680269) as one commit.

Why These Changes Are Needed

The schema said one thing and the model did another, with no warning.

Is it breaking?

I couldn't find a docs line that made a wrong type on a JSON Schema decision property invalid. The closest is in the Evaluating Decision Models guide, and it only covers a hand-written Stickler config:

In a hand-written config, decision stands in for a comparator, the field's type must be its answer type (str for Choice, int for Score, bool for Noul), ...

model_from_json() already enforces that line, and from_json_schema() builds the same decision fields, so I read a JSON Schema property with another type as input the docs never allowed. Under RELEASING.md that makes the entry a Fixed with an "Upgrade note:" and the line above quoted in it. I also added a sentence to the guide saying the same rule applies to a JSON Schema property. Only hand-written schemas can hit it; to_json_schema() output always imports.

If you read that line as covering only the config path, I can switch this to a warning now and a raise in a later minor release instead.

Testing

  • Unit tests added/updated
  • Integration tests added/updated
  • Manual testing completed
  • All existing tests pass

New tests/decisions/test_schema_import.py (32 tests) covers:

  • a wrong type for each kind, bare and with null
  • required for each kind, bare and with null, which imports
  • a nested path (items[].q)
  • enum, const, format: date and format: date-time on a choice are rejected; format: email imports
  • a $ref to a wrong-type definition is rejected
  • the matching types
  • a to_json_schema() round trip with and without description and weight
  • a round trip of a required decision field (Choice(..., default=...)), which exports as ["string", "null"] plus required and imports again

16 of the 32 fail on dev and all pass here. Full suite with --extra llm --extra bert: 7511 passed, 3 skipped, 1 xfailed. ruff check . is clean.

Checklist

  • Code follows the project's coding standards
  • Self-review of code completed
  • Documentation updated (if applicable)
  • CHANGELOG.md entry added under [Unreleased], or not needed because
    nothing user-visible changed (see RELEASING.md)
  • Tests added/updated for new functionality
  • All CI checks are passing
  • Ready for review

Additional Notes

  • One thing I noticed but left alone: from_json_schema() rebuilds a required decision field as not required (default = None if field_info.is_required() ...), while model_from_json() keeps it required. The new test checks that it imports and round-trips, not which way required goes. Happy to make the schema path keep it required here or in a follow-up if you want them to match.
  • An AI assistant (Claude Code) helped write this; I reviewed the change and the tests myself.

@Markgugg
Markgugg force-pushed the fix/707-decision-field-type-required branch from f7507fe to 303c786 Compare December 2, 2026 07:55

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

needs changes

Two blockers and one medium. I ran the repro for blocker 1 on the PR head (303c786).

Blockers

1. The required check makes the library's own to_json_schema() output unimportable.
json_schema_importer.py L758-L763 raises for any decision property listed in required. The PR body says exports never put decision fields in required. That is false for a decision field declared with default=..., which dev already tests (tests/decisions/test_config_round_trip.py::test_round_trip_keeps_a_required_decision_field).

class M(StructuredModel):
    q: Optional[str] = Choice(["a", "b"], default=...)
    note: str
s = M.to_json_schema()
# s["required"] == ['q', 'note'];  s["properties"]["q"]["type"] == ['string', 'null']
StructuredModel.from_json_schema(s)
  • PR head: ValueError: Field 'q' is a choice decision field, which is None when its question was not asked; remove it from 'required'
  • origin/dev (3dc3aec): imports, and the rebuilt field has is_required() == False.

The message is also wrong here. A required field may be None; "required" does not mean "question asked". ["string","null"] plus required is the usual JSON Schema spelling of "must be present, may be null".

What to change: keep the field required and nullable, or drop the check. Do not raise. Add a round-trip test for a required decision field.

2. The branch is stale against dev and conflicts with dev's required semantics.
git merge-tree against origin/dev (3dc3aec) conflicts in three files: CHANGELOG.md, docs/docs/Guides/Evaluation/decision-models.md and json_schema_importer.py.

  • dev now computes default = None if field_info.is_required() else field_info.default and calls _decision_default(spec, default, field_path) (the fix(703) work). This PR's required check contradicts it.
  • dev's docs now say a decision field's required "works as it does on any field". This PR's text says it "must not be in required".

What to change: rebase on dev, and settle the required behaviour against blocker 1 and #703. A hand-merge of the type check onto dev passes the full suite (7501 passed, 3 skipped, 1 xfailed), but no existing test covers blocker 1.

Medium

3. The CHANGELOG entry sits in a released section.
On dev, ## [Unreleased] is empty and ## [1.1.0] - 2026-12-04 is dated, so "still unreleased" in the PR body is no longer true. After 1.1.0, rejecting a required property, a wrong type, or a choice with enum/format/const is a change to input that 1.1.0 accepted. RELEASING.md requires a PR that relies on "the docs never allowed it" to cite the docs line that made the input invalid, in the PR and in the CHANGELOG entry. This PR cites none. Move the entry under [Unreleased] as Changed or Fixed and add the citation (or settle blocker 1 so the required part goes away).

4. Test gap: test_required_is_rejected never uses a nullable type.
tests/decisions/test_schema_import.py L47-L54 only passes the bare json_type. Mutation: changing the check to if field_info.is_required() and not unwrap_optional(field_info.annotation)[1]: left all 22 tests passing. Add the [type, "null"] plus required case; it is the case blocker 1 hits.

Verified

  • 22 new tests pass on the PR head. On dev source with the new test file copied in, 14 fail and 8 pass, as the PR body says.
  • Full suite on the PR head with all extras installed: 7269 passed, 3 skipped, 1 xfailed (the PR body's 13 skips pass once the extras are installed).
  • 10 of 11 mutations of the type and required checks are killed by the new tests. The survivor is the one in item 4.
  • Still accepted as before: string, ["string","null"], ["null","string"], anyOf/oneOf with null, nullable: true, length and pattern limits, default: null, $ref to a plain string, ["boolean","null"] for noul.
  • Rejected as intended: wrong type for each kind, multi-type unions, type: null only, array/object, enum/const/format, $ref to an integer or enum. Error messages name nested paths (o.q, o[].q).
  • Non-decision required properties are unchanged.

@Markgugg
Markgugg force-pushed the fix/707-decision-field-type-required branch from 303c786 to ab8c4bf Compare December 4, 2026 19:11
@Markgugg

Markgugg commented Dec 4, 2026

Copy link
Copy Markdown
Author

Thanks for the careful review and the repro. The branch is now one commit rebased on dev.

  1. I dropped the required check completely. Your M example imports again, and there's a new round-trip test for a Choice(..., default=...) field, which exports as ["string", "null"] plus required. I also replaced test_required_is_rejected with a test that required imports for all three kinds.

  2. I rebased onto dev (9680269). The importer now keeps dev's _decision_default path untouched and only adds the type check. The docs keep "required works as it does on any field", and I took out my "must not be in required" text. The only new docs sentence says the answer-type rule also applies to a JSON Schema property. Full suite: 7507 passed, 3 skipped, 1 xfailed.

  3. The entry is now under [Unreleased] as Fixed, with an Upgrade note. The docs line I cite is the one in the decision-models guide: "the field's type must be its answer type (str for Choice, int for Score, bool for Noul)". It's written for a hand-written config, so I also added a sentence saying the same applies to a JSON Schema property, and the entry says so. If you read that line as not covering JSON Schema, and would rather this warn in this release and raise in a later one, I'm happy to switch it.

  4. The nullable-plus-required case is now tested for each kind, bare and with null, and it imports. The mutation you found no longer applies since the check is gone, but restoring the old required raise in either form now fails those tests.

One more thing I noticed: from_json_schema() rebuilds a required decision field as not required, while model_from_json() keeps it required. I left that alone. Let me know if you want it matched here or in a separate PR.

@vawsgit vawsgit left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

✅ LGTM!

All four findings from my review of 303c786 are fixed at ab8c4bf. No medium or high findings remain.

Previous findings

  1. required check (fixed). The raise is gone. The diff adds only the type check at json_schema_importer.py L751-L757 and keeps dev's _decision_default / is_required() path. My repro (q: Optional[str] = Choice(["a","b"], default=...) plus note: str) now exports required == ['q', 'note'], q.type == ['string', 'null'], imports, and the rebuilt field has is_required() == False, the same as dev. Before: ValueError ... remove it from 'required'.
  2. Stale branch (fixed). One commit on origin/dev (9680269). git merge-tree --write-tree origin/dev pr720 is clean. The docs keep dev's "required works as it does on any field".
  3. CHANGELOG (fixed). The entry is under [Unreleased] / ### Fixed with an "Upgrade note:". The quoted docs line exists at base (decision-models.md:48) and the quote is exact.
  4. Nullable plus required test gap (fixed). test_required_imports and test_exported_required_decision_field_round_trips cover each kind, bare and with null. Restoring the old required raise, in either the plain or the nullable-exempt form, fails them.

Verified

  • tests/decisions/test_schema_import.py: 28 pass at the PR head. With the same file on dev source, 13 fail and 15 pass, as the PR body says. tests/decisions as a whole: 239 passed.
  • Full suite without the llm/bert extras (not installable on this host): 7490 passed, 13 skipped (missing extras), 1 xfailed. The PR body reports 7507 passed with extras.
  • 12 of 12 meaningful mutations of the type check are killed (skip when nullable, skip nested, skip per kind, let enum/const/format through, accept float or bool for score, accept a bool subclass for noul, remove the check). The one survivor was equivalent.
  • 33 property shapes compared on dev and the PR head: anyOf/oneOf with null, nullable: true, ["null", T], default: null, length/pattern/range limits, $ref and anyOf[$ref, null] to a plain type. All behave the same on both. Wrong types now raise with the field path in the message (items[].q).

Non-blocking, optional

  • The message always says "no enum, const or format", even for a plain wrong type, and format: email/hostname on a choice are still accepted.
  • There is no test for a $ref to a wrong-type definition. The same check covers it.
  • One judgement call for the maintainers: the cited docs line is written for a hand-written config. The PR adds a docs sentence for JSON Schema, and the CHANGELOG entry says so openly. The author offered warn-first as an alternative. stickler.decisions is marked experimental.

@sromoam sromoam left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @Markgugg, this fixes all four of vawsgit's points, and dropping the required raise rather than reshaping it was the right call. At head ab8c4bf (one commit on dev 9680269d, so the merge is a fast-forward), tests/decisions/test_schema_import.py gives 28 passed. With the importer back at dev it gives 13 failed, 15 passed. Full suite with all extras: 7507 passed, 3 skipped, 1 xfailed. The entry lands under ## [Unreleased] / ### Fixed. On the judgement call, raising in 1.1.1 is fine and there's no need to warn first. The guide gives a decision field one answer type, model_from_json() already refuses any other, and 1.1.0 ignored the schema's type instead of honouring it. RELEASING.md counts that as input misread, so the citation holds.

Must fix

  1. The guide, the CHANGELOG and the error say a format raises, and most don't (decision-models.md:54, CHANGELOG.md:23, json_schema_importer.py:757). The check compares the imported Python type, so it only refuses a format that imports as something other than str. On this head date, time, date-time, uri and uuid raise, while email, hostname, ipv4, duration and password import as Optional[str]. Either refuse any format on a decision property, or say what the check does. For example: "its type must import as its answer type (string, integer or boolean, alone or with null), so an enum, a const or a format such as date-time raises ValueError". The error text should match, and for a plain wrong type it can leave out the enum/const/format clause.

Smaller

  1. Title and closing keyword. The title still says "or required", and it becomes the squash commit, so please retitle it to match the commit ("raise for a decision property whose type is not its kind's"). Also, Fixes #707 would close the required half of #707, which this rightly leaves alone. To your question: making from_json_schema() match model_from_json() on required is the follow-up I left in #724, so it belongs in a separate PR. Please change it to Refs #707 and I'll keep #707 open for that.
  2. Optional: a $ref to a wrong-type definition raises here too, but no test pins it.

@sromoam sromoam added the anytime Non-breaking improvement off the release critical path: merge whenever it's ready label Dec 4, 2026
from_json_schema() rebuilt every x-aws-stickler-decision property as an
optional field of its kind's type, whatever the property's own type. An
integer-typed choice came back as Optional[str], with no warning.

_decision_field now raises ValueError naming the field when the property
does not import as its kind's answer type (string for choice, integer for
score, boolean for noul, alone or with null). It reads the parsed field,
so a $ref or nested property is checked the same way, and an enum, a
const or a format that imports as another type (such as date-time) is
rejected too. A format that imports as a string, such as email, is not.

required is left as dev reads it: a required decision property, including
the ["<type>", "null"] plus required that to_json_schema() writes for a
field declared with default=..., still imports.

Refs awslabs#707
@Markgugg Markgugg changed the title fix(707): raise for a decision property's mismatched type or required fix(707): raise for a decision property whose type is not its kind's Dec 5, 2026
@Markgugg
Markgugg force-pushed the fix/707-decision-field-type-required branch from ab8c4bf to 621896a Compare December 5, 2026 01:18
@Markgugg

Markgugg commented Dec 5, 2026

Copy link
Copy Markdown
Author

Thanks @sromoam, and thanks for settling the release question. Pushed as one commit on dev (621896a):

  • format wording. I went with describing what the check does rather than refusing every format. The guide, the CHANGELOG and the error now say the property must import as its answer type, so an enum, a const or a format such as date-time raises. The error only adds that clause when the property imported as something other than a plain type; a plain wrong type (e.g. integer on a choice) just gets the type message. Tests cover format: date and date-time raising, format: email importing, and the plain-type message leaving the clause out.
  • Title and keyword. Retitled to match the commit, and the body and commit now say Refs #707, so the required half stays open for fix(703): carry decision specs through to_stickler_config() and model_from_json() #724.
  • $ref test. Added one for a $ref to a wrong-type definition.

tests/decisions/test_schema_import.py: 32 passed here, 16 fail against dev's importer. Full suite with extras: 7511 passed, 3 skipped, 1 xfailed. ruff check . is clean.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

anytime Non-breaking improvement off the release critical path: merge whenever it's ready

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants