Skip to content

Commit d34ab1a

Browse files
committed
Add no content warnings and only call text_contents detectors when there is content text, add Message::has_content
Signed-off-by: declark1 <[email protected]>
1 parent 206565d commit d34ab1a

File tree

3 files changed

+48
-19
lines changed

3 files changed

+48
-19
lines changed

src/clients/openai.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -597,10 +597,17 @@ impl Message {
597597
/// Returns message text content.
598598
pub fn text(&self) -> Option<&String> {
599599
match &self.content {
600-
Some(Content::Text(text)) => Some(text),
600+
Some(Content::Text(text)) if !text.is_empty() => Some(text),
601601
_ => None,
602602
}
603603
}
604+
605+
/// Returns `true` if message contains non-empty content.
606+
pub fn has_content(&self) -> bool {
607+
self.content
608+
.as_ref()
609+
.is_some_and(|content| !content.is_empty())
610+
}
604611
}
605612

606613
/// Content.

src/orchestrator/handlers/chat_completions_detection/streaming.rs

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -486,17 +486,27 @@ async fn handle_whole_doc_detection(
486486
// Spawn detection tasks
487487
let mut tasks = Vec::with_capacity(choices.len() * detector_groups.len());
488488
for (choice_index, message) in &choices {
489+
if !message.has_content() {
490+
// Add no content warning
491+
warnings.push(CompletionDetectionWarning::new(
492+
DetectionWarningReason::EmptyOutput,
493+
&format!("Choice of index {} has no content", choice_index),
494+
));
495+
}
489496
for (detector_type, detectors) in &detector_groups {
490497
let detection_task = match detector_type {
491-
TextContents => tokio::spawn(
492-
common::text_contents_detections(
493-
ctx.clone(),
494-
headers.clone(),
495-
detectors.clone(),
496-
vec![(0, message.text().cloned().unwrap_or_default())],
497-
)
498-
.in_current_span(),
499-
),
498+
TextContents => match message.text() {
499+
Some(content_text) => tokio::spawn(
500+
common::text_contents_detections(
501+
ctx.clone(),
502+
headers.clone(),
503+
detectors.clone(),
504+
vec![(0, content_text.clone())],
505+
)
506+
.in_current_span(),
507+
),
508+
_ => continue, // no content, skip
509+
},
500510
TextChat => tokio::spawn(
501511
common::text_chat_detections(
502512
ctx.clone(),

src/orchestrator/handlers/chat_completions_detection/unary.rs

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -194,17 +194,29 @@ async fn handle_output_detection(
194194
// Spawn detection tasks
195195
let mut tasks = Vec::with_capacity(chat_completion.choices.len() * detector_groups.len());
196196
for choice in &chat_completion.choices {
197+
if !choice.message.has_content() {
198+
// Add no content warning
199+
chat_completion
200+
.warnings
201+
.push(CompletionDetectionWarning::new(
202+
DetectionWarningReason::EmptyOutput,
203+
&format!("Choice of index {} has no content", choice.index),
204+
));
205+
}
197206
for (detector_type, detectors) in &detector_groups {
198207
let detection_task = match detector_type {
199-
TextContents => tokio::spawn(
200-
common::text_contents_detections(
201-
ctx.clone(),
202-
headers.clone(),
203-
detectors.clone(),
204-
vec![(0, choice.message.text().cloned().unwrap_or_default())],
205-
)
206-
.in_current_span(),
207-
),
208+
TextContents => match choice.message.text() {
209+
Some(content_text) => tokio::spawn(
210+
common::text_contents_detections(
211+
ctx.clone(),
212+
headers.clone(),
213+
detectors.clone(),
214+
vec![(0, content_text.clone())],
215+
)
216+
.in_current_span(),
217+
),
218+
_ => continue, // no content, skip
219+
},
208220
TextChat => tokio::spawn(
209221
common::text_chat_detections(
210222
ctx.clone(),

0 commit comments

Comments
 (0)