Skip to content

AddAIContentType automatically registers the content type against every base in the inheritance chain up to AIContent#7358

Merged
jozkee merged 6 commits intomainfrom
copilot/make-add-ai-content-type-public
Mar 7, 2026
Merged

AddAIContentType automatically registers the content type against every base in the inheritance chain up to AIContent#7358
jozkee merged 6 commits intomainfrom
copilot/make-add-ai-content-type-public

Conversation

Copy link
Contributor

Copilot AI commented Mar 5, 2026

Follow-up from #7299 (comment).

Summary

Instead of exposing new baseType overloads, the existing AddAIContentType methods now automatically walk the inheritance chain from the content type up to AIContent, registering the type as a polymorphic derived type at every level. This means a single call like options.AddAIContentType<MyToolCall>("myToolCall") where MyToolCall : ToolCallContent : AIContent will register against both ToolCallContent and AIContent.

Usage

// Before: callers had to manually register against each base
options.AddAIContentType<MyToolCall>("myToolCall");
options.AddAIContentType<MyToolCall>(typeof(ToolCallContent), "myToolCall");

// After: a single call handles the entire chain
options.AddAIContentType<MyToolCall>("myToolCall");

Changes

  • AIJsonUtilities.cs — Existing public overloads now call AddAIContentTypeChain, 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, using Theory to 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

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
@github-actions github-actions bot added area-ai Microsoft.Extensions.AI libraries and removed needs-area-label labels Mar 5, 2026
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]>
@jozkee jozkee requested a review from eiriktsarpalis March 6, 2026 18:13
…lot/make-add-ai-content-type-public

# Conflicts:
#	src/Libraries/Microsoft.Extensions.AI.Abstractions/Utilities/AIJsonUtilities.Defaults.cs
@jozkee jozkee marked this pull request as ready for review March 6, 2026 18:21
@jozkee jozkee requested a review from a team as a code owner March 6, 2026 18:21
Copilot AI review requested due to automatic review settings March 6, 2026 18:21
Copy link
Member

@stephentoub stephentoub left a comment

Choose a reason for hiding this comment

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

LGTM. Can you also add a one-liner to the M.E.AI.Abstractions CHANGELOG for this?

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 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 AddAIContentType to walk the base-type chain and register polymorphic derived types at each level (e.g., ToolCallContent and AIContent).
  • 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.

@jozkee jozkee changed the title Expose AddAIContentType overloads with baseType parameter AddAIContentType automatically registers the content type against every base in the inheritance chain up to AIContent Mar 6, 2026
@jozkee jozkee enabled auto-merge (squash) March 6, 2026 22:46
@jozkee jozkee merged commit 243ac34 into main Mar 7, 2026
6 checks passed
@jozkee jozkee deleted the copilot/make-add-ai-content-type-public branch March 7, 2026 00:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area-ai Microsoft.Extensions.AI libraries

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants