The Gemini Planet Imager (GPI) is a complex optical system designed to directly detect the self-emission of young planets within two arcseconds of their host stars. After suppressing the starlight with an advanced AO system and apodized coronagraph, the dominant residual contamination in the focal plane are speckles from the atmosphere and optical surfaces. Since speckles are diffractive in nature their positions in the field are strongly wavelength dependent, while an actual companion planet will remain at fixed separation. By comparing multiple images at different wavelengths taken simultaneously, we can freeze the speckle pattern and extract the planet light adding an order of magnitude of contrast. To achieve a bandpass of 20%, sufficient to perform speckle suppression, and to observe the entire two arcsecond field of view at diffraction limited sampling, we designed and built an integral field spectrograph with extremely low wavefront error and almost no chromatic aberration. The spectrograph is fully cryogenic and operates in the wavelength range 1 to 2.4 microns with five selectable filters. A prism is used to produce a spectral resolution of 45 in the primary detection band and maintain high throughput. Based on the OSIRIS spectrograph at Keck, we selected to use a lenslet-based spectrograph to achieve an rms wavefront error of approximately 25 nm. Over 36,000 spectra are taken simultaneously and reassembled into image cubes that have roughly 192x192 spatial elements and contain between 11 and 20 spectral channels. The primary dispersion prism can be replaced with a Wollaston prism for dual polarization measurements. The spectrograph also has a pupil-viewing mode for alignment and calibration.