Hooks
zenml.hooks
(Lifecycle) Hooks for steps and pipelines.
Attributes
__all__ = ['alerter_success_hook', 'alerter_failure_hook', 'run_hook']
module-attribute
Functions:
alerter_failure_hook(exception: BaseException) -> None
Standard failure hook that executes after step fails.
This hook uses any BaseAlerter that is configured within the active stack to post a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
Original exception that lead to step failing. |
required |
Source code in src/zenml/hooks/alerter_hooks.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
alerter_success_hook() -> None
Standard success hook that executes after step finishes successfully.
This hook uses any BaseAlerter that is configured within the active stack to post a message.
Source code in src/zenml/hooks/alerter_hooks.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
run_hook(func: Union[Callable[..., Any], Source], *args: Any, kwargs: Optional[Dict[str, Any]] = None, name: Optional[str] = None, hook_type: HookType = HookType.CUSTOM, store_return: bool = False, track: bool = True) -> Any
Run a hook, optionally recording the invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Union[Callable[..., Any], Source]
|
The hook function or a source to load it from. |
required |
*args
|
Any
|
Positional arguments for the hook function. |
()
|
kwargs
|
Optional[Dict[str, Any]]
|
Keyword arguments for the hook function. |
None
|
name
|
Optional[str]
|
Custom event name for the invocation. |
None
|
hook_type
|
HookType
|
Type of the hook invocation. |
CUSTOM
|
store_return
|
bool
|
Whether to materialize the return value as outputs. |
False
|
track
|
bool
|
Whether to record the invocation. |
True
|
Raises:
| Type | Description |
|---|---|
BaseException
|
Any exception raised by the hook function. |
Returns:
| Type | Description |
|---|---|
Any
|
The return value of the hook function. |
Source code in src/zenml/hooks/execution.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
Modules
alerter_hooks
Functionality for standard hooks.
Classes
Functions:
alerter_failure_hook(exception: BaseException) -> None
Standard failure hook that executes after step fails.
This hook uses any BaseAlerter that is configured within the active stack to post a message.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
exception
|
BaseException
|
Original exception that lead to step failing. |
required |
Source code in src/zenml/hooks/alerter_hooks.py
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | |
alerter_success_hook() -> None
Standard success hook that executes after step finishes successfully.
This hook uses any BaseAlerter that is configured within the active stack to post a message.
Source code in src/zenml/hooks/alerter_hooks.py
63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 | |
execution
Hook execution.
Classes
Functions:
run_hook(func: Union[Callable[..., Any], Source], *args: Any, kwargs: Optional[Dict[str, Any]] = None, name: Optional[str] = None, hook_type: HookType = HookType.CUSTOM, store_return: bool = False, track: bool = True) -> Any
Run a hook, optionally recording the invocation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Union[Callable[..., Any], Source]
|
The hook function or a source to load it from. |
required |
*args
|
Any
|
Positional arguments for the hook function. |
()
|
kwargs
|
Optional[Dict[str, Any]]
|
Keyword arguments for the hook function. |
None
|
name
|
Optional[str]
|
Custom event name for the invocation. |
None
|
hook_type
|
HookType
|
Type of the hook invocation. |
CUSTOM
|
store_return
|
bool
|
Whether to materialize the return value as outputs. |
False
|
track
|
bool
|
Whether to record the invocation. |
True
|
Raises:
| Type | Description |
|---|---|
BaseException
|
Any exception raised by the hook function. |
Returns:
| Type | Description |
|---|---|
Any
|
The return value of the hook function. |
Source code in src/zenml/hooks/execution.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 | |
run_lifecycle_hook(hook_source: Optional[Source], hook_type: HookType, optional_args: Optional[Tuple[Any, ...]] = None) -> None
Run a configured lifecycle hook, recording it and swallowing errors.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook_source
|
Optional[Source]
|
The source of the hook function, or None to do nothing. |
required |
hook_type
|
HookType
|
The type of the lifecycle hook. |
required |
optional_args
|
Optional[Tuple[Any, ...]]
|
Candidate arguments offered to the hook in order. As many leading arguments as the hook signature accepts are bound. |
None
|
Source code in src/zenml/hooks/execution.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 | |
setup_hook_logging_context(hook_type: HookType, name: Optional[str]) -> Optional[LoggingContext]
Setup a logging context that captures a hook's output.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook_type
|
HookType
|
The type of the hook invocation. |
required |
name
|
Optional[str]
|
The resolved name of the hook invocation. |
required |
Returns:
| Type | Description |
|---|---|
Optional[LoggingContext]
|
The logging context. |
Source code in src/zenml/hooks/execution.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
Modules
recording
Hook invocation recording.
Classes
Functions:
record_hook_invocation(name: Optional[str] = None, hook_type: HookType = HookType.CUSTOM, source: Optional[str] = None, outputs: Optional[Dict[str, Any]] = None, start_time: Optional[datetime] = None, end_time: Optional[datetime] = None, status: ExecutionStatus = ExecutionStatus.COMPLETED, exception_info: Optional[ExceptionInfo] = None, logs_id: Optional[UUID] = None) -> HookInvocationResponse
Record a hook invocation for an already-completed event.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
name
|
Optional[str]
|
Custom event name for the invocation. |
None
|
hook_type
|
HookType
|
Type of the hook invocation. |
CUSTOM
|
source
|
Optional[str]
|
Resolved source of the hook function. |
None
|
outputs
|
Optional[Dict[str, Any]]
|
Output values to materialize as artifacts. |
None
|
start_time
|
Optional[datetime]
|
Start time of the event. |
None
|
end_time
|
Optional[datetime]
|
End time of the event. |
None
|
status
|
ExecutionStatus
|
Terminal status of the event. |
COMPLETED
|
exception_info
|
Optional[ExceptionInfo]
|
Exception information when the event failed. |
None
|
logs_id
|
Optional[UUID]
|
ID of the logs entry to link to the invocation. |
None
|
Returns:
| Type | Description |
|---|---|
HookInvocationResponse
|
The recorded hook invocation. |
Source code in src/zenml/hooks/recording.py
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 | |
Modules
validation
Hook resolution and argument validation.
Classes
Functions:
bind_optional_hook_args(func: Callable[..., Any], candidate_args: Tuple[Any, ...]) -> Tuple[Any, ...]
Binds as many leading candidate arguments as the signature accepts.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
func
|
Callable[..., Any]
|
The hook function. |
required |
candidate_args
|
Tuple[Any, ...]
|
Candidate arguments in order. |
required |
Returns:
| Type | Description |
|---|---|
Tuple[Any, ...]
|
The leading candidate arguments the signature can accept. |
Source code in src/zenml/hooks/validation.py
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
resolve_and_validate_hook(hook: Union[HookSpecification, InitHookSpecification], *args: Any, kwargs: Optional[Dict[str, Any]] = None) -> Tuple[Source, Dict[str, Any]]
Resolve a hook and validate it can be called with the given arguments.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hook
|
Union[HookSpecification, InitHookSpecification]
|
Hook function or source. |
required |
*args
|
Any
|
Positional arguments offered to the hook. Only the leading arguments the signature accepts are bound and validated. |
()
|
kwargs
|
Optional[Dict[str, Any]]
|
Keyword arguments passed to the hook. |
None
|
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
HookValidationException
|
If the hook cannot accept the arguments. |
Returns:
| Type | Description |
|---|---|
Tuple[Source, Dict[str, Any]]
|
The hook source and the validated, JSON-safe keyword arguments. |
Source code in src/zenml/hooks/validation.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 | |