ImportanceThere is an absence of mortality risk assessment tools in first-episode psychosis (FEP) that could enable personalized interventions.ObjectiveTo examine the feasibility of machine learning (ML) in discerning mortality risk in FEP and to assess whether such risk predictions can inform pharmacotherapy choices.Design, Setting, and ParticipantsIn this prognostic study, Swedish nationwide cohort data (from July 1, 2006, to December 31, 2021) were harnessed for model development and validation. Finnish cohort data (from January 1, 1998, to December 31, 2017) were used for external validation. Data analyses were completed between December 2022 and December 2023.Main Outcomes and MeasuresFifty-one nationwide register variables, encompassing demographics and clinical and work-related histories, were subjected to ML to predict future mortality risk. The ML model’s performance was evaluated by calculating the area under the receiver operating characteristic curve (AUROC). The comparative effectiveness of pharmacotherapies in patients was assessed and was stratified by the ML model to those with predicted high mortality risk (vs low risk), using the between-individual hazard ratio (HR). The 5 most important variables were then identified and a model was retrained using these variables in the discovery sample.ResultsThis study included 24 052 Swedish participants (20 000 in the discovery sample and 4052 in the validation sample) and 1490 Finnish participants (in the validation sample). Swedish participants had a mean (SD) age of 29.1 (8.1) years, 62.1% were men, and 418 died with 2 years. Finnish participants had a mean (SD) age of 29.7 (8.0) years, 61.7% were men, and 31 died within 2 years. The discovery sample achieved an AUROC of 0.71 (95% CI, 0.68-0.74) for 2-year mortality prediction. Using the 5 most important variables (ie, the top 10% [substance use comorbidities, first hospitalization duration due to FEP, male sex, prior somatic hospitalizations, and age]), the final model resulted in an AUROC of 0.70 (95% CI, 0.63-0.76) in the Swedish sample and 0.67 (95% CI, 0.56-0.78) in the Finnish sample. Individuals with predicted high mortality risk had an elevated 15-year risk in the Swedish sample (HR, 3.77 [95% CI, 2.92-4.88]) and an elevated 20-year risk in the Finnish sample (HR, 3.72 [95% CI, 2.67-5.18]). For those with predicted high mortality risk, long-acting injectable antipsychotics (HR, 0.45 [95% CI, 0.23-0.88]) and mood stabilizers (HR, 0.64 [95% CI, 0.46-0.90]) were associated with decreased mortality risk. Conversely, for those predicted to survive, only oral aripiprazole (HR, 0.38 [95% CI, 0.20-0.69]) and risperidone (HR, 0.38 [95% CI, 0.18-0.82]) were associated with decreased mortality risk.Conclusions and RelevanceIn this prognostic study, an ML-based model was developed and validated to predict mortality risk in FEP. These findings may help to develop personalized interventions to mitigate mortality risk in FEP.