import ast


def create_udf_field(udf_input: str):
    """
    Converts the given string with udf keys and values to a valid dictionary for the query.

    Args:
        udf_input: the string representing the udf values as given by the user.

    Returns:
        A dictionary where every key is the udf_field key and the value given by the user.
    """
    if not udf_input:
        return {}
    try:
        if udf_input[0] == '{' and udf_input[-1] == '}':  # check if the user entered a dict as the value
            return ast.literal_eval(udf_input)

        fields = udf_input.split(',')
        udf_dict = {}
        for field in fields:
            if field:
                field_key_value = field.split(':')
                if field_key_value[0] and field_key_value[1]:
                    udf_dict[field_key_value[0]] = field_key_value[1]
                else:
                    raise Exception('Invalid input')
        return udf_dict
    except Exception:
        raise Exception('Illegal udf fields format. Input format should be a string of key and value separated by : '
                        'Multiple key;value pairs can be given, separated with a comma')