@@ -231,26 +231,7 @@ public virtual async Task<Response<PersistentThreadMessage>> CreateMessageAsync(
231231 Argument . AssertNotNull ( contentBlocks , nameof ( contentBlocks ) ) ;
232232
233233 // Convert blocks to a JSON array stored as BinaryData
234- var jsonElements = new List < JsonElement > ( ) ;
235- foreach ( MessageInputContentBlock block in contentBlocks )
236- {
237- // Write the content into a MemoryStream.
238- using var memStream = new MemoryStream ( ) ;
239-
240- // Write the RequestContent into the MemoryStream
241- block . ToRequestContent ( ) . WriteTo ( memStream , default ) ;
242-
243- // Reset stream position to the beginning
244- memStream . Position = 0 ;
245-
246- // Parse to a JsonDocument, then clone the root element so we can reuse it
247- using var tempDoc = JsonDocument . Parse ( memStream ) ;
248- jsonElements . Add ( tempDoc . RootElement . Clone ( ) ) ;
249- }
250-
251- // Now serialize the array of JsonElements into a single BinaryData for the request:
252- var jsonString = JsonSerializer . Serialize ( contentBlocks , JsonElementSerializer . Default . ListJsonElement ) ;
253- BinaryData serializedBlocks = BinaryData . FromString ( jsonString ) ;
234+ BinaryData serializedBlocks = ConvertMessageInputContentBlocksToJson ( contentBlocks ) ;
254235
255236 return await CreateMessageAsync (
256237 threadId ,
@@ -295,6 +276,21 @@ public virtual Response<PersistentThreadMessage> CreateMessage(
295276 Argument . AssertNotNullOrEmpty ( threadId , nameof ( threadId ) ) ;
296277 Argument . AssertNotNull ( contentBlocks , nameof ( contentBlocks ) ) ;
297278
279+ // Convert blocks to a JSON array stored as BinaryData
280+ BinaryData serializedBlocks = ConvertMessageInputContentBlocksToJson ( contentBlocks ) ;
281+
282+ return CreateMessage (
283+ threadId ,
284+ role ,
285+ serializedBlocks ,
286+ attachments ,
287+ metadata ,
288+ cancellationToken
289+ ) ;
290+ }
291+
292+ private static BinaryData ConvertMessageInputContentBlocksToJson ( IEnumerable < MessageInputContentBlock > contentBlocks )
293+ {
298294 // Convert blocks to a JSON array stored as BinaryData
299295 var jsonElements = new List < JsonElement > ( ) ;
300296 foreach ( MessageInputContentBlock block in contentBlocks )
@@ -315,16 +311,7 @@ public virtual Response<PersistentThreadMessage> CreateMessage(
315311
316312 // Now serialize the array of JsonElements into a single BinaryData for the request:
317313 var jsonString = JsonSerializer . Serialize ( jsonElements , JsonElementSerializer . Default . ListJsonElement ) ;
318- BinaryData serializedBlocks = BinaryData . FromString ( jsonString ) ;
319-
320- return CreateMessage (
321- threadId ,
322- role ,
323- serializedBlocks ,
324- attachments ,
325- metadata ,
326- cancellationToken
327- ) ;
314+ return BinaryData . FromString ( jsonString ) ;
328315 }
329316
330317 /// <summary> Gets a list of messages that exist on a thread. </summary>
0 commit comments