def find_label_errors(
    records: Union[List[TextClassificationRecord], DatasetForTextClassification],
    sort_by: Union[str, SortBy] = "likelihood",
    metadata_key: str = "label_error_candidate",
    n_jobs: Optional[int] = 1,
    **kwargs,
) -> List[TextClassificationRecord]:
    """Finds potential annotation/label errors in your records using [cleanlab](https://github.com/cleanlab/cleanlab).

    We will consider all records for which a prediction AND annotation is available. Make sure the predictions were made
    in a holdout manner, that is you should only include records that were not used in the training of the predictor.

    Args:
        records: A list of text classification records
        sort_by: One of the three options
            - "likelihood": sort the returned records by likelihood of containing a label error (most likely first)
            - "prediction": sort the returned records by the probability of the prediction (highest probability first)
            - "none": do not sort the returned records
        metadata_key: The key added to the record's metadata that holds the order, if ``sort_by`` is not "none".
        n_jobs : Number of processing threads used by multiprocessing. If None, uses the number of threads
            on your CPU. Defaults to 1, which removes parallel processing.
        **kwargs: Passed on to `cleanlab.pruning.get_noise_indices` (cleanlab < 2.0)
            or `cleanlab.filter.find_label_issues` (cleanlab >= 2.0)

    Returns:
        A list of records containing potential annotation/label errors

    Raises:
        NoRecordsError: If none of the records has a prediction AND annotation.
        MissingPredictionError: If a prediction is missing for one of the labels.
        ValueError: If not supported kwargs are passed on, e.g. 'sorted_index_method'.

    Examples:
        >>> import argilla_v1 as rg
        >>> records = rg.load("my_dataset")
        >>> records_with_label_errors = find_label_errors(records)
    """
    import cleanlab

    if parse_version(cleanlab.__version__) < parse_version("2.0"):
        from cleanlab.pruning import get_noise_indices as find_label_issues
    else:
        from cleanlab.filter import find_label_issues

    if isinstance(sort_by, str):
        sort_by = SortBy(sort_by)

    # select only records with prediction and annotation
    records = [rec for rec in records if rec.prediction and rec.annotation]
    if not records:
        raise NoRecordsError("It seems that none of your records have a prediction AND annotation!")

    # check and update kwargs for get_noise_indices
    _check_and_update_kwargs(cleanlab.__version__, records[0], sort_by, kwargs)

    # construct "noisy" label vector and probability matrix of the predictions
    s, psx = _construct_s_and_psx(records)

    indices = find_label_issues(s, psx, n_jobs=n_jobs, **kwargs)

    records_with_label_errors = np.array(records)[indices].tolist()

    # add metadata
    if sort_by is not SortBy.NONE:
        for i, rec in enumerate(records_with_label_errors):
            rec.metadata[metadata_key] = i

    return records_with_label_errors
Finds potential annotation/label errors in your records using [cleanlab](https://github.com/cleanlab/cleanlab).

    We will consider all records for which a prediction AND annotation is available. Make sure the predictions were made
    in a holdout manner, that is you should only include records that were not used in the training of the predictor.

    Args:
        records: A list of text classification records
        sort_by: One of the three options
            - "likelihood": sort the returned records by likelihood of containing a label error (most likely first)
            - "prediction": sort the returned records by the probability of the prediction (highest probability first)
            - "none": do not sort the returned records
        metadata_key: The key added to the record's metadata that holds the order, if ``sort_by`` is not "none".
        n_jobs : Number of processing threads used by multiprocessing. If None, uses the number of threads
            on your CPU. Defaults to 1, which removes parallel processing.
        **kwargs: Passed on to `cleanlab.pruning.get_noise_indices` (cleanlab < 2.0)
            or `cleanlab.filter.find_label_issues` (cleanlab >= 2.0)

    Returns:
        A list of records containing potential annotation/label errors

    Raises:
        NoRecordsError: If none of the records has a prediction AND annotation.
        MissingPredictionError: If a prediction is missing for one of the labels.
        ValueError: If not supported kwargs are passed on, e.g. 'sorted_index_method'.

    Examples:
        >>> import argilla_v1 as rg
        >>> records = rg.load("my_dataset")
        >>> records_with_label_errors = find_label_errors(records)
    
argilla-v1/src/argilla_v1/labeling/text_classification/label_errors.py