ABSTRACT Planets, embedded in their natal discs, harbour hot envelopes. When pebbles are accreted by these planets, the contained volatile components may sublimate, enriching the envelope and potentially changing its thermodynamical properties. However, the envelopes of embedded planets actively exchange material with the disc, which would limit the buildup of a vapour-rich atmosphere. To properly investigate these processes, we have developed a new phase change module to treat the sublimation process with hydrodynamical simulations. Combined with the recently developed multidust fluid approach, we conduct 2D self-consistent hydrodynamic simulations to study how pebble sublimation influences the water content of super-Earths and sub-Neptunes. We find the extent and the amount of vapour that a planet is able to hold on to is determined by the relative size of the sublimation front and the atmosphere. When the sublimation front lies far inside the atmosphere, vapour tends to be locked deep in the atmosphere and keeps accumulating through a positive feedback mechanism. On the other hand, when the sublimation front exceeds the (bound) atmosphere, the ice component of incoming pebbles can be fully recycled and the vapour content reaches a low, steady value. Low disc temperature, small planet mass, and high pebble flux (omitting accretion heating by pebbles) render the planet atmosphere vapour-rich while the reverse changes render it vapour-poor. The phase change module introduced here can in future studies also be employed to model the chemical composition of the gas in the vicinity of accreting planets and around snowlines.