def _sort_key_max_confidence_sd(sample, labels):
    """Samples sort key by the maximum confidence_sd."""
    max_confidence_sd = float("-inf")
    for inference in sample["inferences"]:
        if labels and inference["label"] not in labels:
            continue
        confidence_sd = inference.get("confidence_sd", float("-inf"))
        if confidence_sd > max_confidence_sd:
            max_confidence_sd = confidence_sd
    return max_confidence_sd