We are getting errors after updating OpenAI to 1.11.0 preventing responses from the server to be parsed properly. Digging deeper, there is an issue with deserializing ChatMessage objects.
Expected behavior
Deserializing responses from OpenAI including chat messages should work without error. Serializing a ChatMessage to JSON and deserializing the JSON should give a valid ChatMessage equivalent to the original.
Code Snippets
Following code, used to isolate the issue, throws an exception while it should work and deserialize a ChatMessage equivalent to the original.
var message = new ChatMessage() { Role = ChatMessageRole.User, TextContent = "This is a test" }; var jmsg = JsonConvert.SerializeObject(message); var msg = JsonConvert.DeserializeObject<ChatMessage>(jmsg);
jmsg contains a properly serialized JSON:
{"role":"user","content":[{"type":"text","text":"This is a test","image_url":null}],"name":null}
Deserializing above JSON string should give back a ChatMessage. Instead, we are getting the following exception. This also happens when parsing real responses from OpenAPI.
Exception: Newtonsoft.Json.JsonSerializationException: Error setting value to 'ContentItems' on 'OpenAI_API.Chat.ChatMessage'. ---> System.NullReferenceException: Object reference not set to an instance of an object. at OpenAI_API.Chat.ChatMessage.set_ContentItems(IList`1 value) at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
Additional context
I thought this could be some configuration in our specific project, but was able to reproduce with a simple .NET Core 8 project with just OpenAI dependency.
Describe the bug
We are getting errors after updating OpenAI to 1.11.0 preventing responses from the server to be parsed properly. Digging deeper, there is an issue with deserializing ChatMessage objects.
Expected behavior
Deserializing responses from OpenAI including chat messages should work without error. Serializing a ChatMessage to JSON and deserializing the JSON should give a valid ChatMessage equivalent to the original.
Code Snippets
Following code, used to isolate the issue, throws an exception while it should work and deserialize a ChatMessage equivalent to the original.
var message = new ChatMessage() { Role = ChatMessageRole.User, TextContent = "This is a test" }; var jmsg = JsonConvert.SerializeObject(message); var msg = JsonConvert.DeserializeObject<ChatMessage>(jmsg);
jmsg contains a properly serialized JSON:
{"role":"user","content":[{"type":"text","text":"This is a test","image_url":null}],"name":null}
Deserializing above JSON string should give back a ChatMessage. Instead, we are getting the following exception. This also happens when parsing real responses from OpenAPI.
Exception: Newtonsoft.Json.JsonSerializationException: Error setting value to 'ContentItems' on 'OpenAI_API.Chat.ChatMessage'. ---> System.NullReferenceException: Object reference not set to an instance of an object. at OpenAI_API.Chat.ChatMessage.set_ContentItems(IList`1 value) at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value) --- End of inner exception stack trace --- at Newtonsoft.Json.Serialization.ExpressionValueProvider.SetValue(Object target, Object value) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateValueInternal(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue) at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
Additional context
I thought this could be some configuration in our specific project, but was able to reproduce with a simple .NET Core 8 project with just OpenAI dependency.