Repository navigation
fix(export): write nullable items for a list of a union with None - #690
Conversation
- to_json_schema() read only Optional[T] as a nullable list element, so List[Union[str, Literal["b"], None]] wrote string items without null and the rebuilt model rejected its None items - before awslabs#666 that shape warned; the union-by-arms rule skips the None arm, so the export now has to carry it - only multi-arm unions with a None arm change; a model union and to_stickler_config() are untouched
Security Scan ResultsASH Security Scan Report
Scan Metadata
SummaryScanner ResultsThe table below shows findings by scanner, with status based on severity thresholds and dependencies:
Report generated by Automated Security Helper (ASH) at 2026-12-03T15:44:17+00:00 |
mangeshraut712
left a comment
There was a problem hiding this comment.
Ran this against dev (33ed6c3). Everything below is from running the branch, not reading it. Not an approval — that's for a maintainer.
Confirmed
- Full suite on the branch:
7122 passed, 13 skipped, 1 xfailed— matches the description.ruff checkpasses. - The new test has teeth:
test_a_list_element_with_a_none_arm_keeps_its_none_itemsdropped ontodevgives3 failed, 1 passed— the three multi-arm unions fail andOptional[str]passes, exactly as described. - The 7-line fix reads correctly: it only promotes
element_is_nullablewhen the unwrapped element is a union with aNoneTypearm, so_unwrap_optional's existingOptional[T]behaviour is untouched.
The user-visible failure, end to end
Validating data that the original model accepts against the exported schema:
dev: items.type = string -> ["a", "b", None] REJECTED: None is not of type 'string'
branch: items.type = ['string', 'null'] -> ["a", "b", None] ACCEPTED
The differential, independently
I ran a 17-element-type differential on to_json_schema()["properties"]["items"] between dev and the branch. Exactly 3 of 17 changed, each gaining "null" in items and nothing else:
Union[str, int, None]Union[str, Literal["b"], None]Annotated[Union[str, int, None], "x"]
The 14 unchanged include the controls that matter: Union[str, int] and Union[int, float] (multi-arm, no None) are byte-identical, Union[str, None] is unchanged (already correct), and Optional[List[str]] keeps its nested ["array", "null"]. The UserWarning for Union[str, int] and Union[str, int, None] fires identically on both, so "warnings are unchanged" holds.
On the to_stickler_config() gap in "not covered"
Confirmed it's pre-existing and not touched here — the diff is confined to to_json_schema(). Since #666 removed the warning that used to catch this, that path is now silent too; worth its own issue so it doesn't get lost, but it's correctly out of scope for this PR.
sromoam
left a comment
There was a problem hiding this comment.
Thanks Diego. The list fix works: on this head, List[Union[str, Literal["b"], None]] exports ["string", "null"] items and the rebuilt model accepts ["a", None]. One more place has the same bug.
The scalar branch drops the None arm too. A required f: Union[str, Literal["b"], None] = ComparableField(default=...) exports {"type": "string"}, so from_json_schema(schema)(f=None) raises ValidationError. It's the same regression as #697: before #666 this shape warned, and now it exports silently. field_is_nullable comes from _unwrap_optional, which reads only Optional[T].
Could you add one shared helper, something like annotation_is_nullable() (a union with NoneType among its arms), and use it on the scalar branch and the list branch of to_json_schema()? Please also check whether to_stickler_config() needs it. That fixes the class of bug, and it replaces the two overlapping checks this PR adds (_unwrap_optional and then is_union(...) and NoneType in args, with unwrap_annotated(args[0]) computed twice). A test for the required scalar case would pin it.
List[Union[Card, Bank, None]] doesn't need anything here: since #686 merged, it raises on export.
- a required Union[str, Literal["b"], None] field exported as "string", so the rebuilt model rejected None; the scalar branch had the same gap as the list element - annotation_is_nullable() in the shared helpers (a union with a None arm, seen through Annotated) replaces the two overlapping checks - to_stickler_config() is unchanged: its list branch already writes Optional, and other unions still warn as on dev
|
Thanks @sromoam. Fixed in
One older gap I found while checking, not from this PR and not fixed here: a required Full suite: 7129 passed, 13 skipped, 1 xfailed. |
…rites nullable From review: the entry said any field or list element whose union has a None arm is written nullable, which isn't true for nested lists, type aliases, NewType, an optional nested model or to_stickler_config(). Those keep 1.0.x behaviour and are tracked in awslabs#727.
sromoam
left a comment
There was a problem hiding this comment.
Thanks Diego, approving. The shared annotation_is_nullable fixes both branches. On this head, a required Union[str, Literal["b"], None] exports ["string", "null"] and its list exports nullable items, and both rebuilt models accept None.
I pushed one commit, 12849a03, so this can merge for 1.1.0. It narrows the CHANGELOG line, which said any field or list element whose union has a None arm is now nullable. That isn't true yet for a nested list of one, a type alias or NewType of an optional type, a required Optional nested model, or to_stickler_config(). All of those lose None exactly as 1.0.2 did, so none is a regression. They're in #727 for 1.1.1, with the measurements and a single-predicate fix that could also absorb TypeResolver.is_optional_type.
One small ask if you're in here again: the PR description still says only the list branch changed and "nothing else exports differently". The plain-field export changes too, and that's the right call.
tl;dr for humans
to_json_schema()wroteList[Union[str, Literal["b"], None]]as string items withoutnull, so the rebuilt model rejected theNoneitems the original accepts. Before #666 that shape at least warned; #666 judges a union by its arms and skips theNonearm, so the warning went away and nothing signalled the loss. The list items now carrynull, the same wayList[Optional[str]]already did. Nothing else exports differently.for agents
Nonearm, so the export itself must keep the element nullable.to_json_schema(), a list whose element is a union of several arms plusNonewritesitems: {"type": [<type>, "null"]}. Warnings are unchanged: a union that string items cannot hold, such asUnion[str, int, None], still warns. A model union (Union[Card, Bank, None]) andto_stickler_config()are untouched.StructuredModel.to_json_schema()list branch insrc/stickler/structured_object_evaluator/models/structured_model.py: setselement_is_nullablewhen the unwrapped element is a union with aNoneTypearm._unwrap_optionalreads onlyOptional[T].fix(export): write nullable items for a list of a union with None: patch, corrects an export that the rebuilt model could not load.test_a_list_element_with_a_none_arm_keeps_its_none_itemsintests/structured_object_evaluator/test_export_roundtrip_shapes.py: 4 cases; 3 fail without the fix,Optional[str]is the control.Nonechange, each gainingnullin its items, with the same warnings as before.uv run --frozen pytest -q: 7122 passed, 13 skipped, 1 xfailed.ruff checkpasses.to_stickler_config()writesList[Optional[str]]without nullability, so a rebuilt model rejectsNoneitems there too. That was already so before fix(export): judge a union by its arms in the unmapped-type warning (#662) #666 and before this change.Fixes #697.