Does hamilton currently have a functionality of ex...
# contribute
i
Does hamilton currently have a functionality of external callbacks following successful node materialization? The use case is hamilton being called from another tool (e.g. airflow), and whenever data is saved to disk we want to generate an event in the caller (e.g. "asset event" in airflow lingo). I can think think of multiple ways to achieve "event-like" functionality, but wondering what existing mechanism, if any, can be used for something like this.
s
There isn’t a materializer-specific callback today, but the existing
NodeExecutionHook
should work for this. Materializers execute as regular Hamilton nodes tagged with
hamilton.data_saver=True
, and the post-node hook receives
success
plus the metadata returned by
DataSaver.save_data()
. So you can register an adapter with
Builder.with_adapters(...)
, filter for successful saver nodes, and emit the Airflow asset event there. Roughly:
Copy code
from hamilton.lifecycle import api

class AssetEventHook(api.NodeExecutionHook):
    def run_before_node_execution(self, **kwargs):
        pass

    def run_after_node_execution(
        self, *, node_name, node_tags, success, result, **kwargs
    ):
        if success and node_tags.get("hamilton.data_saver") is True:
            emit_asset_event(
                materializer_id=node_name,
                metadata=result,
            )
Our OpenLineage adapter already uses this general pattern for successful loader/saver nodes. Putting the event emission directly in a custom
DataSaver.save_data()
would also work. I’d lean toward the lifecycle hook when this is an orchestration/observability side effect, since it keeps Airflow-specific behavior separate from storage. A custom saver makes more sense if saving and publishing the event intentionally form one operation. One caveat: the event is emitted after the save succeeds, but it isn’t transactional with the save. If event publication fails, Hamilton can’t roll back the saved data, so the callback should ideally be idempotent/retry-safe.
but we could also look to add something to the materializer hierarchy itself
i
Thanks for the overview! A custom datasaver sounds reasonable. Follow-up question: suppose we have a dag and we have N nodes with side effects but K<=N we want to generate Airflow events for. How should we pass to the builder the nodes and dataset URIs we want them to produce? Via configs? p.s. This thread was started in response to a suggestion raised on the Airflow provider discussion.
s
IIUC, all N nodes are materialized but only K should emit Airflow asset events? Could do something like this: the lifecycle hook can be initialized with a mapping for just those K materializer nodes:
Copy code
asset_uris = {
    "save_orders": "s3://bucket/orders.parquet",
    "save_customers": "s3://bucket/customers.parquet",
}

class AirflowAssetHook(api.NodeExecutionHook):
    def __init__(self, asset_uris):
        self.asset_uris = asset_uris

    def run_before_node_execution(self, **kwargs):
        pass

    def run_after_node_execution(
        self, *, node_name, node_tags, success, result, **kwargs
    ):
        uri = self.asset_uris.get(node_name)
        if (
            success
            and node_tags.get("hamilton.data_saver") is True
            and uri is not None
        ):
            emit_airflow_asset_event(uri=uri, metadata=result)
Then construct the driver with that provider configuration:
Copy code
dr = (
    driver.Builder()
    .with_modules(my_dataflow)
    .with_adapters(AirflowAssetHook(asset_uris))
    .with_materializers(*all_n_materializers)
    .build()
)
All N materializers still save their outputs. The hook sees all successful saver executions, but only emits for the K whose materializer IDs appear in
asset_uris
. I’d key this by materializer ID rather than by the upstream computation node, because the event represents a particular persisted asset and one computation node could be saved to multiple destinations. The URI could also be taken from the saver’s returned metadata where available, but an explicit ID-to-URI mapping is probably clearer and more reliable for an Airflow provider?
👍 1