Patients with advanced gastric or gastro-oesophageal junction cancer that progresses on chemotherapy have poor outcomes. We compared pembrolizumab with paclitaxel in patients with advanced gastric or gastro-oesophageal junction cancer that progressed on first-line chemotherapy with a platinum and fluoropyrimidine.This randomised, open-label, phase 3 study was done at 148 medical centres in 30 countries. Eligible patients were randomised (1:1) in blocks of four per stratum with an interactive voice-response and integrated web-response system to receive either pembrolizumab 200 mg every 3 weeks for up to 2 years or standard-dose paclitaxel. Primary endpoints were overall survival and progression-free survival in patients with a programmed cell death ligand 1 (PD-L1) combined positive score (CPS) of 1 or higher. Safety was assessed in all patients, irrespective of CPS. The significance threshold for overall survival was p=0·0135 (one-sided). This trial is registered at ClinicalTrials.gov, number NCT02370498.Between June 4, 2015, and July 26, 2016, 592 patients were enrolled. Of the 395 patients who had a PD-L1 CPS of 1 or higher, 196 patients were assigned to receive pembrolizumab and 199 patients were assigned to receive paclitaxel. As of Oct 26, 2017, 326 patients in the population with CPS of 1 or higher had died (151 [77%] of 196 patients in the pembrolizumab group and 175 [88%] of 199 patients in the paclitaxel group). Median overall survival was 9·1 months (95% CI 6·2-10·7) with pembrolizumab and 8·3 months (7·6-9·0) with paclitaxel (hazard ratio [HR] 0·82, 95% CI 0·66-1·03; one-sided p=0·0421). Median progression-free survival was 1·5 months (95% CI 1·4-2·0) with pembrolizumab and 4·1 months (3·1-4·2) with paclitaxel (HR 1·27, 95% CI 1·03-1·57). In the total population, grade 3-5 treatment-related adverse events occurred in 42 (14%) of the 294 patients treated with pembrolizumab and 96 (35%) of the 276 patients treated with paclitaxel.Pembrolizumab did not significantly improve overall survival compared with paclitaxel as second-line therapy for advanced gastric or gastro-oesophageal junction cancer with PD-L1 CPS of 1 or higher. Pembrolizumab had a better safety profile than paclitaxel. Additional trials of pembrolizumab in gastric and gastro-oesophageal cancer are ongoing.Merck Sharp & Dohme, a subsidiary of Merck & Co.