Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 21, 2025

Replace JsonSerializer.Deserialize with JsonElement.Parse throughout Microsoft.Extensions.AI libraries and tests. JsonElement.Parse is the canonical API for parsing JSON to JsonElement since System.Text.Json 10.0.0, and supports multiple input types including strings, byte spans, and streams.

Changes

  • Test files (7 instances): ChatResponseFormatTests, AIAnnotationTests, CitationAnnotationTests, AzureAIInferenceChatClientTests, ChatClientIntegrationTests, OpenAIConversionTests (2 instances)
  • Production code (2 instances):
    • MicrosoftExtensionsAIChatExtensions (byte span conversion)
    • AIJsonUtilities.Schema.Create (removed internal ParseJsonElement helper method)

Examples

// String to JsonElement
// Before
JsonSerializer.Deserialize<JsonElement>("[1,2,3]", AIJsonUtilities.DefaultOptions)
// After
JsonElement.Parse("[1,2,3]")

// ReadOnlySpan<byte> to JsonElement
// Before
JsonSerializer.Deserialize(ms.GetBuffer().AsSpan(0, (int)ms.Position), AIJsonUtilities.DefaultOptions.GetTypeInfo(typeof(JsonElement)))
// After
JsonElement.Parse(ms.GetBuffer().AsSpan(0, (int)ms.Position))

// Removed internal helper method
// Before
private static JsonElement ParseJsonElement(ReadOnlySpan<byte> utf8Json)
{
    Utf8JsonReader reader = new(utf8Json);
    return JsonElement.ParseValue(ref reader);
}
// After
(removed - use JsonElement.Parse directly)
Original prompt

Find all use of JsonSerializer.Deserialize in the Microsoft.Extensions.AI libraries and tests, where it's being used to deserialize from a string into a JsonElement, and instead use JsonElement.Parse. JsonElement.Parse was added in the 10.0.0 version of System.Text.Json, which these are all using.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Microsoft Reviewers: Open in CodeFlow

…to JsonElement conversions

Co-authored-by: stephentoub <[email protected]>
Copilot AI changed the title [WIP] Refactor JsonSerializer.Deserialize usage to JsonElement.Parse Use JsonElement.Parse for string-to-JsonElement conversions in AI libraries Nov 21, 2025
Copilot AI requested a review from stephentoub November 21, 2025 02:46
@stephentoub
Copy link
Member

@copilot, please do another search. I think you missed some, such as in OpenAIConversionTests.cs

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot, please do another search. I think you missed some, such as in OpenAIConversionTests.cs

You're right, I missed 3 additional instances. Fixed in commit a44e0bc:

  • MicrosoftExtensionsAIChatExtensions.cs line 237 (production code using byte span)
  • OpenAIConversionTests.cs lines 488 and 544 (test code using byte spans)

I initially only looked for string-to-JsonElement conversions, but JsonElement.Parse also supports ReadOnlySpan overloads.

@stephentoub stephentoub marked this pull request as ready for review November 21, 2025 03:00
@stephentoub stephentoub requested a review from a team as a code owner November 21, 2025 03:00
Copilot AI review requested due to automatic review settings November 21, 2025 03:00
@stephentoub stephentoub enabled auto-merge (squash) November 21, 2025 03:00
Copilot AI changed the title Use JsonElement.Parse for string-to-JsonElement conversions in AI libraries Use JsonElement.Parse for JsonElement conversions in AI libraries Nov 21, 2025
Copilot AI requested a review from stephentoub November 21, 2025 03:01
Copy link
Contributor

Copilot AI left a comment

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 modernizes JSON parsing in the AI libraries by migrating from JsonSerializer.Deserialize<JsonElement>() to the newer, more efficient JsonElement.Parse() method introduced in System.Text.Json 10.0.0.

Key changes:

  • Replaced 8 instances across test and production code where string or byte span inputs were being deserialized to JsonElement
  • Removed unnecessary AIJsonUtilities.DefaultOptions parameters since JsonSerializerOptions don't affect JsonElement parsing behavior
  • Improved code efficiency by using the purpose-built parsing API

Reviewed Changes

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

Show a summary per file
File Description
OpenAIConversionTests.cs Updated 2 test assertions that parse function arguments from byte spans to JsonElement
ChatClientIntegrationTests.cs Updated CustomAIFunction's JsonSchema property initialization to use JsonElement.Parse
AzureAIInferenceChatClientTests.cs Updated JSON schema parsing in response format test to use JsonElement.Parse
CitationAnnotationTests.cs Updated serialization roundtrip test assertion to use JsonElement.Parse
AIAnnotationTests.cs Updated serialization roundtrip test assertion to use JsonElement.Parse
ChatResponseFormatTests.cs Updated JSON schema creation in serialization test to use JsonElement.Parse
MicrosoftExtensionsAIChatExtensions.cs Updated production code that converts tool result content from byte span to JsonElement

Copy link
Member

@eiriktsarpalis eiriktsarpalis left a comment

Choose a reason for hiding this comment

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

@copilot I recall that at some point I added an internal helper method in AIJsonUtilities that replicates the behavior of the new JsonElement.Parse method. If it still exists, please remove it and update all consuming code.

auto-merge was automatically disabled November 21, 2025 08:58

Head branch was pushed to by a user without write access

Copy link
Contributor Author

Copilot AI commented Nov 21, 2025

@copilot I recall that at some point I added an internal helper method in AIJsonUtilities that replicates the behavior of the new JsonElement.Parse method. If it still exists, please remove it and...

Found and removed the ParseJsonElement helper method in commit c2c0f6e. It was in AIJsonUtilities.Schema.Create.cs and was only used once to initialize DefaultJsonSchema. Updated to use JsonElement.Parse directly.

Copilot AI requested a review from eiriktsarpalis November 21, 2025 09:01
This was referenced Dec 17, 2025
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.

3 participants