def create_valid_result_smaller(result_key = 'loss'):
  """Creates a compare_fn with the correct function signature.

  Args:
    result_key: The key to identify the result within best_eval_result and
      current_eval_result. Possible keys are `loss` or any other key which is
      specified in the models metrics dict.

  Returns:
    A valid_result_smaller, a compare_fn with the only allowed function
    signature.
  """

  def valid_result_smaller(best_eval_result, current_eval_result):
    """Compares two evaluation results.

    This implementation is a robustified version of the default _result_smaller
    implementation which had the requirement that 'result' is available in all
    results which does not necessarily has to hold true in case there are
    multiple summary writers.

    Args:
      best_eval_result: best eval metrics.
      current_eval_result: current eval metrics.

    Returns:
      True if the result of current_eval_result is smaller or best_eval_result
      was
        invalid so far; otherwise, False.
    """
    if not best_eval_result or result_key not in best_eval_result:
      # This effectively means all entries so far did not have the result_key.
      # Therefore, we can simply use the current_eval_result since the
      # the best_eval_result so far is invalid.
      return True

    if not current_eval_result or result_key not in current_eval_result:
      return False

    return best_eval_result[result_key] > current_eval_result[result_key]

  return valid_result_smaller