Skip to main content
Requires version 0.8.61 or greater.
Callbacks
Callback blocks are part of your pipeline but don’t run as individual steps in your pipeline like data loader blocks, transformer blocks, etc. However, callback blocks are associated to other blocks within the pipeline (e.g. data loaders, transformers, data exporters, etc). After those other blocks are executed, any associated callback blocks will also be executed depending on the status of the parent block.

Example

  1. Your pipeline has the following blocks:
    1. load_data_from_api (data loader)
    2. clean_column_names (transformer)
    3. save_data (data exporter)
  2. Then, you add the following callback block to your pipeline:
  3. You associate the above callback block to the following 2 blocks:
    • load_data_from_api (data loader)
    • clean_column_names (transformer)
  4. When you run the pipeline, it’ll execute the load_data_from_api (data loader) block first. It successfully completes. Then, the callback block will run and execute the function hello_world.
  5. Next, the pipeline will execute the clean_column_names (transformer) block. It fails due to some error. The callback block will run and execute the function alert_me.

How to add callbacks to your pipeline

  1. Create a new pipeline or open an existing pipeline.
  2. Edit the pipeline.
  3. On the right side of the page, click the Add-ons icon in the navigation. If you don’t see it, try expanding the right area of the page.
  4. Click the button Callbacks.
  5. Click the button + Callback block.
  6. In the callback block’s code, add a function and decorate it with @callback. You can have as many functions in a callback block as you want.
  7. Use @callback or @callback('success') to decorate a function that should only run when the parent block successfully completes.
  8. Use @callback('failure') to decorate a function that should only run when the parent block fails.

Supported callbacks

success

Execute callback function when block runs successfully.

failure

Execute callback function when block fails.

cancelled

Available in versions >= 0.9.77.
Execute callback function when block is cancelled (manually or by system interruption).

Mage Pro callback features

Pipeline-level callbacks

In Mage Pro, callback blocks can be attached to the pipeline itself instead of only to individual parent blocks. Pipeline-level callbacks run once after a pipeline run reaches one of these terminal statuses:
  • completed
  • failed
  • cancelled
Block-level callbacks continue to run from their parent block status. Pipeline-level callbacks are separate and wait for the full pipeline run outcome. To configure pipeline-level callbacks in the editor:
  1. Open the pipeline editor.
  2. Create at least one callback block from the Add-ons panel.
  3. In the callback block footer selector, choose the current pipeline UUID under Pipeline.
The selector groups targets under Pipeline and Blocks. The Pipeline group contains only the current pipeline UUID. If a callback block still uses block-level-only function signatures with required positional block input, such as def success_callback(parent_block_data, **kwargs), the editor still shows the current pipeline target. Saving that pipeline target shows a compatibility error because pipeline callbacks do not pass parent_block_data or other block inputs. To use the callback at the pipeline level, make block input arguments optional, for example def success_callback(parent_block_data=None, **kwargs), use documented pipeline context keyword arguments, or use a kwargs-only signature such as def success_callback(**kwargs). The original required-argument signature remains valid for block-level callbacks. You can also configure the same behavior in the pipeline metadata.yaml file or through the pipeline create and update API:
The association controls whether the callback block runs once for the pipeline. The callback block code controls terminal-status-specific behavior by defining matching callback functions:
Pipeline-level callbacks receive a safe pipeline-run context instead of upstream block output. Available keyword arguments are intentionally minimal: Use pipeline_run_id with existing authorized APIs or models if callback code needs additional pipeline-run details. Run details show each dispatched pipeline callback as a block run with its status and any user-code error captured by the normal block-run execution path. Callback statuses can be initial, queued, running, completed, or failed.

Advanced parameters

In Mage Pro, you can pass additional parameters to the @callback decorator.

fetch_inputs

By default, Mage will fetch upstream inputs before executing a callback. If your callback doesn’t need input data (e.g. sending alerts, posting logs), you can disable input fetching to improve performance.
  • fetch_inputs=True (default) → Fetch parent block inputs before executing callback
  • fetch_inputs=False → Skip fetching inputs, speeding up execution

Positional arguments

The 1st positional argument is the data output of the callback block’s parent block.
Positional arguments are only supported in Mage versions >= 0.8.81.

Keyword arguments

Here are the keyword arguments that are available in each callback function:

__input keyword argument

For versions >= 0.8.81.
If your callback function is defined to only accept keyword arguments and no positional arguments, you can access the callback block’s parent block’s data output by accessing the key labeled __input in the callback function’s keyword arguments. The value of the __input key is a dictionary. The keys in that dictionary are named after the callback block’s parent block’s UUID. For example, if the callback block’s parent block’s UUID is load_api_data, then this is how you would access load_api_data’s data output:

Data integration pipelines only

The following keyword arguments are only available in callback functions with parent blocks in a data integration pipelines:

Legacy

For versions < 0.8.61.

Adding a callback to your block

  1. Create a block as normal.
  2. In the top right of the block, click the triple dot icon which should open up a menu with more options. Click “Add callback”.
  3. You should now see another code editor under the main editor for your block. Block callback
  4. You can add an on_success callback and/or a on_failure callback. These callbacks will run after your block run completes or fails. When you run your block in the pipeline edit page, it will also run the callback depending on the status of the block.
  5. The callback function will be passed your pipeline’s runtime variables as keyword arguments, so you can use the same kwargs.get('<variable>') syntax in your callback.