It is standard medical procedure for expectant mothers to undergo periodic ultrasound examinations, during which the physicians see the fetus so it can be carefully examined to ensure it is developing properly. During this examination, the doctor can easily identify the fetus’ gender, and doctors generally pass on this information to the parents. The question arises whether it is proper, from a Torah perspective, for the parents to learn the fetus’ gender during pregnancy. The Torah commands, "Tamim Tiheyeh Im Hashem Elokecha" ("You shall be innocent with Hashem your G-d" – Debarim 18:13), which is understood as an obligation not to concern ourselves with the future, to conduct ourselves the way we see fit, placing our trust in Hashem, without trying to access information about the future. Does finding out a fetus’ gender violate this principle?
We do not find any clear-cut basis in Halachic literature to forbid such a practice, and it would appear that learning a fetus’ gender does not indicate a lack of faith in G-d or an inappropriate attempt to access information about the future. There is, however, one interesting passage in the Midrash which perhaps leads us to discourage this practice. The Midrash (Kohelet Rabba) lists several pieces of information which G-d withheld from human beings. For example, nobody knows when he will leave this world, and, quite obviously, G-d arranged this intentionally so that we will always conduct ourselves properly, rather than wait and repent shortly before we die. As we do not know when we will leave this world, we have no choice but to approach every day as potentially our last, and conduct ourselves accordingly. The Midrash also includes in this list the thoughts of other people. G-d does not empower us to read other people’s minds, because if people could access each other’s thoughts, the world would be overrun by animosity. The Midrash lists a fetus’ gender as one of the pieces of information which G-d withholds from us. No reason is given, but we can reasonably assume that if the Midrash includes a fetus’ gender in this list, there must be a valuable reason for this information to be denied to us. Perhaps, if the mother was hoping for one gender, then knowing that the infant is the other gender could cause her distress, which might be detrimental to the child. Or, perhaps to the contrary, knowing the gender during pregnancy diminishes from the excitement when the baby is born. In any event, the Midrash clearly indicates that it is for our benefit that G-d conceals from parents their child’s gender during pregnancy.
While this Midrash certainly does not suffice to establish a Halachic prohibition against finding out a fetus’ gender, it would seem that this is something which should be discouraged, unless there is a particular reason to obtain this information. In some situations, the parents need to know ahead of time whether a Berit Mila must be arranged, and there might be circumstances where for purposes of Shalom Bayit (harmony between husband and wife) this information is valuable. When such a need arises, it is certainly acceptable to be told the gender, as this does not violate any Halachic prohibition.
We should add that if the father is a Kohen, there might actually be value in the parents’ finding out the fetus’ gender. The Mishna Berura (Rav Yisrael Meir Kagan of Radin, 1839-1933) addresses the question of whether a woman who is married to a Kohen may come in contact with Tum’at Met (the impurity generated by a human corpse) during pregnancy, such as by visiting a cemetery or entering a home where a corpse is present. All male Kohanim, including infants, are included in the prohibition which forbids Kohanim from coming in contact with Tum’at Met, and the question thus arises as to whether a pregnant wife of a Kohen should avoid Tum’at Met, in case she gives birth. The Mishna Berura rules that this is permissible, because it is a situation of "Sefek Sefeka" – where two uncertainties are involved. First, it is uncertain whether the fetus is a boy, who is forbidden from coming in contact with Tum’at Met, or a girl, who is not forbidden. Second, it is possible that the infant will be stillborn, Heaven forbid, in which case, of course, there is no prohibition. On this basis, the Mishna Berura permits the pregnant wife of a Kohen to go to a place where there is a human corpse.
However, in a situation where Halacha permits something because of a "Sefek Sefeka," if it becomes possible to resolve one of the uncertainties, there is an obligation to do. Therefore, in the case of a wife of a Kohen who is pregnant, there is value in determining the gender in order to resolve the first uncertainty. Then, if she is carrying a boy, she would be required to avoid exposure to Tum’at Met, and if it is a girl, this would not be necessary.
It should be noted that the Magen Abraham (Rav Abraham Gombiner, 1633-1683) maintained that the pregnant woman in any event would be permitted to go to a place where there is Tum’at Met, because the prohibition does not apply in such a case. Therefore, in consideration of this opinion, we would not go so far as to require a Kohen’s pregnant wife to determine the child’s gender.
(Parenthetically, we should note that a Kohen’s wife is certainly allowed to go to a hospital to deliver the child, despite the high probability that there is a human corpse in the hospital, because this is a situation of Pikua’h Nefesh – a potentially life-threatening circumstance. Additionally, the spread of Tum’a from one room to another and one floor to another in the hospital likely occurs only Mi’de’rabbanan (on the level of Rabbinic enactment), such that there is greater room for leniency.)
Summary: There is no Halachic prohibition against finding out a fetus’ gender during pregnancy, though it is preferable not to, unless there is a particular need, or if not knowing could compromise Shalom Bayit. If the father is a Kohen, it might, according to some opinions, be preferable to find out the gender, so that the mother will avoid places of Tum’at Met if it’s a boy, and will not have to avoid such places if it is a girl. If the couple does not know the gender, the woman is nevertheless allowed to visit places where there is Tum’at Met.