A search for the direct production of a pair of τ sleptons, the supersymmetric partners of τ leptons, is presented. Each τ slepton is assumed to decay to a τ lepton and the lightest supersymmetric particle (LSP), which is assumed to be stable and to not interact in the detector, leading to an imbalance in the total reconstructed transverse momentum. The search is carried out in events identified as containing two τ leptons, each decaying to one or more hadrons and a neutrino, and significant transverse momentum imbalance. In addition to scenarios in which the τ sleptons decay promptly, the search also addresses scenarios in which the τ sleptons have sufficiently long lifetimes to give rise to nonprompt τ leptons. The data were collected in proton-proton collisions at a center-of-mass energy of 13 TeV at the CERN LHC with the CMS detector in 2016-2018, and correspond to an integrated luminosity of 138 fb - 1 . No significant excess is seen with respect to standard model expectations. Upper limits on cross sections for the pair production of τ sleptons are obtained in the framework of simplified models. In a scenario in which the τ sleptons are superpartners of left-handed τ leptons, and each undergoes a prompt decay to a τ lepton and a nearly massless LSP, τ slepton masses between 115 and 340 GeV are excluded. In a scenario in which the lifetime of the τ sleptons corresponds to c τ 0 = 0.1 mm , where τ 0 represents the mean proper lifetime of the τ slepton, masses between 150 and 220 GeV are excluded.