Skip to main content

Overview

Mage allows you to reuse blocks across multiple pipelines within the same project, enabling you to build modular, maintainable data pipelines. When you reuse a block, any changes made to that block will automatically apply to all pipelines that use it.

Benefits of reusing blocks

  • Code efficiency - Write once, use many times
  • Consistency - Ensure the same logic is applied across all pipelines
  • Maintenance - Update logic in one place and it applies everywhere
  • Modularity - Build complex pipelines from smaller, reusable components
  • Testing - Test block logic once and reuse with confidence

Reuse Scenarios and Methods

ScenarioMethodDescriptionBest For
Within PipelineReplicate blocksRight-click to duplicate blocks in the same pipelineSimilar data sources, parallel processing, testing variations
Across PipelinesDrag & dropDrag blocks from file browser to pipeline canvasQuick reuse of existing blocks
Across PipelinesSearch barSearch and add blocks from all projectsFinding specific blocks across projects
Code ReuseUtility filesCreate shared Python modules and import themSmall functions, data processing utilities
Code ReuseBlock templatesCreate reusable templates with placeholdersCommon patterns with slight variations
Across ProjectsPro templatesUse Mage Pro’s template libraryOrganization-wide standardization
Across ProjectsExport/importExport blocks from one project to anotherSharing between different projects

How block sharing works

When you create a block in one pipeline, you can reference that same block in other pipelines. The block file exists in the project’s file system and can be imported by any pipeline within the same project.

Block sharing indicators

When viewing a pipeline, you’ll see indicators showing which blocks are shared:
  • Shared blocks display a symbol like <> 2 pipelines in the block header
  • Non-shared blocks show no additional symbols
  • The number indicates how many pipelines are using that block

Viewing pipelines using a block

To see which pipelines are using a specific block:
  1. Click on a block in the pipeline editor
  2. Open block settings - Click the settings icon in the right side panel
  3. View “Pipelines using this block” - See a list of all pipelines that reference this block
  4. Check project paths - See which project each pipeline belongs to
This helps you understand the impact of changes to shared blocks across your project.

Methods to reuse blocks

Drag and drop from file browser

This is the most intuitive way to reuse blocks across pipelines:
  1. Open the file browser on the Pipeline Editor page
  2. Navigate to the block you want to reuse in the file browser
  3. Click and hold the block file
  4. Drag the block file onto the center of the page (the Notebook area)
  5. Release the button and the block will be added to the pipeline

Drag blocks from the file browser and drop it on the pipeline

Supported block types:
  • callbacks, conditionals, custom, data_exporters, data_loaders
  • dbt, dbts, markdowns, scratchpads, sensors, transformers
Mage OSS: Version 0.9.64 or greater is required to add callback and conditional blocks from the file browser.Mage Pro: Callback and conditional blocks are supported by default in all versions.

Use the search block bar

Search for existing blocks across all your projects:
  1. Click in the search bar at the bottom of the pipeline editor
  2. Type keywords to search for existing blocks (e.g., “load titanic”)
  3. Browse the search results showing blocks from different projects
  4. Click on a block from the dropdown to add it to your pipeline
  5. The block will be added to your current pipeline

Search blocks across entire projects

Mage OSS: The “Add new block v2” feature must be enabled in project settings and is available in standard (batch) pipelines. If recently enabled, Mage must be restarted for blocks to be indexed.Mage Pro: This feature is enabled by default and available in all pipeline types.

Replicate blocks within a pipeline

To reuse a block within the same pipeline (create a copy):
  1. Click the triple dot icon in the top right corner of the block you want to replicate
  2. Select “Replicate block” from the dropdown menu
  3. Name the new replica block (optional)
  4. Customize the new block as needed for your specific use case
  5. Connect the blocks in your pipeline flow as required

Replicate the code of an existing block

Use cases for block replication:
  • Similar data sources - Load from different tables with similar structure
  • Multiple transformations - Apply similar logic to different data streams
  • Parallel processing - Process the same data with different parameters
  • Testing variations - Compare different approaches side by side
Learn more about replicating blocks.

Other ways to reuse code

Create utility files and import in blocks

For shared functions and logic that don’t need to be full blocks:
  1. Create a utility file in your project (e.g., default_repo/utils/data_processing.py)
  2. Write reusable functions in the utility file
  3. Import the functions in your blocks using standard Python imports
Example utility file:
# default_repo/utils/data_processing.py
import pandas as pd

def clean_column_names(df):
    """Clean column names by removing spaces and special characters."""
    df.columns = df.columns.str.replace(' ', '_').str.lower()
    return df

def validate_dataframe(df, required_columns):
    """Validate that dataframe has required columns."""
    missing_cols = set(required_columns) - set(df.columns)
    if missing_cols:
        raise ValueError(f"Missing columns: {missing_cols}")
    return True
Using in a block:
# In your data loader block
from default_repo.utils.data_processing import clean_column_names, validate_dataframe

@data_loader
def load_data():
    df = pd.read_csv('data.csv')
    df = clean_column_names(df)
    validate_dataframe(df, ['id', 'name', 'email'])
    return df
Learn more about importing custom Python code.

Use block templates

Create reusable block templates for common patterns:
  1. Navigate to Templates - Click “Templates” in the left navigation menu
  2. Create new template - Click “New block template” button
  3. Define template properties - Set template UUID, block type, and language
  4. Edit template content - Write the template code with placeholders
  5. Save template - Click “Save template” to create the template
Learn more about creating and using user-defined templates.

Reusing blocks across projects

Block templates across projects (Mage Pro)

Mage Pro feature: Mage Pro supports configuring a central template library that shares templates across projects, allowing you to create and share reusable block templates across multiple projects in your organization.
Creating block templates:
  1. Navigate to Templates - Click “Templates” in the left navigation menu
  2. Create new template - Click “New block template” button
  3. Define template properties - Set template UUID, block type, and language
  4. Edit template content - Write the template code with placeholders
  5. Save template - Click “Save template” to create the template
Using block templates:
  1. Open pipeline editor - Navigate to the pipeline where you want to add the block
  2. Add new block - Click the ”+” button to add a new block
  3. Browse templates - Select “Browse templates” from the block creation options
  4. Select template - Choose the template you want to use from the available templates
  5. Name the block - Enter a specific name for your new block instance

Cross-project block sharing

For organizations with multiple projects:
  1. Export blocks by downloading the block file from the source project
  2. Import blocks by uploading the block file in the file browser of the target project
  3. Maintain version control across project boundaries
  4. Share common utilities and configurations
Best practices for cross-project sharing:
  • Document dependencies - Clearly list required packages and configurations
  • Use relative imports - Make blocks portable across different project structures
  • Test thoroughly - Ensure blocks work in different project environments

Best practices for block reuse

Design reusable blocks

  • Make blocks generic - Avoid hardcoded values, use runtime variables instead
  • Clear naming - Use descriptive names that indicate the block’s purpose
  • Documentation - Add comments explaining what the block does and its inputs/outputs
  • Error handling - Include proper error handling for different use cases

Runtime variables for flexibility

Use runtime variables to make blocks adaptable across different pipelines:
# Instead of hardcoded values
database_name = "production_db"

# Use runtime variables
database_name = kwargs.get("database_name", "default_db")
Learn more about using runtime variables.

Block dependencies

When reusing blocks, consider their dependencies:
  • Data loaders - Ensure data sources are accessible from all pipelines
  • Transformers - Make sure input data format is consistent
  • Exporters - Verify output destinations are appropriate for each pipeline

Managing shared blocks

Viewing shared block usage

  1. Click on a shared block in any pipeline
  2. Look for the sharing indicator (<> X pipelines)
  3. Click the indicator to see which pipelines are using the block
  4. Review the usage to understand the block’s impact

Updating shared blocks

When you modify a shared block:
  1. Changes apply everywhere - All pipelines using the block will be affected
  2. Test thoroughly - Ensure changes work for all use cases
  3. Communicate changes - Notify team members about significant modifications
  4. Version control - Use Git to track changes and enable rollbacks if needed

Detaching blocks when needed

If you need to customize a shared block for a specific pipeline:
  1. Use the detach feature - See Detaching blocks guide
  2. Create a copy - This creates an independent version
  3. Customize freely - Make changes without affecting other pipelines

Common use cases

Data loading blocks

Reuse data loading logic across multiple pipelines:
# Shared data loader block
@data_loader
def load_customer_data():
    # Load customer data from database
    # Use runtime variables for database connection
    return df

Transformation blocks

Share common data transformations:
# Shared transformer block
@transformer
def clean_customer_data(df):
    # Standard customer data cleaning logic
    # Works with any customer dataset
    return cleaned_df

Export blocks

Reuse export logic for different destinations:
# Shared exporter block
@data_exporter
def export_to_warehouse(df):
    # Export to data warehouse
    # Use runtime variables for destination configuration
    pass
I