Rashi quotes from Chazal that Leah was supposed to have another son, but given that there were going to be only 12 shevatim, she realized that if she has a baby boy, it would mean that Rachel would be the mother of even fewer shevatim than the shefachos. Leah therefore davened that her baby be switched to become a girl, and Dinah was born.
The gemara asks how such a thing is possible. The Mishna in Brachos writes that a person who says a ye’hi ratzon and asks for his pregnant wife to give birth to a boy is wasting his breath. Once the fetus’ sex is fixed, there is no going back. The gemara gives two answers: 1) what happened to Leah was a miracle; 2) the sex of the fetus is fixed only after 40 days from conception and Leah davened within the 40 day window.
The Midrash Tanchuma raises the same issue but gives a very different answer. Nothing is impossible for G-d, says the Midrash (a very frum answer!) – even changing the sex of the fetus in utero.
Some of the meforshei Rashi understand that the gemara and Tanchuma are at odds. The Mishna holds that what Leah asked for was impossible; the Midrash holds that G-d can do anything. There must be some braysa or other Tanna out there that the Tanchuma relies on in contradiction to the Mishna. (It would be interesting to speculate what the nekudas hamachlokes here might be. Is this like the question of whether G-d can make a square circle? It doesn't sound like the same question.)
The Taz (here), however, writes that there is no contradiction. The gemara in Brachos, explains the Taz, is addressing the question of how it is possible for the sex of a baby to change – a practical question. To that, the gemara answers that it must have been a miracle (or before 40 days). But there is another question that the gemara does not address: given that what Leah was asking for could only come about through a miracle, how could she ask for it? Why was it not a tefilas shav, a waste of breath, like the case in the Mishna? It must be, says the Taz, that there is a difference between saying a ye'hi ratzon for the baby to be born a boy when you know the fetus is a girl vs. asking Hashem to change the sex of the fetus. One is a request for a square circle -- a girl that is a boy. The other is a request for the circle to be transformed into a square -- a different metziyus. True, that would take a miracle -- but, explains the Tanchuma, there is nothing that says you can't pray for a miracle.
Ain somchin al ha'nes, says the Taz, only applies to the realm of action. When it comes to tefilah, you can shoot for the stars, even if it would take a miracle to get what you want. (See Maharasha in Kidushin 29b as well).
One other point on tefilah: "Vayizkor Elokim es Rachel vayishma eileha Elokim..." (30:22) At first glance you would expect the two clauses to be reversed, i.e. first "vayishma," Hashem would hear Rachel's tefilah, and then "vayizkor," he would remember to do something for her. But that's not how it works. The Ohr haChaim explains that first there is the "vayizkor," Hashem "remembering" that he wants to do something for Rachel. But, as we discussed back here also from the Ohr haChaim, even when Hashem wants to give a person something, it's not a freebie -- the person still has to earn the gift. Hashem helps out by providing the opportunity to do so. Rachel still had to do her part, in this case davening, so that "vayishma eileha..."