The chirp fiber bragg grating (CFBG) created using the substrate-straining technique requires an invariable center wavelength when subjected to a temperature change. To accomplish this task, a secondary substrate is attached to one side of the main substrate in a bimetallic beam configuration, used as a thermal compensator. However, when the temperature changes, the strain along the CFBG is not uniform because the main substrate is not prismatic. A systematic method based on the shape optimization technique was developed to determine the geometry of the second substrate for temperature compensation. The results show that the second substrate, determined using shape optimization, can produce the required strain distribution to compensate for the center wavelength shift in a CFBG due to temperature changes. Although the thermal compensation of a CFBG demonstrated here is simple, it should be emphasized that this methodology can be easily applied to determine the substrate profile for thermal compensation in other FBG-bonded substrates with nonuniform cross sections.