def valid_history(history, expected_count, expected_messages=None):
    """Checks if history is valid"""
    expected_messages = expected_messages or []
    if len(history) != expected_count:
        return False
    for i, value in enumerate(expected_messages):
        if history[i]["type"] != value:
            return False
    return True