fix(openapi): collapse nullable unions so Optional[List[X]] tools keep valid schemas - #343
Open
K4bain wants to merge 1 commit into
Open
fix(openapi): collapse nullable unions so Optional[List[X]] tools keep valid schemas#343K4bain wants to merge 1 commit into
K4bain wants to merge 1 commit into
Conversation
…p valid schemas (tadata-org#304) Pydantic v2 renders Optional[List[X]] as anyOf: [{type: array, items: ...}, {type: null}]. Projecting that shape into a tool inputSchema by bolting on a bare type: 'array' (or stripping anyOf without hoisting items) yields {type: 'array'} with no items — invalid JSON Schema that MCP clients reject with 'tool parameters array type must have items'. - add collapse_nullable_union(): collapses a single-non-null-variant anyOf to that variant, carrying over title/description/default - convert.py routes path/query/body parameter schemas through it so the projected inputSchema is {type: array, items: {...}} (or the plain type) - clean_schema_for_display() hoists type/items/enum/format from the non-null variant before stripping anyOf, for the response-schema text Regression tests cover Optional[List[str]], Optional[List[Model]], Optional scalars, and the display-side schema.
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.
Fixes #304 —
clean_schema_for_display()stripsanyOfand losesitemsforOptional[List[X]]parameters.Root cause
Pydantic v2 renders
Optional[List[X]]as:{ "anyOf": [{ "type": "array", "items": {...} }, { "type": "null" }] }Both symptom paths in the issue reproduce from that shape:
convert.pybolts a baretype: "array"onto the property (viaget_single_param_type_from_schema) while leavinganyOf(with theitemsinside it) in place — the projected property hadanyOf: [...]ANDtype: "array"but noitems. This is the exact broken schema I observed from the current code.clean_schema_for_display()stripsanyOfwithout hoistingitems, emitting{type: "array"}alone.Both are invalid JSON Schema, and MCP clients reject the tool outright (
tool parameters array type must have items).The fix
collapse_nullable_union()inopenapi/utils.py: collapses a single-non-null-variantanyOfunion to that variant (copying it and carrying overtitle/description/default). Non-union and multi-variant schemas pass through unchanged.convert.pyroutes path/query/body parameter schemas through it, so the projected inputSchema becomes{type: "array", items: {...}}— the correct collapse ofOptional[List[X]].clean_schema_for_display()now hoiststype/items/enum/formatfrom the single non-null variant before strippinganyOf, so the response-schema text never losesitemseither.Verification
mainfirst:Optional[List[str]]andOptional[List[Model]]properties projected astype='array' items=False anyOf=True; after the fix:type='array' items=True anyOf=Falsewith the full item schema preserved ({name, value}for the model case).convert_openapi_to_mcp_toolscoveringOptional[List[str]],Optional[List[Model]],Optional[int], plus an invariant sweep asserting no array property anywhere lacksitems; one forclean_schema_for_display()hoisting behavior including the multi-variant (non-nullable) pass-through case.main, unrelated to this change); ruff check + format clean; mypy clean.Cross-references #246/#165/#218 as related symptoms of the same root family; this fix addresses the mechanism for all of them, though their individual repros deserve their own assertions if maintainers want them folded in.