Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

.Net: Support polymorphic serialization of ChatMessageContent class and its derivatives #8901

Conversation

SergeyMenshykh
Copy link
Member

@SergeyMenshykh SergeyMenshykh commented Sep 18, 2024

Motivation, Context and Description

Today, when serializing chat history, the ChatMessageContent type or its derivatives, like OpenAIChatMessageContent, are serialized as the ChatMessageContent type because the type is used as the chat history element type and the JSON serializer uses this type's public contract for serialization.

However, when attempting serialization of instances of either the ChatMessageContent or OpenAIChatMessageContent type that are declared as KernelContent or object type, the serialization fails with the System.NotSupportedException: Runtime type '{OpenAI}ChatMessageContent' is not supported by polymorphic type 'Microsoft.SemanticKernel.KernelContent' exception. The reason for this exception is that neither of these types is registered for polymorphic serialization.

This PR registers ChatMessageContent type for polymorphic serialization to allow serialization of the type instances declared as of KernelContent or object types:

KernelContent content = new ChatMessageContent(...);

// Now it's possible to serialize the content variable of KernelContent type that holds reference to an instance of the ChatMessageContent type as ChatMessageContent type.
var json = JsonSerializer.Serialize(content); 

Additionally, it enables serialization of unknow in advance and external derivatives of ChatMessageContent type like OpenAIChatMessageContent. These types are serialized using contract of nearest ancestor which is ChatMessageContent by default. To change this behavior and register the unknown type for polymorphic serialization use the contract model - Configure polymorphism with the contract model

KernelContent content = new UnknownChatMessageContent(...);

// The content variable will be serialized using the ChatMessageContent type contract.
var json = JsonSerializer.Serialize(content);

private class UnknownChatMessageContent : ChatMessageContent{}

Closes: #7478

Contribution Checklist

@SergeyMenshykh SergeyMenshykh requested a review from a team as a code owner September 18, 2024 21:26
@SergeyMenshykh SergeyMenshykh self-assigned this Sep 18, 2024
@markwallace-microsoft markwallace-microsoft added .NET Issue or Pull requests regarding .NET code kernel Issues or pull requests impacting the core kernel kernel.core labels Sep 18, 2024
@SergeyMenshykh SergeyMenshykh marked this pull request as draft September 18, 2024 21:44
@SergeyMenshykh SergeyMenshykh marked this pull request as ready for review September 19, 2024 07:57
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Sep 19, 2024
@github-merge-queue github-merge-queue bot removed this pull request from the merge queue due to failed status checks Sep 19, 2024
@SergeyMenshykh SergeyMenshykh added this pull request to the merge queue Sep 19, 2024
Merged via the queue into microsoft:main with commit fbdd6bc Sep 19, 2024
15 checks passed
@SergeyMenshykh SergeyMenshykh deleted the polymorphic-serialization-for-chat-message-content branch September 19, 2024 13:22
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kernel.core kernel Issues or pull requests impacting the core kernel .NET Issue or Pull requests regarding .NET code
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

.Net: Bug When Serializing ChatMessageContent type from a object? throws error.
4 participants