The Prophet ﷺ said, "Allah has assigned an angel to the womb. The angel says, 'My Lord, a drop of fluid. My Lord, a clot. My Lord, a lump of flesh.' When Allah
decrees its creation, the angel asks, 'My Lord, will it be wretched or blessed? Male or female? What is its provision? What is its lifespan?' So all of that is written while it is still in its mother's womb."
حدثنا يحيى بن سعيد حدثنا حماد بن زيد حدثنا عبيد الله بن أبي بكر
عن النبي ﷺ قال إن الله عز وجل وكل بالرحم ملكا قال أي رب نطفة أي رب علقة أي رب مضغة فإذا قضى الرب عز وجل خلقها قال أي رب أشقي أو سعيد ذكرا أو أنثى فما الرزق وما الأجل قال فيكتب كذلك في بطن أمه
Its isnad is sahih. Ubaydullah bin Abu Bakr bin Anas is narrating here from his grandfather. The hadith is found in Bukhari 1/87, in the book of menstruation, on formed and unformed [fetuses], in Muslim 4/2036 number 2643, in the book of divine decree, on how the human being is created, in Abu Dawud 4/228 number 4708, in the book of sunnah, on divine decree, and in Tirmidhi 4/446 number 2137, in the book of divine decree, on what has been said that deeds are judged by their endings. All of them narrate it from Ibn Masud.