Skip to content

Python: Map NoneType to JSON Schema "null" instead of "object" in schema builder#14155

Open
LeSingh1 wants to merge 1 commit into
microsoft:mainfrom
LeSingh1:fix/json-schema-null-type
Open

Python: Map NoneType to JSON Schema "null" instead of "object" in schema builder#14155
LeSingh1 wants to merge 1 commit into
microsoft:mainfrom
LeSingh1:fix/json-schema-null-type

Conversation

@LeSingh1

Copy link
Copy Markdown

Motivation

KernelJsonSchemaBuilder omitted NoneType from TYPE_MAPPING, so get_json_schema(type(None)) — and any union of 3+ members that includes None (e.g. int | str | None) — emitted {"type": "object"} for the None member instead of the correct {"type": "null"}. These schemas are sent to models during function calling / structured output, where an object type for a None member is misleading and can distort tool arguments.

The two-member Optional[T] path builds nullability separately and was already correct; this only affected NoneType reached directly or as one member of a 3+-member union.

Fix

Add NoneType (and the "None" / "NoneType" string names used by build_from_type_name) to TYPE_MAPPING, so None maps to JSON Schema {"type": "null"}.

Files

  • python/semantic_kernel/schema/kernel_json_schema_builder.py
  • python/tests/unit/schema/test_schema_builder.py — regression tests

Verification

Added regression tests covering type(None) directly and a 3-member union containing None; they fail before the change ({"type":"object"}) and pass after ({"type":"null"}). Existing Optional[T] behavior is unchanged.

…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.
Copilot AI review requested due to automatic review settings July 15, 2026 00:49
@LeSingh1
LeSingh1 requested a review from a team as a code owner July 15, 2026 00:49

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) to TYPE_MAPPING so get_json_schema(type(None)) and union-member schema generation emit "null".
  • Add "None" and "NoneType" string mappings so build_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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants