diff --git a/src/providers/openai.rs b/src/providers/openai.rs index cb30668..6a6d99a 100644 --- a/src/providers/openai.rs +++ b/src/providers/openai.rs @@ -128,12 +128,24 @@ impl OpenAIProvider { let normalized = self.normalize_tool_arguments(arguments); if self.uses_json_tool_arguments() { + // Model expects JSON object format normalized } else { + // Standard OpenAI format: arguments as JSON string + // But ensure we serialize valid JSON, not null 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::(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( - serde_json::to_string(&value).unwrap_or_else(|_| "null".to_string()), + serde_json::to_string(&value).unwrap_or_else(|_| "{}".to_string()), ), } }