The Gemara in Brachos, 60a states that during the first forty days after conception one may daven that an expectant mother give birth to a boy. After forty days, the fetus has developed to the extent that the gender has been determined, and this tefillah is no longer relevant. Therefore, one who recites this tefillah after forty days is considered to have davened a tefillas shav – a prayer in vain.
The Mefarshim ask: Granted, after forty days the gender of the baby has been determined, but why can one not daven that a fetus change from a girl to a boy? The Vilna Gaon (cited in Imrei Noam) and the Bechor Shor (Shabbos 21b) both give the same answer: This Gemara teaches us that one must not daven for a miracle that defies the laws of nature. As transforming a fetus from a girl into a boy obviously requires such a miracle, it is not something one is permitted to daven for. Rav Akiva Eiger in his hagahos on Shulchan Aruch also cites the Sefer Chareidim as saying one should not daven for a neis, although he does not reference the Gemara in Brachos.
However, we must ask an obvious question. We learn in this week’s parsha that when Leah realized that she was going to have her seventh son, and her sister Rachel would then only be able to have one of the Shevatim, she davened that her fetus be changed to a girl. The Gemara tells us that Dina was in fact in Rachel’s womb and Yosef was in Leah’s, and they switched. How was Leah allowed to make this request of Hashem?
A similar question is raised pertaining to Chanukah. The Kol Bo (cited by the Rema 187:4 and 682:1) states that if one forgot to say Al Ha’nissim in the appropriate place during bentching he should add a special HaRachaman: “HaRachaman Hu yaaseh lanu nissim k’mo she’asah bayamim haheym…” (“The Merciful One should perform miracles for us as He did in those days…”) followed by the full text of Al Hanissim. The Mefarshim ask: Since one should not daven for a miracle, how is it permitted to say this Harachaman, a clear request for Hashem to perform nissim on our behalf?
The Bechor Shor (ibid.) answers that the rule that one should not ask for a neis applies only when asking that a neis be done for an individual. However, it is permitted to ask that nissim be done for the tzibur at large. This is because the reason one should not daven for a neis (according to the Bechor Shor) is that a person is presumably not worthy of the neis being done for him, rendering the tefillah a tefillas shav. On the other hand, when davening for the tzibur there is reason to assume that the collective merits of the tzibur will deem them worthy of a neis, and the tefillah is not considered in vain.
The Bechor Shor also states that a gavrah rabbah – great person – is allowed to daven for a neis. Generally, one should not daven for a neis is because the tefillah is said to be in vain. However, when a gavrah rabbah davens for a neis, the prayer is not in vain at all. As Chazal tell us, “A tzaddik decrees and Hashem fulfills!” (Kesubos 103b).
The Yeshuos Yaakov (682:2) is of the opinion that one should not ask for a neis because it may cause his zechuyos to be deducted. Accordingly, he draws a distinction between a neis nistar – one that no one will be aware of – and a neis mefursam – one that will be publicized to all.
A nies nistar causes one’s zechuyos to be deducted from; therefore, it is something one should not daven for. A neis mefursam, on the other hand, is not deducted from one’s zechuyos, hence one may daven for it. The reason a neis mefursam is not deducted from one’s zechuyos is because the public kiddush hashem resulting from a neis mefursam brings additional zechuyos to the neis’ recipient, offsetting any deduction of zechuyos caused from the actual performance of the neis. Since one does not stand to lose zechuyos from a neis mefursam, one may daven for such a neis. Since the nissim we ask for in the Ha’Rachaman are nissim mefursamim – similar to the ones found in the story of Chanukah and Purim – it is an acceptable tefillah.
Perhaps we can extend all of these answers to explain how Leah was permitted to daven that Hashem switch her fetus with her sister’s. Since she was asking for something that was not for her benefit, but for the sake of someone else, at her expense, it would likewise be permitted. Also, she was one the level of a gavrah rabbah to be able to daven for such miracles.
I have heard first hand stories where doctors have given grave diagnoses to fetuses and the parents have gone to Rav Chaim Kanievsky shlit”a who advised against the doctors prognosis and told the parents to daven and not perform tests. The reason why this is not in contradiction with the above discussion is because Rav Chaim feels that while a baby is in utero everything is considered a neis nistar. Whether a baby is a male or female is something that is definitely determined after forty days. However, whatever issue the doctors are claiming a fetus has is not considered definite and is still considered unknown and asking for it to change is not regarded as asking for a miracle because it has not yet been defined in a definite manner. Under such circumstances not only can one daven, but one must daven, and through tefilla Hashem can and has countless times overturned diagnosis after diagnosis.