The Prophet ﷺ said, "The pen is lifted from three: from the sleeper until he wakes, from the child until he reaches puberty, and from the insane person until he regains his reason."Hammad also said, "And from the mentally impaired person until he regains his reason."
حدثنا عفان قال حدثنا حماد عن حماد عن إبراهيم عن الأسود
عن النبي ﷺ قال رفع القلم عن ثلاث عن النائم حتى يستيقظ وعن الصبي حتى يحتلم وعن المجنون حتى يعقل وقد قال حماد وعن المعتوه حتى يعقل
Its isnad is sahih. The two Hammads here narrate from one another, and both are among the senior scholars of hadith. The hadith was recorded by Abu Dawud, 4/139, no. 4398, and Tirmidhi, 4/32, no. 1423, who said, hasan gharib from Ali, both in the chapter on legal punishments. It was also recorded by Ibn Majah, 2/658, no. 2041, in the chapter on divorce, by Ibn Abu Shayba, 5/268, by Saeed bin Mansur, 2/68, no. 2080, by Ibn Khuzayma, 2/102, and by Ibn Hibban, 259, no. 1496, in al-Mawarid.