Skip to content

.Net: fix: prevent duplicate "null" in JSON Schema type arrays for nullable parameters#13635

Merged
SergeyMenshykh merged 4 commits into
microsoft:mainfrom
roli-lpci:fix/json-schema-duplicate-null-type
Jun 16, 2026
Merged

.Net: fix: prevent duplicate "null" in JSON Schema type arrays for nullable parameters#13635
SergeyMenshykh merged 4 commits into
microsoft:mainfrom
roli-lpci:fix/json-schema-duplicate-null-type

Conversation

@roli-lpci

Copy link
Copy Markdown
Contributor

Summary

  • Fixes InsertNullTypeIfRequired() producing duplicate "null" entries in JSON Schema type arrays (e.g., ["string", "null", "null"]) for Nullable<T> parameters with = null defaults
  • Replaces reference-equality guard (JsonArray.Contains()) with value-based .Any() check
  • Adds 3 regression tests covering both trigger paths and positive insertion

Root Cause

InsertNullTypeIfRequired() in OpenAIFunction.cs uses jsonArray.Contains(NullType) to check for existing "null" entries before adding one. JsonArray.Contains() compares JsonNode objects by reference equality — JsonNode does not override Equals(). The NullType constant ("null") creates a new JsonNode on implicit conversion, so the guard always fails and "null" is always added as a duplicate.

For Nullable<T> parameters with = null defaults, AIJsonUtilities.CreateJsonSchema() correctly produces ["string", "null"]. The strict-mode sanitizer then attempts to add "null" again — the broken guard lets it through, producing ["string", "null", "null"].

Two trigger paths reach the same bug:

  1. Optional parameters (IsRequired = false) → insertNullType = true
  2. Schemas with "nullable": true keyword

Changes

File Change
dotnet/src/Connectors/Connectors.OpenAI/Core/OpenAIFunction.cs Replace jsonArray.Contains(NullType) with value-based .Any() check (follows existing pattern in NormalizeAdditionalProperties)
dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Core/OpenAIFunctionTests.cs 3 new tests: duplicate prevention for optional params, duplicate prevention for nullable keyword, positive case (null inserted when absent)

Testing

  • 3 new unit tests added covering:
    • ItDoesNotInsertDuplicateNullInTypeArrayForOptionalParameter — schema with pre-existing ["string", "null"] + IsRequired = false
    • ItDoesNotInsertDuplicateNullInTypeArrayForNullableKeyword — schema with "nullable": true + IsRequired = true
    • ItInsertsNullInTypeArrayWhenAbsent — schema with ["string"] only → "null" correctly added

Fixes #13527

… parameters

InsertNullTypeIfRequired() uses jsonArray.Contains(NullType) to check
for existing "null" entries before adding one. JsonArray.Contains()
compares JsonNode objects by reference equality — JsonNode does not
override Equals(). The NullType constant ("null") creates a new
JsonNode on implicit conversion, so the guard always fails and "null"
is always added as a duplicate.

This produces invalid schemas like ["string", "null", "null"] for
Nullable<T> parameters with = null defaults, causing HTTP 400 from
OpenAI's strict-mode API.

The fix replaces .Contains() with a value-based .Any() check, following
the existing pattern used in NormalizeAdditionalProperties in the same
file.

Fixes microsoft#13527

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@roli-lpci roli-lpci requested a review from a team as a code owner March 5, 2026 18:42
@moonbox3 moonbox3 added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel labels Mar 5, 2026
@github-actions github-actions Bot changed the title fix: prevent duplicate "null" in JSON Schema type arrays for nullable parameters .Net: fix: prevent duplicate "null" in JSON Schema type arrays for nullable parameters Mar 5, 2026
@westey-m westey-m assigned westey-m and SergeyMenshykh and unassigned westey-m Apr 29, 2026
@westey-m westey-m moved this to Community PR in Agent Framework Apr 29, 2026
@SergeyMenshykh SergeyMenshykh moved this to Planned in Agent Framework May 19, 2026
Copilot AI review requested due to automatic review settings June 15, 2026 14:50

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 a strict-mode JSON Schema sanitization bug in the OpenAI connector where nullable parameters could end up with duplicate "null" entries in the schema type array (e.g. ["string","null","null"]), causing schema validation failures in strict mode.

Changes:

  • Fixes InsertNullTypeIfRequired() to detect an existing "null" entry by value (instead of JsonArray.Contains() reference equality) before adding "null".
  • Adds regression tests to cover both trigger paths (optional parameter insertion + nullable: true keyword) and the positive insertion case.

Reviewed changes

Copilot reviewed 2 out of 2 changed files in this pull request and generated no comments.

File Description
dotnet/src/Connectors/Connectors.OpenAI/Core/OpenAIFunction.cs Prevents duplicate "null" insertion by switching to a value-based scan of the type array.
dotnet/src/Connectors/Connectors.OpenAI.UnitTests/Core/OpenAIFunctionTests.cs Adds unit tests to ensure "null" is not duplicated and is inserted when missing in strict-mode sanitization.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

The ToFunctionDefinition method parameter has always been named
'allowStrictSchemaAdherence', not 'strict'. Fix the named argument
in the three new test methods to match.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Jun 15, 2026
@github-merge-queue github-merge-queue Bot removed this pull request from the merge queue due to failed status checks Jun 15, 2026
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Jun 16, 2026
Merged via the queue into microsoft:main with commit 7fd75c3 Jun 16, 2026
18 checks passed
@github-project-automation github-project-automation Bot moved this from Planned to Done in Agent Framework Jun 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code

Projects

Status: Done

Development

Successfully merging this pull request may close these issues.

.Net: Bug: Possible problem with KernelJsonSchema generation

7 participants