lifecycle.GracefulErrorAdapter

class hamilton.lifecycle.default.GracefulErrorAdapter(error_to_catch: Type[Exception], sentinel_value: Any = None)

Gracefully handles errors in a graph’s execution. This allows you to proceed despite failure, dynamically pruning branches. While it still runs every node, it replaces them with no-ops if any upstream required dependencies fail (including optional dependencies).

__init__(error_to_catch: Type[Exception], sentinel_value: Any = None)

Initializes the adapter. Allows you to customize the error to catch (which exception your graph will throw to indicate failure), as well as the sentinel value to use in place of a node’s result if it fails (this defaults to None).

Note that this is currently only compatible with the dict-based result builder (use at your own risk with pandas series, etc…).

Be careful using None as the default – feel free to replace it with a sentinel value of your choice (this could negatively impact your graph’s execution if you actually do intend to use None return values).

You can use this as follows:

# my_module.py
# custom exception
class DoNotProceed(Exception):
    pass

def wont_proceed() -> int:
    raise DoNotProceed()

def will_proceed() -> int:
    return 1

def never_reached(wont_proceed: int) -> int:
    return 1  # this should not be reached

dr = (
    driver.Builder()
    .with_modules(my_module)
    .with_adapters(
        default.GracefulErrorAdapter(
            error_to_catch=DoNotProceed,
            sentinel_value=None
        )
    )
    .build()
)
dr.execute(["will_proceed", "never_reached"])  # will return {'will_proceed': 1, 'never_reached': None}

Note you can customize the error you want it to fail on and the sentinel value to use in place of a node’s result if it fails.

Parameters:
  • error_to_catch – The error to catch

  • sentinel_value – The sentinel value to use in place of a node’s result if it fails