Central to the concept of ecological speciation is the evolution of ecotypes, i.e. groups of individuals occupying different ecological niches. However, the mechanisms behind the first step of separation, the switch of individuals into new niches, are unclear. One long-standing hypothesis, which was proposed for insects but never tested, is that early learning causes new ecological preferences, leading to a switch into a new niche within one generation. Here, we show that a host switch occurred within a parasitoid wasp, which is associated with the ability for early learning and the splitting into separate lineages during speciation. Lariophagus distinguendus consists of two genetically distinct lineages, most likely representing different species. One attacks drugstore beetle larvae (Stegobium paniceum (L.)), which were probably the ancestral host of both lineages. The drugstore beetle lineage has an innate host preference that cannot be altered by experience. In contrast, the second lineage is found on Sitophilus weevils as hosts and changes its preference by early learning. We conclude that a host switch has occurred in the ancestor of the second lineage, which must have been enabled by early learning. Because early learning is widespread in insects, it might have facilitated ecological divergence and associated speciation in this hyperdiverse group.