Background: Biliary tract cancers, which arise from the intrahepatic or extrahepatic bile ducts and the gallbladder, generally have a poor prognosis and are rising in incidence worldwide. The standard-of-care treatment for advanced biliary tract cancer is chemotherapy with gemcitabine and cisplatin. Because most biliary tract cancers have an immune-suppressed microenvironment, immune checkpoint inhibitor monotherapy is associated with a low objective response rate. We aimed to assess whether adding the immune checkpoint inhibitor pembrolizumab to gemcitabine and cisplatin would improve outcomes compared with gemcitabine and cisplatin alone in patients with advanced biliary tract cancer. Methods: KEYNOTE-966 was a randomised, double-blind, placebo-controlled, phase 3 trial done at 175 medical centres globally. Eligible participants were aged 18 years or older; had previously untreated, unresectable, locally advanced or metastatic biliary tract cancer; had disease measurable per Response Evaluation Criteria in Solid Tumours version 1.1; and had an Eastern Cooperative Oncology Group performance status of 0 or 1. Eligible participants were randomly assigned (1:1) to pembrolizumab 200 mg or placebo, both administered intravenously every 3 weeks (maximum 35 cycles), in combination with gemcitabine (1000 mg/m2 intravenously on days 1 and 8 every 3 weeks; no maximum duration) and cisplatin (25 mg/m2 intravenously on days 1 and 8 every 3 weeks; maximum 8 cycles). Randomisation was done using a central interactive voice-response system and stratified by geographical region, disease stage, and site of origin in block sizes of four. The primary endpoint of overall survival was evaluated in the intention-to-treat population. The secondary endpoint of safety was evaluated in the as-treated population. This study is registered at ClinicalTrials.gov, NCT04003636. Findings: Between Oct 4, 2019, and June 8, 2021, 1564 patients were screened for eligibility, 1069 of whom were randomly assigned to pembrolizumab plus gemcitabine and cisplatin (pembrolizumab group; n=533) or placebo plus gemcitabine and cisplatin (placebo group; n=536). Median study follow-up at final analysis was 25·6 months (IQR 21·7–30·4). Median overall survival was 12·7 months (95% CI 11·5–13·6) in the pembrolizumab group versus 10·9 months (9·9–11·6) in the placebo group (hazard ratio 0·83 [95% CI 0·72–0·95]; one-sided p=0·0034 [significance threshold, p=0·0200]). In the as-treated population, the maximum adverse event grade was 3 to 4 in 420 (79%) of 529 participants in the pembrolizumab group and 400 (75%) of 534 in the placebo group; 369 (70%) participants in the pembrolizumab group and 367 (69%) in the placebo group had treatment-related adverse events with a maximum grade of 3 to 4. 31 (6%) participants in the pembrolizumab group and 49 (9%) in the placebo group died due to adverse events, including eight (2%) in the pembrolizumab group and three (1%) in the placebo group who died due to treatment-related adverse events. Interpretation: Based on a statistically significant, clinically meaningful improvement in overall survival compared with gemcitabine and cisplatin without any new safety signals, pembrolizumab plus gemcitabine and cisplatin could be a new treatment option for patients with previously untreated metastatic or unresectable biliary tract cancer. Funding: Merck Sharp & Dohme, a subsidiary of Merck & Co, Rahway, NJ, USA.