Several hadiths substantiate this regarding someone who speaks during prayer out of ignorance.
Among them is Ibn Masʿūd’s widely transmitted hadith concerning the tashahhud. He said:
“We used to say during prayer: ‘Peace be upon Allah from His servants; peace be upon Gabriel and Michael; peace be upon so-and-so and so-and-so.’ The Prophet, may Allah bless him and grant him peace, forbade them from saying this and said: ‘Indeed, Allah is the Source of peace.’”
He did not instruct them to repeat the prayers in which they had said this, even though these words are intrinsically forbidden: it is not permissible to supplicate for peace to be bestowed upon Allah; rather, He is the One to whom supplications are addressed. Since they were unaware that this was forbidden, he did not instruct them to repeat their prayers.
Another example is the Bedouin who said: “O Allah, have mercy on me and Muhammad, and have mercy on no one else besides us.” The Prophet said: “You have restricted what is vast!”—referring to Allah’s mercy. This supplication is forbidden, since it asks Allah not to have mercy on any of His creatures other than those two.
Another example is what someone said when Abū Mūsā led them in prayer: “Have you coupled prayer with righteousness and zakat?”
Abū Mūsā said: “Hittān, was it perhaps you who said that?”
I replied: “I did not say it, though I feared you would rebuke me for it.” Abū Mūsā did not instruct me to repeat the prayer.
On this basis, two views are reported from Ahmad concerning deliberately speaking for the benefit of the prayer:
The first is that it is permissible. This is Mālik’s position.
The second is that it is not permissible. This is al-Shāfiʿī’s position.