Fix BadKeyError exception for invalid strings.