fix: handle empty arguments in tool call (#1111)

Co-authored-by: Salman Mohammed <smohammed@squareup.com>
This commit is contained in:
Yingjie He
2025-02-06 10:34:57 -08:00
committed by GitHub
parent 8eddc40b94
commit 8dad86dc5f
2 changed files with 33 additions and 2 deletions
+24 -1
View File
@@ -183,10 +183,14 @@ pub fn response_to_message(response: Value) -> anyhow::Result<Message> {
.as_str()
.unwrap_or_default()
.to_string();
let arguments = tool_call["function"]["arguments"]
let mut arguments = tool_call["function"]["arguments"]
.as_str()
.unwrap_or_default()
.to_string();
// If arguments is empty, we will have invalid json parsing error later.
if arguments.is_empty() {
arguments = "{}".to_string();
}
if !is_valid_function_name(&function_name) {
let error = ToolError::NotFound(format!(
@@ -581,4 +585,23 @@ mod tests {
Ok(())
}
#[test]
fn test_response_to_message_empty_argument() -> anyhow::Result<()> {
let mut response: Value = serde_json::from_str(OPENAI_TOOL_USE_RESPONSE)?;
response["choices"][0]["message"]["tool_calls"][0]["function"]["arguments"] =
serde_json::Value::String("".to_string());
let message = response_to_message(response)?;
if let MessageContent::ToolRequest(request) = &message.content[0] {
let tool_call = request.tool_call.as_ref().unwrap();
assert_eq!(tool_call.name, "example_fn");
assert_eq!(tool_call.arguments, json!({}));
} else {
panic!("Expected ToolRequest content");
}
Ok(())
}
}