ImportanceThe developmental origins of mental illness are incompletely understood. Although the development of autism and schizophrenia are linked to infections during fetal life, it is unknown whether more common psychiatric conditions such as depression might begin in utero.ObjectiveTo estimate the risk of psychopathologic conditions imparted from fetal exposure to any maternal infection while hospitalized during pregnancy.Design, Setting, and ParticipantsA total of 1 791 520 Swedish children born between January 1, 1973, and December 31, 2014, were observed for up to 41 years using linked population-based registries. Children were excluded if they were born too late to contribute person-time, died before being at risk for the outcome, or were missing particular model data. Infection and psychiatric diagnoses were derived using codes from hospitalizations. Directed acyclic graphs were developed from a systematic literature review to determine Cox proportional hazards regression models for risk of psychopathologic conditions in the children. Results were evaluated using probabilistic and simple bias analyses. Statistical analysis was conducted from February 10 to October 17, 2018.ExposuresHospitalization during pregnancy with any maternal infection, severe maternal infection, and urinary tract infection.Main Outcomes and MeasuresInpatient diagnosis of autism, depression, bipolar disorder, or psychosis among offspring.ResultsA total of 1 791 520 Swedish-born children (48.6% females and 51.4% males) were observed from birth up to age 41 years, with a total of 32 125 813 person-years. Within the directed acyclic graph framework of assumptions, fetal exposure to any maternal infection increased the risk of an inpatient diagnosis in the child of autism (hazard ratio [HR], 1.79; 95% CI, 1.34-2.40) or depression (HR, 1.24; 95% CI, 1.08-1.42). Effect estimates for autism and depression were similar following a severe maternal infection (autism: HR, 1.81; 95% CI, 1.18-2.78; depression: HR, 1.24; 95% CI, 0.88-1.73) or urinary tract infection (autism: HR, 1.89; 95% CI, 1.23-2.90; depression: HR, 1.30; 95% CI, 1.04-1.61) and were robust to moderate unknown confounding. Within the directed acyclic graph framework of assumptions, the relationship between infection and depression was vulnerable to bias from loss to follow-up, but separate data from the Swedish Death Registry demonstrated increased risk of suicide among individuals exposed to pregnancy infection. No evidence was found for increased risk of bipolar disorder or psychosis among children exposed to infection in utero.Conclusions and RelevanceThese findings suggest that fetal exposure to a maternal infection while hospitalized increased the risk for autism and depression, but not bipolar or psychosis, during the child’s life. These results emphasize the importance of avoiding infections during pregnancy, which may impart subtle fetal brain injuries contributing to development of autism and depression.