fix: gemini empty content (#1425)

This commit is contained in:
Yingjie He
2025-02-27 15:58:26 -08:00
committed by GitHub
parent d0ca46983e
commit 6d28f44768

View File

@@ -63,6 +63,7 @@ pub fn format_messages(messages: &[Message]) -> Vec<Value> {
.map(|content| content.unannotated())
.collect();
let mut tool_content = Vec::new();
for content in abridged {
match content {
Content::Image(image) => {
@@ -74,15 +75,29 @@ pub fn format_messages(messages: &[Message]) -> Vec<Value> {
}));
}
_ => {
parts.push(json!({
"functionResponse": {
"name": response.id,
"response": {"content": content},
}}
));
tool_content.push(content);
}
}
}
let mut text = tool_content
.iter()
.filter_map(|c| match c {
Content::Text(t) => Some(t.text.clone()),
_ => None,
})
.collect::<Vec<_>>()
.join("\n");
if !tool_content.is_empty() {
if text.is_empty() {
text = "Tool call is done.".to_string();
}
parts.push(json!({
"functionResponse": {
"name": response.id,
"response": {"content": {"text": text}},
}}
));
}
}
Err(e) => {
parts.push(json!({"text":format!("Error: {}", e)}));
@@ -400,6 +415,36 @@ mod tests {
);
}
#[test]
fn test_message_to_google_spec_tool_result_multiple_texts() {
let tool_result: Vec<Content> = vec![
Content::text("Hello"),
Content::text("World"),
Content::text("This is a test."),
];
let messages = vec![set_up_tool_response_message("response_id", tool_result)];
let payload = format_messages(&messages);
let expected_payload = vec![json!({
"role": "model",
"parts": [
{
"functionResponse": {
"name": "response_id",
"response": {
"content": {
"text": "Hello\nWorld\nThis is a test."
}
}
}
}
]
})];
assert_eq!(payload, expected_payload);
}
#[test]
fn test_tools_to_google_spec_with_valid_tools() {
let params1 = json!({