fix[format/openai]: return error on empty msg. (#6511)

Signed-off-by: Yelsin Sepulveda <yelsinsepulveda@gmail.com>
Signed-off-by: The-Best-Codes <bestcodes.official@gmail.com>
Co-authored-by: The-Best-Codes <bestcodes.official@gmail.com>
This commit is contained in:
Yelsin Sepulveda
2026-01-27 10:53:25 -05:00
committed by GitHub
parent 2a41f5daed
commit bc0881caf4
+64 -4
View File
@@ -287,10 +287,15 @@ pub fn response_to_message(response: &Value) -> anyhow::Result<Message> {
.and_then(|c| c.get(0))
.and_then(|m| m.get("message"))
else {
return Ok(Message::new(
Role::Assistant,
chrono::Utc::now().timestamp(),
Vec::new(),
if let Some(error) = response.get("error") {
let error_message = error
.get("message")
.and_then(|m| m.as_str())
.unwrap_or("Unknown error");
return Err(anyhow::anyhow!("API error: {}", error_message));
}
return Err(anyhow::anyhow!(
"No message in API response. This may indicate a quota limit or other restriction."
));
};
@@ -1163,6 +1168,61 @@ mod tests {
Ok(())
}
#[test]
fn test_response_to_message_api_error() -> anyhow::Result<()> {
// Test that API responses with an "error" field return the error message
let response = json!({
"error": {
"message": "You have exceeded your quota",
"type": "insufficient_quota",
"code": "quota_exceeded"
}
});
let result = response_to_message(&response);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("API error:"));
assert!(err.to_string().contains("You have exceeded your quota"));
Ok(())
}
#[test]
fn test_response_to_message_api_error_unknown() -> anyhow::Result<()> {
// Test that API responses with an "error" field but no message return "Unknown error"
let response = json!({
"error": {
"type": "some_error"
}
});
let result = response_to_message(&response);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("API error:"));
assert!(err.to_string().contains("Unknown error"));
Ok(())
}
#[test]
fn test_response_to_message_no_choices() -> anyhow::Result<()> {
// Test that responses without "choices" return an error
let response = json!({
"id": "chatcmpl-123",
"object": "chat.completion",
"created": 1234567890
});
let result = response_to_message(&response);
assert!(result.is_err());
let err = result.unwrap_err();
assert!(err.to_string().contains("No message in API response"));
Ok(())
}
#[test]
fn test_format_messages_tool_request_with_none_arguments() -> anyhow::Result<()> {
// Test that tool calls with None arguments are formatted as "{}" string