@@ -259,6 +259,26 @@ impl ChatCompletionsRequest {
259
259
"`messages` must not be empty" . into ( ) ,
260
260
) ) ;
261
261
}
262
+
263
+ if !self . detectors . input . is_empty ( ) {
264
+ // Content of type Array is not supported yet
265
+ // Adding this validation separately as we do plan to support arrays of string in the future
266
+ if let Some ( Content :: Array ( _) ) = self . messages . last ( ) . unwrap ( ) . content {
267
+ return Err ( ValidationError :: Invalid (
268
+ "Detection on array is not supported" . into ( ) ,
269
+ ) ) ;
270
+ }
271
+
272
+ // As text_content detections only run on last message at the moment, only the last
273
+ // message is being validated.
274
+ if self . messages . last ( ) . unwrap ( ) . is_text_content_empty ( ) {
275
+ return Err ( ValidationError :: Invalid (
276
+ "if input detectors are provided, `content` must not be empty on last message"
277
+ . into ( ) ,
278
+ ) ) ;
279
+ }
280
+ }
281
+
262
282
Ok ( ( ) )
263
283
}
264
284
}
@@ -414,6 +434,32 @@ pub struct Message {
414
434
pub tool_call_id : Option < String > ,
415
435
}
416
436
437
+ impl Message {
438
+ /// Checks if text content of a message is empty.
439
+ ///
440
+ /// The following messages are considered empty:
441
+ /// 1. [`Message::content`] is None.
442
+ /// 2. [`Message::content`] is an empty string.
443
+ /// 3. [`Message::content`] is an empty array.
444
+ /// 4. [`Message::content`] is an array of empty strings and ContentType is Text.
445
+ pub fn is_text_content_empty ( & self ) -> bool {
446
+ match & self . content {
447
+ Some ( content) => match content {
448
+ Content :: Text ( string) => string. is_empty ( ) ,
449
+ Content :: Array ( content_parts) => {
450
+ content_parts. is_empty ( )
451
+ || content_parts. iter ( ) . all ( |content_part| {
452
+ content_part. text . is_none ( )
453
+ || ( content_part. r#type == ContentType :: Text
454
+ && content_part. text . as_ref ( ) . unwrap ( ) . is_empty ( ) )
455
+ } )
456
+ }
457
+ } ,
458
+ None => true ,
459
+ }
460
+ }
461
+ }
462
+
417
463
/// Content.
418
464
#[ derive( Debug , Clone , Serialize , Deserialize , PartialEq ) ]
419
465
#[ serde( untagged) ]
0 commit comments