Skip to content

fix(jsonschema): guard against empty items list in create_array_type#1352

Open
devteamaegis wants to merge 1 commit into
PrefectHQ:mainfrom
devteamaegis:fix/typeerror-crash-on-empty-items-list-in-array-schema
Open

fix(jsonschema): guard against empty items list in create_array_type#1352
devteamaegis wants to merge 1 commit into
PrefectHQ:mainfrom
devteamaegis:fix/typeerror-crash-on-empty-items-list-in-array-schema

Conversation

@devteamaegis

Copy link
Copy Markdown

What's broken

jsonschema_to_type crashes with TypeError: Cannot take a Union of no types. when an array schema has items set 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(), when items is a list, the code collects item_types by iterating over it and then calls Union[tuple(item_types)]. When the list is empty, Union[()] raises TypeError: Cannot take a Union of no types. The analogous code in schema_to_type() already uses Union[tuple(types)] if len(types) > 1 else types[0] to guard this, but create_array_type had no such guard.

Fix

Added an early path in the isinstance(items, list) branch: if item_types is empty, fall back to list[Any]. For the single-type case, skip the Union wrapper entirely (matching the schema_to_type pattern). This keeps the change minimal — 6 lines touched.

Test

Added test_empty_items_list_does_not_crash to TestArrayTypes: passes {"type": "array", "items": []} to jsonschema_to_type and asserts the returned type validates a mixed list without raising.

Fixes #1351

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
@github-actions github-actions Bot added the tests label May 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

BUG: TypeError crash in create_array_type() when items is an empty list

1 participant