fix: detect anyOf null members by type, not exact dict equality#2617
Open
nyxst4ck wants to merge 1 commit into
Open
fix: detect anyOf null members by type, not exact dict equality#2617nyxst4ck wants to merge 1 commit into
nyxst4ck wants to merge 1 commit into
Conversation
handle_null_fields removed the null member of an anyOf with
schema['anyOf'].remove({'type': 'null'}), which matches by exact dict equality.
A null member annotated with extra keys (e.g. {'type': 'null', 'title': 'N'},
common in JSON Schemas from MCP tools) doesn't equal the bare {'type': 'null'},
raising ValueError: list.remove(x): x not in list. It also mutated the list
while iterating.
Detect null members by item.get('type') == 'null' and rebuild the anyOf list
instead, preserving the flatten-when-single-type behavior.
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.
Problem
handle_null_fieldsremoves thenullmember of ananyOfschema withschema['anyOf'].remove({'type': 'null'})— i.e. by exact dict equality.JSON Schemas frequently annotate the null member with extra keys, e.g.
{'type': 'null', 'title': 'N'}or{'type': 'null', 'description': '...'}(common output from MCP tools and other JSON-Schema sources). In that case no
element equals the bare
{'type': 'null'}, so:This is reachable from the public path:
t_schema→process_schema→handle_null_fields, hit when a dictresponse_schemais passed togenerate_content. It also mutates the list while iterating it. Plain pydanticemits a bare
{'type': 'null'}, which is why existing tests never exercised it.Fix
Identify null members by
item.get('type') == 'null'(with anisinstanceguard) and rebuild the
anyOflist instead ofremove()-ing by equality. Thispreserves the existing flatten-when-one-type-remains behavior, no longer mutates
during iteration, and tolerates null members with extra keys (and multiple null
members).
Testing
Added
test_handle_null_fields_anyof_null_member_with_extra_keys. It raisesValueErroronmainand passes with the fix. The fulltests/transformers/test_schema.pysuite passes (54 passed).