Conversation
Add two new public overloads of AddAIContentType that accept a baseType parameter, allowing consumers to register AIContent-derived types in custom polymorphic hierarchies (e.g., under ToolCallContent or ToolResultContent, not just AIContent). - Generic: AddAIContentType<TContent>(options, baseType, discriminatorId) - Non-generic: AddAIContentType(options, baseType, contentType, discriminatorId) Updates API baseline and adds comprehensive unit tests. Co-authored-by: jozkee <[email protected]>
Copilot
AI
changed the title
[WIP] Make AddAIContentType method public
Expose AddAIContentType overloads with baseType parameter
Mar 5, 2026
stephentoub
reviewed
Mar 5, 2026
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.cs
Outdated
Show resolved
Hide resolved
stephentoub
reviewed
Mar 5, 2026
src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.cs
Outdated
Show resolved
Hide resolved
Instead of adding new baseType overloads, modify the existing AddAIContentType methods to automatically walk the inheritance chain from the content type up to AIContent, registering against every intermediate base type. This eliminates the need for callers to manually register against each level in the hierarchy. The internal registration of experimental types (CodeInterpreter, ImageGeneration) is simplified from 8 calls to 4, since the chain walker handles intermediate bases (ToolCallContent/ToolResultContent) automatically. Co-authored-by: Copilot <[email protected]>
…lot/make-add-ai-content-type-public # Conflicts: # src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs
stephentoub
approved these changes
Mar 6, 2026
Member
stephentoub
left a comment
There was a problem hiding this comment.
LGTM. Can you also add a one-liner to the M.E.AI.Abstractions CHANGELOG for this?
Contributor
There was a problem hiding this comment.
Pull request overview
This PR improves Microsoft.Extensions.AI.Abstractions JSON polymorphic configuration by making AddAIContentType automatically register a custom AIContent-derived type against every base type in its inheritance chain up to AIContent, eliminating the need for callers to register the same derived type multiple times.
Changes:
- Updated
AddAIContentTypeto walk the base-type chain and register polymorphic derived types at each level (e.g.,ToolCallContentandAIContent). - Simplified internal default registration for experimental tool-call / tool-result content types by relying on the chain-walker behavior.
- Added tests covering chain registration for both custom hierarchies and built-in intermediate base types, and for both generic/non-generic overloads.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.
| File | Description |
|---|---|
| src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.cs | Replaces single-base registration with AddAIContentTypeChain that registers derived types for each base up to AIContent. |
| src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs | Reduces default experimental content registration calls by leveraging the inheritance chain registration behavior. |
| test/Libraries/Microsoft.Extensions.AI.Abstractions.Tests/Utilities/AIJsonUtilitiesTests.cs | Adds coverage verifying roundtrip polymorphism through custom and built-in base-type chains, for both overload forms. |
Co-authored-by: Copilot <[email protected]>
AddAIContentType automatically registers the content type against every base in the inheritance chain up to AIContent
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.
Follow-up from #7299 (comment).
Summary
Instead of exposing new
baseTypeoverloads, the existingAddAIContentTypemethods now automatically walk the inheritance chain from the content type up toAIContent, registering the type as a polymorphic derived type at every level. This means a single call likeoptions.AddAIContentType<MyToolCall>("myToolCall")whereMyToolCall : ToolCallContent : AIContentwill register against bothToolCallContentandAIContent.Usage
Changes
AIJsonUtilities.cs— Existing public overloads now callAddAIContentTypeChain, which walks the base type hierarchy using a static local function. No new public API surface added.AIJsonUtilities.Defaults.cs— Internal experimental type registration simplified from 8 calls to 4, since the chain walker handles intermediate bases (ToolCallContent/ToolResultContent) automatically.Microsoft.Extensions.AI.Abstractions.json— Reverted API baseline additions (no new overloads needed).AIJsonUtilitiesTests.cs— Tests verify chain registration through both custom (LeafContent : DerivedAIContent : AIContent) and built-in (DerivedToolCallContent : ToolCallContent : AIContent) hierarchies, usingTheoryto cover both generic and non-generic overloads.💡 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