fix(jsonschema): guard against empty items list in create_array_type#1352
Open
devteamaegis wants to merge 1 commit into
Open
Conversation
Union[tuple([])] raises TypeError when items=[] in an array schema. Add an early-return to list[Any] for the empty case, matching the existing guard pattern in schema_to_type(). Also handle single-type lists without wrapping in Union. Fixes PrefectHQ#1351
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What's broken
jsonschema_to_typecrashes withTypeError: Cannot take a Union of no types.when an array schema hasitemsset to an empty list ([]), which is valid JSON Schema for a zero-element positional tuple. Any caller that generates such a schema — for example, an LLM emitting a function signature with no positional items — will hit this unconditionally.Why it happens
In
create_array_type(), whenitemsis a list, the code collectsitem_typesby iterating over it and then callsUnion[tuple(item_types)]. When the list is empty,Union[()]raisesTypeError: Cannot take a Union of no types.The analogous code inschema_to_type()already usesUnion[tuple(types)] if len(types) > 1 else types[0]to guard this, butcreate_array_typehad no such guard.Fix
Added an early path in the
isinstance(items, list)branch: ifitem_typesis empty, fall back tolist[Any]. For the single-type case, skip theUnionwrapper entirely (matching theschema_to_typepattern). This keeps the change minimal — 6 lines touched.Test
Added
test_empty_items_list_does_not_crashtoTestArrayTypes: passes{"type": "array", "items": []}tojsonschema_to_typeand asserts the returned type validates a mixed list without raising.Fixes #1351