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

Error deserializing a ChatMessage #207

Open
EricBuist opened this issue May 1, 2024 · 0 comments · May be fixed by #209
Open

Error deserializing a ChatMessage #207

EricBuist opened this issue May 1, 2024 · 0 comments · May be fixed by #209

Comments

@EricBuist
Copy link

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.

zeecorleone added a commit to zeecorleone/OpenAI-API-dotnet that referenced this issue May 9, 2024
@zeecorleone zeecorleone linked a pull request May 9, 2024 that will close this issue
tangrui1992 added a commit to tangrui1992/OpenAI-API-dotnet-fork20240519 that referenced this issue May 20, 2024
…eserialization

Fixed issue OkGoDoIt#207 - Error Deserializing ChatMessage
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant