Background Expression of PD-L1 has been shown to be upregulated in some patients with gastric cancer. As part of the phase 1b KEYNOTE-012 study, we aimed to assess the safety and activity of the anti-PD-1 antibody pembrolizumab in patients with PD-L1-positive recurrent or metastatic adenocarcinoma of the stomach or gastro-oesophageal junction. Methods This study was a multicentre, open-label, phase 1b trial done at 13 cancer research centres in the USA, Israel, Japan, South Korea, and Taiwan. We enrolled patients with PD-L1-positive recurrent or metastatic adenocarcinoma of the stomach or gastro-oesophageal junction. Patients received intravenous pembrolizumab at 10 mg/kg once every 2 weeks for 24 months or until progression or unacceptable toxic effects occurred. Response was assessed every 8 weeks in accordance with Response Evaluation Criteria in Solid Tumors version 1.1. The primary objectives were safety in patients who received at least one dose of pembrolizumab and the proportion of patients achieving overall responses in patients who received at least one pembrolizumab dose and who either had a post-baseline scan or who discontinued therapy because of clinical disease progression or a treatment-related adverse event before the first post-baseline scan. The study is registered with ClinicalTrials.gov, number NCT01848834, and is ongoing but no longer enrolling patients. Findings From Oct 23, 2013, to May 5, 2014, 39 patients were enrolled. 36 were evaluable for response by central assessment. Eight (22%, 95% CI 10–39) patients were judged to have had an overall response at central review; all responses were partial. All 39 patients were included in the safety analyses. Five (13%) patients had a total of six grade 3 or 4 treatment-related adverse events, consisting of two cases of grade 3 fatigue, one case each of grade 3 pemphigoid, grade 3 hypothyroidism, and grade 3 peripheral sensory neuropathy, and one case of grade 4 pneumonitis. No treatment-related deaths occurred. Interpretation In this population of patients with recurrent or metastatic PD-L1-positive gastric cancer, pembrolizumab had a manageable toxicity profile and promising antitumour activity, warranting further study in phase 2 and 3 trials. Funding Merck & Co.