Primary immunodeficiency diseases (PIDD) are a group of immune-related disorders that have a current median delay of diagnosis between 6 and 9 years. Early diagnosis and treatment of PIDD has been associated with improved patient outcomes. To develop a machine learning model using elements within the electronic health record data that are related to prior symptomatic treatment to predict PIDD. We conducted a retrospective study of patients with PIDD identified using inclusion criteria of PIDD-related diagnoses, immunodeficiency-specific medications, and low immunoglobulin levels. We constructed a control group of age-,sex-, and race-matched patients with asthma. The primary outcome was the diagnosis of PIDD. We considered comorbidities, laboratory tests, medications, and radiological orders as features, all before diagnosis and indicative of symptom-related treatment. Features were presented sequentially to logistic regression, elastic net, and random forest classifiers, which were trained using a nested cross-validation approach. Our cohort consisted of 6422 patients, of whom 247 (4%) were diagnosed with PIDD. Our logistic regression model with comorbidities demonstrated good discrimination between patients with PIDD and those with asthma (c-statistic: 0.62 [0.58-0.65]). Adding laboratory results, medications, and radiological orders improved discrimination (c-statistic: 0.70 vs 0.62, P < .001), sensitivity, and specificity. Extending to the advanced machine learning models did not improve performance. We developed a prediction model for early diagnosis of PIDD using historical data that are related to symptomatic care, which has potential to fill an important need in reducing the time to diagnose PIDD, leading to better outcomes for immunodeficient patients.