Python: Map NoneType to JSON Schema "null" instead of "object" in schema builder#14155
Open
LeSingh1 wants to merge 1 commit into
Open
Python: Map NoneType to JSON Schema "null" instead of "object" in schema builder#14155LeSingh1 wants to merge 1 commit into
LeSingh1 wants to merge 1 commit into
Conversation
…ema builder
KernelJsonSchemaBuilder omitted NoneType from TYPE_MAPPING, so
get_json_schema(type(None)) and any union of 3+ members containing None
(e.g. int | str | None) emitted {"type": "object"} for the None member
instead of the correct {"type": "null"}. These schemas mislead LLMs
during function calling / structured output. The two-member Optional[T]
path builds nullability separately and was already correct.
Add NoneType (and the "None"/"NoneType" string names used by
build_from_type_name) to TYPE_MAPPING, with regression tests.
Contributor
There was a problem hiding this comment.
Pull request overview
This PR fixes Python JSON Schema generation for NoneType in KernelJsonSchemaBuilder, ensuring None is represented as JSON Schema {"type": "null"} (instead of defaulting to {"type": "object"}) when encountered directly or as a member of larger unions used for function calling / structured output.
Changes:
- Add
type(None)toTYPE_MAPPINGsoget_json_schema(type(None))and union-member schema generation emit"null". - Add
"None"and"NoneType"string mappings sobuild_from_type_name(...)correctly emits"null". - Add regression tests covering
type(None),build(type(None)),Union[int, str, None], and"None"/"NoneType"type-name paths.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| python/semantic_kernel/schema/kernel_json_schema_builder.py | Maps type(None) / "None" / "NoneType" to JSON Schema "null" via TYPE_MAPPING. |
| python/tests/unit/schema/test_schema_builder.py | Adds regression tests for direct NoneType and 3+ member unions containing None. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
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.
Motivation
KernelJsonSchemaBuilderomittedNoneTypefromTYPE_MAPPING, soget_json_schema(type(None))— and any union of 3+ members that includesNone(e.g.int | str | None) — emitted{"type": "object"}for theNonemember instead of the correct{"type": "null"}. These schemas are sent to models during function calling / structured output, where anobjecttype for aNonemember is misleading and can distort tool arguments.The two-member
Optional[T]path builds nullability separately and was already correct; this only affectedNoneTypereached directly or as one member of a 3+-member union.Fix
Add
NoneType(and the"None"/"NoneType"string names used bybuild_from_type_name) toTYPE_MAPPING, soNonemaps to JSON Schema{"type": "null"}.Files
python/semantic_kernel/schema/kernel_json_schema_builder.pypython/tests/unit/schema/test_schema_builder.py— regression testsVerification
Added regression tests covering
type(None)directly and a 3-member union containingNone; they fail before the change ({"type":"object"}) and pass after ({"type":"null"}). ExistingOptional[T]behavior is unchanged.