22using System ;
33using System . ClientModel . Primitives ;
44using System . Collections . Generic ;
5+ using System . Text ;
56using System . Text . Json ;
67
78namespace OpenAI . Chat ;
@@ -14,6 +15,14 @@ void IJsonModel<ChatMessageContentPart>.Write(Utf8JsonWriter writer, ModelReader
1415
1516 internal static void WriteCoreContentPart ( ChatMessageContentPart instance , Utf8JsonWriter writer , ModelReaderWriterOptions options )
1617 {
18+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
19+ if ( instance . Patch . Contains ( "$"u8 ) )
20+ {
21+ writer . WriteRawValue ( instance . Patch . GetJson ( "$"u8 ) ) ;
22+ return ;
23+ }
24+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
25+
1726 writer . WriteStartObject ( ) ;
1827 writer . WritePropertyName ( "type"u8 ) ;
1928 writer . WriteStringValue ( instance . _kind . ToSerialString ( ) ) ;
@@ -43,11 +52,13 @@ internal static void WriteCoreContentPart(ChatMessageContentPart instance, Utf8J
4352 writer . WritePropertyName ( "file"u8 ) ;
4453 writer . WriteObjectValue ( instance . _fileFile , options ) ;
4554 }
46- writer . WriteSerializedAdditionalRawData ( instance . _additionalBinaryDataProperties , options ) ;
55+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
56+ instance . Patch . WriteTo ( writer ) ;
57+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
4758 writer . WriteEndObject ( ) ;
4859 }
4960
50- internal static ChatMessageContentPart DeserializeChatMessageContentPart ( JsonElement element , ModelReaderWriterOptions options = null )
61+ internal static ChatMessageContentPart DeserializeChatMessageContentPart ( JsonElement element , BinaryData data , ModelReaderWriterOptions options = null )
5162 {
5263 options ??= ModelSerializationExtensions . WireOptions ;
5364
@@ -62,8 +73,9 @@ internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonEle
6273 InternalChatCompletionRequestMessageContentPartImageImageUrl imageUri = default ;
6374 InternalChatCompletionRequestMessageContentPartAudioInputAudio inputAudio = default ;
6475 InternalChatCompletionRequestMessageContentPartFileFile fileFile = default ;
65- IDictionary < string , BinaryData > serializedAdditionalRawData = default ;
66- Dictionary < string , BinaryData > rawDataDictionary = new Dictionary < string , BinaryData > ( ) ;
76+ #pragma warning disable SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
77+ JsonPatch patch = new JsonPatch ( data is null ? ReadOnlyMemory < byte > . Empty : data . ToMemory ( ) ) ;
78+ #pragma warning restore SCME0001 // Type is for evaluation purposes only and is subject to change or removal in future updates.
6779 foreach ( var property in element . EnumerateObject ( ) )
6880 {
6981 if ( property . NameEquals ( "type"u8 ) )
@@ -78,7 +90,7 @@ internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonEle
7890 }
7991 if ( property . NameEquals ( "image_url"u8 ) )
8092 {
81- imageUri = InternalChatCompletionRequestMessageContentPartImageImageUrl . DeserializeInternalChatCompletionRequestMessageContentPartImageImageUrl ( property . Value , options ) ;
93+ imageUri = InternalChatCompletionRequestMessageContentPartImageImageUrl . DeserializeInternalChatCompletionRequestMessageContentPartImageImageUrl ( property . Value , property . Value . GetUtf8Bytes ( ) , options ) ;
8294 continue ;
8395 }
8496 if ( property . NameEquals ( "refusal"u8 ) )
@@ -89,21 +101,20 @@ internal static ChatMessageContentPart DeserializeChatMessageContentPart(JsonEle
89101 if ( property . NameEquals ( "input_audio"u8 ) )
90102 {
91103 inputAudio = InternalChatCompletionRequestMessageContentPartAudioInputAudio
92- . DeserializeInternalChatCompletionRequestMessageContentPartAudioInputAudio ( property . Value , options ) ;
104+ . DeserializeInternalChatCompletionRequestMessageContentPartAudioInputAudio ( property . Value , property . Value . GetUtf8Bytes ( ) , options ) ;
93105 continue ;
94106 }
95107 if ( property . NameEquals ( "file"u8 ) )
96108 {
97109 fileFile = InternalChatCompletionRequestMessageContentPartFileFile
98- . DeserializeInternalChatCompletionRequestMessageContentPartFileFile ( property . Value , options ) ;
110+ . DeserializeInternalChatCompletionRequestMessageContentPartFileFile ( property . Value , property . Value . GetUtf8Bytes ( ) , options ) ;
99111 continue ;
100112 }
101113 if ( true )
102114 {
103- rawDataDictionary . Add ( property . Name , BinaryData . FromString ( property . Value . GetRawText ( ) ) ) ;
115+ patch . Set ( [ .. "$."u8 , .. Encoding . UTF8 . GetBytes ( property . Name ) ] , property . Value . GetUtf8Bytes ( ) ) ;
104116 }
105117 }
106- serializedAdditionalRawData = rawDataDictionary ;
107- return new ChatMessageContentPart ( kind , text , imageUri , refusal , inputAudio , fileFile , serializedAdditionalRawData ) ;
118+ return new ChatMessageContentPart ( kind , text , imageUri , refusal , inputAudio , fileFile , patch ) ;
108119 }
109120}
0 commit comments