Preoperative diagnosis of urinary infection stones is difficult, and accurate detection of stone composition can only be performed ex vivo. To provide guidance for better perioperative management and postoperative prevention of infection stones, we developed a machine learning model for preoperative identification of infection stones in vivo. The clinical data of patients with urolithiasis who underwent surgery in our hospital from January 2011 to December 2015 and January 2017 to December 2021 were retrospectively analyzed. A total of 2565 patients were included in the study, and 1168 eligible patients with urinary calculi were randomly divided into training set (70%) and test set (30%). Five machine learning algorithms (Support Vector Machine (SVM), Multilayer Perceptron (MLP), Decision Tree (DT), Random Forest Classifier (RFC), and Adaptive Boost (AdaBoost)) and 14 preoperative variables were used to construct the prediction model. The performance measure was the area under the receiver operating characteristic curve (AUC) of the validation set. The importance of 14 features in each prediction model for predicting infection stones was analyzed. A total of 89 patients (5.34%) with infection stones were included in the validation set. All the five prediction models showed strong discrimination in the validation set (AUC: 0.689–0.772). AdaBoost model was selected as the final model (AUC: 0.772(95% confidence interval, 0.657–0.887); Sensitivity: 0.522; Specificity: 0.902), UC positivity, and urine pH value were two important predictors of infection stones. We developed a predictive model through machine learning that can quickly identify infection stones in vivo with good predictive performance. It can be used for risk assessment and decision support of infection stones, optimize the disease management of urinary calculi and improve the prognosis of patients.Supplementary InformationThe online version contains supplementary material available at 10.1007/s00240-023-01457-z.