feat: 优化工具参数序列化逻辑,确保字符串格式的有效性

This commit is contained in:
oudecheng 2026-05-13 10:07:14 +08:00
parent 3aeaea5fe4
commit 9d3d8b812c

View File

@ -128,12 +128,24 @@ impl OpenAIProvider {
let normalized = self.normalize_tool_arguments(arguments); let normalized = self.normalize_tool_arguments(arguments);
if self.uses_json_tool_arguments() { if self.uses_json_tool_arguments() {
// Model expects JSON object format
normalized normalized
} else { } else {
// Standard OpenAI format: arguments as JSON string
// But ensure we serialize valid JSON, not null
match normalized { match normalized {
Value::String(raw) => Value::String(raw), Value::String(raw) => {
// If the string is already valid JSON, keep it as-is
// Otherwise, ensure it's a proper JSON string
if serde_json::from_str::<Value>(raw).is_ok() {
Value::String(raw)
} else {
// Invalid JSON string - wrap it as a proper JSON string
serde_json::to_string(&raw).unwrap_or_else(|_| "null".to_string())
}
}
value => Value::String( value => Value::String(
serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()), serde_json::to_string(&value).unwrap_or_else(|_| "{}".to_string()),
), ),
} }
} }