def generate_output_filename(filename: str, extension=None):
    """
    >>> generate_output_filename('test')
    'test_out'
    >>> generate_output_filename('test', extension='csv')
    'test_out.csv'
    >>> generate_output_filename('test.csv')
    'test_out'
    >>> generate_output_filename('test2.tsv.csv')
    'test2_out'
    >>> generate_output_filename('test.csv', extension='csv')
    'test_out.csv'
    """
    assert extension is None or '.' not in extension

    filename = filename.split('.', maxsplit=1)[0] + '_out'

    return filename if extension is None else filename + '.' + extension