openai / openai-dotnet

The official .NET library for the OpenAI API
https://www.nuget.org/packages/OpenAI
MIT License
707 stars 60 forks source link

Mitigate message content serialization problems in mono/wasm/etc. #58

Closed trrwilson closed 2 weeks ago

trrwilson commented 2 weeks ago

As reported:

As described in this dotnet runtime issue, some environments encounter a peculiarity with explicit interface method implementations involving covariant generics:

https://github.com/dotnet/runtime/issues/103365

This manifests as several derived types, most notably the one used for chat messages, inappropriately invoking the serialization logic for the abstract base type rather than the intended derived type -- meaning we don't serialize the important content value and send invalid requests.

Pending runtime resolution, this change aligns this library's impacted request types with a better practice of managing serialization in an overrideable WriteCore(); this means that, even with the covariant oddity invoking the wrong instantiation of the explicit interface, we'll use the correct serialization logic from the downcast type.