def _pack_data(X, Y, metadata):
    """
    After modifying / preprocessing inputs,
    reformat the data in preparation for JSON serialization
    """
    if not any(metadata):
        # legacy list of list format is acceptable
        return list(zip(X, Y))

    else:
        # newer dictionary-based format is required in order to save metadata
            return [
                {
                    'data': x,
                    'target': y,
                    'metadata': meta
                }
                for x, y, meta in zip(X, Y, metadata)
            ]