microsoft / semantic-kernel

Integrate cutting-edge LLM technology quickly and easily into your apps
https://aka.ms/semantic-kernel
MIT License
21.27k stars 3.12k forks source link

.Net: Bug When Serializing `ChatMessageContent` type from a `object?` throws error. #7478

Open RogerBarreto opened 1 month ago

RogerBarreto commented 1 month ago

Describe the bug When attempting to serialize ChatMessageContent from any object that stores it as an object?, an exception is thrown.

To Reproduce

using System.Text.Json;
using Microsoft.SemanticKernel;

var msg = new ChatMessageContent();

msg.Items.Add(new TextContent("text"));

public class A
{
    public object? Message { get; set; }
};

var a = new A { Message = msg };
var dic = new Dictionary<string, object?>() { ["foo"] = msg };

JsonSerializer.Serialize(a); // Throws.
JsonSerializer.Serialize(dic); // Throws.
dluc commented 1 month ago

the issue is likely related to this code: https://github.com/microsoft/semantic-kernel/blob/68b21b81df441fabfdebdb6c5867265308175950/dotnet/src/SemanticKernel.Abstractions/Contents/KernelContent.cs#L12