Hadithcore
Mishkat al-Masabih, 78
graded· Zubair `Aliza'i

Al-Qasim b. Muhammad said that a man told him he was greatly distressed by wandering thoughts while he was engaged in prayer. He told him to persevere with his prayer, explaining that he would not be free from that till he said when he finished his prayer, “I have not performed my prayer perfectly.” Malik transmitted it.

وَعَنِ الْقَاسِمِ بْنِ مُحَمَّدٍ أَنَّ رَجُلًا سَأَلَهُ فَقَالَ: «إِنِّي أهم فِي صَلَاتي فيكثر ذَلِك عَليّ فَقَالَ الْقَاسِم بن مُحَمَّد امْضِ فِي صَلَاتك فَإِنَّهُ لن يذهب عَنْكَ حَتَّى تَنْصَرِفَ وَأَنْتَ تَقُولُ مَا أَتْمَمْتُ صَلَاتي» . رَوَاهُ مَالك

References1 variant
In-Book Reference
Book 1, Hadith 72