Levofloxacin-based therapy or bismuth-based quadruple therapy are the recommended second-line regimens for Helicobacter pylori eradication after failure of clarithromycin-based therapy. However, resistance to levofloxacin has increased in the past decade. Furthermore, little is known about the long-term effects of H pylori eradication on the antibiotic resistome. In this study, we compared these second-line eradication therapies for efficacy, tolerability, and short-term and long-term effects on the gut microbiota, antibiotic resistome, and metabolic parameters. We did a multicentre, open-label, parallel group, randomised controlled trial at eight hospitals in Taiwan. Adult patients (age ≥20 years) with persistent H pylori infection after first-line clarithromycin-based therapy were randomly assigned (1:1, permuted block sizes of four) to receive levofloxacin-based sequential quadruple therapy for 14 days (EAML14; esomeprazole 40 mg and amoxicillin 1 g for 7 days, followed by esomeprazole 40 mg, metronidazole 500 mg, and levofloxacin 250 mg for 7 days, all twice-daily) or bismuth-based quadruple therapy for 10 days (BQ10; esomeprazole 40 mg twice daily, bismuth tripotassium dicitrate 300 mg four times a day, tetracycline 500 mg four times a day, and metronidazole 500 mg three times a day). All investigators were masked to the randomisation sequence. The primary endpoint was H pylori eradication rate measured by 13C urea breath test 6 weeks after second-line treatment according to both intention-to-treat (ITT) and per-protocol analysis. The microbiota composition and antibiotic resistome of faecal samples collected at baseline (before treatment) and at 2 weeks, 8 weeks, and 1 year after eradication therapy was profiled by shotgun metagenomic sequencing and 16S rRNA gene sequencing. The frequency of adverse effects and changes in the gut microbiota and antibiotic resistome were assessed in all participants with available data. The trial is complete and registered with ClinicalTrails.gov, NCT03148366. Between Feb 25, 2015, and Dec 11, 2020, 560 patients were randomly assigned to receive EAML14 or BQ10 (n=280 per group; 261 [47%] men and 299 [53%] women). Mean age was 55·9 years (SD 12·7) in the EAML14 group and 54·9 years (12·3) in the BQ10 group. Eradication of H pylori was achieved in 246 (88%) of 280 participants in the EAML14 group and 245 (88%) of 280 in the BQ10 group according to ITT analysis (risk difference -0·4%, 95% CI -5·8 to 5·1; p=0·90). In the per-protocol analysis, 246 (90%) of 273 participants in the EAML14 group and 245 (93%) of 264 participants in the BQ10 group achieved H pylori eradication (risk difference 2·7%, 95% CI -0·2 to 7·4; p=0·27). Transient perturbation of faecal microbiota diversity at week 2 was largely restored to basal state 1 year after EAML14 or BQ10. Diversity recovery was slower with BQ10, and recovery in species abundance was partial after both therapies. On shotgun sequencing, we observed significant increases in total resistome after EAML14 (p=0·0002) and BQ10 (p=4·3 × 10-10) at week 2, which were restored to pretreatment level by week 8. The resistance rates of Escherichia coli and Klebsiella pneumonia to levofloxacin, ciprofloxacin, ampicillin (ampicillin-sulbactam for K pneumonia), and various cephalosporins were significantly increased in the EAML14 group compared with in the BQ10 group at week 2, which were restored to pretreatment levels and showed no significant differences at week 8 and 1 year. The frequency of any adverse effects was significantly higher after BQ10 therapy (211 [77%] of 273 participants) than after EAML14 therapy (134 [48%] of 277; p<0·0001). We found no evidence of superiority between levofloxacin-based quadruple therapy and bismuth-based quadruple therapy in the second-line treatment of H pylori infection. The transient increase in the antibiotic resistome and perturbation of faecal microbiota diversity were largely restored to pretreatment state from 2 months to 1 year after eradication therapy. The Ministry of Science and Technology of Taiwan, the Ministry of Health and Welfare of Taiwan, National Taiwan University Hospital, Taipei Veteran General Hospital, and the Australian Federal Government through the St George and Sutherland Medical Research Foundation. For the Chinese translation of the abstract see Supplementary Materials section.