Skip to content

Commit 12acc24

Browse files
committed
Validate user messages on chat completions endpoint
Signed-off-by: Mateus Devino <mdevino@ibm.com>
1 parent 6bc045f commit 12acc24

File tree

2 files changed

+41
-1
lines changed

2 files changed

+41
-1
lines changed

src/clients/openai.rs

+11-1
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,17 @@ impl TryFrom<Map<String, Value>> for ChatCompletionsRequest {
219219
}
220220
let messages = if let Some(messages) = value.get("messages") {
221221
Vec::<Message>::deserialize(messages)
222-
.map_err(|_| ValidationError::Invalid("error deserializing `messages`".into()))
222+
.map(|messages| {
223+
for message in &messages {
224+
if message.role == Role::User && message.content.is_none() {
225+
return Err(ValidationError::Invalid(
226+
"`content` is mandatory for user messages".into(),
227+
));
228+
}
229+
}
230+
Ok(messages)
231+
})
232+
.map_err(|_| ValidationError::Invalid("error deserializing `messages`".into()))?
223233
} else {
224234
Err(ValidationError::Required("messages".into()))
225235
}?;

tests/chat_completions_detection.rs

+30
Original file line numberDiff line numberDiff line change
@@ -1037,5 +1037,35 @@ async fn orchestrator_validation_error() -> Result<(), anyhow::Error> {
10371037
"failed on non-existing input detector scenario"
10381038
);
10391039

1040+
// Non-existing output detector scenario
1041+
let response = orchestrator_server
1042+
.post(ORCHESTRATOR_CHAT_COMPLETIONS_DETECTION_ENDPOINT)
1043+
.json(&json!({
1044+
"model": MODEL_ID,
1045+
"detectors": {
1046+
"input": {},
1047+
"output": {
1048+
NON_EXISTING_DETECTOR: {},
1049+
}
1050+
},
1051+
"messages": [{
1052+
"role": "user",
1053+
"name": "string"
1054+
}],
1055+
}))
1056+
.send()
1057+
.await?;
1058+
1059+
let results = response.json::<OrchestratorError>().await?;
1060+
debug!("{results:#?}");
1061+
assert_eq!(
1062+
results,
1063+
OrchestratorError {
1064+
code: 422,
1065+
details: "`content` is mandatory for user messages".into()
1066+
},
1067+
"failed on user message missing `content` scenario"
1068+
);
1069+
10401070
Ok(())
10411071
}

0 commit comments

Comments
 (0)