The functionalization of the Si(001) surface by pyrrole and polypyrrole is investigated by means of first-principles pseudopotential calculations. We find dissociative reactions, leading to the partial fragmentation of the molecule, to be energetically most favored for pyrrole adsorption. The lowest energy configuration for monolayer coverage is characterized by pyrrole rings bonded to the surface via Si-N linkage. In co-existence with alternative adsorption geometries where both N and C are bonded to the surface, this structure accounts very well for the available experimental data. Chemisorption of pyrrole is found to effectively passivate the Si(001) surface, irrespective of the details of the adsorption geometry. The formation of well-ordered polypyrrole structures on Si(001) may require chemical modifications of the polypyrrole chains in order to accommodate the lattice mismatch-induced strain.