Special-case AIContent returned from AIFunctionFactory.Create AIFunctions to not be serialized#6935
Merged
stephentoub merged 2 commits intodotnet:mainfrom Oct 20, 2025
Merged
Conversation
…ions to not be serialized They'll likely end up being serialized, anyway, by leaf IChatClients, but this gives those IChatClients the opportunity to do something different, such as by treating DataContent differently.
Contributor
There was a problem hiding this comment.
Pull Request Overview
This PR modifies the AIFunctionFactory to special-case AIContent-related return types by not serializing them automatically. This allows downstream IChatClient implementations to handle different content types (like DataContent) in specialized ways rather than forcing JSON serialization at the function factory level.
Key Changes
- Modified
AIFunctionFactoryto detect and preserveAIContentandIEnumerable<AIContent>return types without serialization - Added type normalization for return types to support reflection-based serialization scenarios
- Updated tests to verify the new behavior across synchronous, async Task, and async ValueTask patterns
- Reorganized JSON serialization attributes in
AIJsonUtilities.Defaults.csfor better maintainability
Reviewed Changes
Copilot reviewed 6 out of 6 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs |
Added special handling for AIContent return types and type normalization logic |
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs |
Reorganized JsonSerializable attributes with semantic grouping and added IEnumerable |
src/Libraries/Microsoft.Extensions.AI.OpenAI/OpenAIResponsesChatClient.cs |
Added switch statement structure for future AIContent type handling in tool results |
test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs |
Added comprehensive test coverage for AIContent return type behavior |
test/Libraries/Microsoft.Extensions.AI.OpenAI.Tests/OpenAIChatClientTests.cs |
Updated test expectations to reflect serialized TextContent in tool responses |
test/Libraries/Microsoft.Extensions.AI.Integration.Tests/VerbatimHttpHandler.cs |
Refactored whitespace removal logic and fixed control flow issue |
test/Libraries/Microsoft.Extensions.AI.Tests/Functions/AIFunctionFactoryTest.cs
Show resolved
Hide resolved
src/Libraries/Microsoft.Extensions.AI.Abstractions/Functions/AIFunctionFactory.cs
Show resolved
Hide resolved
eiriktsarpalis
approved these changes
Oct 20, 2025
jeffhandley
pushed a commit
to jeffhandley/extensions
that referenced
this pull request
Oct 21, 2025
…ions to not be serialized (dotnet#6935) * Special-case AIContent returned from AIFunctionFactory.Create AIFunctions to not be serialized They'll likely end up being serialized, anyway, by leaf IChatClients, but this gives those IChatClients the opportunity to do something different, such as by treating DataContent differently. * Update XML comments
jeffhandley
pushed a commit
to jeffhandley/extensions
that referenced
this pull request
Oct 21, 2025
…ions to not be serialized (dotnet#6935) * Special-case AIContent returned from AIFunctionFactory.Create AIFunctions to not be serialized They'll likely end up being serialized, anyway, by leaf IChatClients, but this gives those IChatClients the opportunity to do something different, such as by treating DataContent differently. * Update XML comments
jeffhandley
pushed a commit
to jeffhandley/extensions
that referenced
this pull request
Oct 21, 2025
…ions to not be serialized (dotnet#6935) * Special-case AIContent returned from AIFunctionFactory.Create AIFunctions to not be serialized They'll likely end up being serialized, anyway, by leaf IChatClients, but this gives those IChatClients the opportunity to do something different, such as by treating DataContent differently. * Update XML comments
This was referenced Nov 11, 2025
Merged
Merged
Closed
This was referenced Nov 19, 2025
Closed
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 subscribe to this conversation on GitHub.
Already have an account?
Sign in.
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.
They'll likely end up being serialized, anyway, by leaf IChatClients, but this gives those IChatClients the opportunity to do something different, such as by treating DataContent differently.
Microsoft Reviewers: Open in CodeFlow