def flatten_doc(doc, return_fields, exceptions=None):
    """
    Parameters
    ----------
    doc : dict
        The document in the Solr response.
    return_fields : string
        A string of comma-separated field names.
    exceptions : list, optional
        A list of names of fields that should not be flattened.

    Flattens single-item list fields returned by Solr.
    """
    if type(doc) is not dict:
            raise ValueError('Document must be a dictionary.')
            
    for field in return_fields.split(","):
        if exceptions is not None and field in exceptions:
            continue
        elif field not in doc:
            continue
        else:
            doc[field] = doc[field][0] if len(doc[field]) == 1 else doc[field]
    return doc