The existence of a radius valley in the Kepler size distribution stands as one of the most important observational constraints to understand the origin and composition of exoplanets with radii between those of Earth and Neptune. In this work we provide insights into the existence of the radius valley, first from a pure formation point of view and then from a combined formation-evolution model. We run global planet formation simulations including the evolution of dust by coagulation, drift, and fragmentation, and the evolution of the gaseous disc by viscous accretion and photoevaporation. A planet grows from a moon-mass embryo by either silicate or icy pebble accretion, depending on its position with respect to the water ice line. We include gas accretion, type I–II migration, and photoevaporation driven mass-loss after formation. We perform an extensive parameter study evaluating a wide range of disc properties and initial locations of the embryo. We find that due to the change in dust properties at the water ice line, rocky cores form typically with ∼3 M⊕ and have a maximum mass of ∼5 M⊕, while icy cores peak at ∼10 M⊕, with masses lower than 5 M⊕ being scarce. When neglecting the gaseous envelope, the formed rocky and icy cores account naturally for the two peaks of the Kepler size distribution. The presence of massive envelopes yields planets more massive than ∼10 M⊕ with radii above 4 R⊕. While the first peak of the Kepler size distribution is undoubtedly populated by bare rocky cores, as shown extensively in the past, the second peak can host half-rock–half-water planets with thin or non-existent H-He atmospheres, as suggested by a few previous studies. Some additional mechanisms inhibiting gas accretion or promoting envelope mass-loss should operate at short orbital periods to explain the presence of ∼10–40 M⊕ planets falling in the second peak of the size distribution.