Callbacks
Callback system for PyAgenity.
This module provides a comprehensive callback framework that allows users to define their own validation logic and custom behavior at key points in the execution flow:
- before_invoke: Called before AI/TOOL/MCP invocation for input validation and modification
- after_invoke: Called after AI/TOOL/MCP invocation for output validation and modification
- on_error: Called when errors occur during invocation for error handling and logging
The system is generic and type-safe, supporting different callback types for different invocation contexts.
Classes:
Name | Description |
---|---|
AfterInvokeCallback |
Abstract base class for after_invoke callbacks. |
BeforeInvokeCallback |
Abstract base class for before_invoke callbacks. |
CallbackContext |
Context information passed to callbacks. |
CallbackManager |
Manages registration and execution of callbacks for different invocation types. |
InvocationType |
Types of invocations that can trigger callbacks. |
OnErrorCallback |
Abstract base class for on_error callbacks. |
Functions:
Name | Description |
---|---|
register_after_invoke |
Register an after_invoke callback on the global callback manager. |
register_before_invoke |
Register a before_invoke callback on the global callback manager. |
register_on_error |
Register an on_error callback on the global callback manager. |
Attributes:
Name | Type | Description |
---|---|---|
AfterInvokeCallbackType |
|
|
BeforeInvokeCallbackType |
|
|
OnErrorCallbackType |
|
|
default_callback_manager |
|
|
logger |
|
Attributes¶
AfterInvokeCallbackType
module-attribute
¶
AfterInvokeCallbackType = Union[AfterInvokeCallback[Any, Any], Callable[[CallbackContext, Any, Any], Union[Any, Awaitable[Any]]]]
BeforeInvokeCallbackType
module-attribute
¶
BeforeInvokeCallbackType = Union[BeforeInvokeCallback[Any, Any], Callable[[CallbackContext, Any], Union[Any, Awaitable[Any]]]]
OnErrorCallbackType
module-attribute
¶
OnErrorCallbackType = Union[OnErrorCallback, Callable[[CallbackContext, Any, Exception], Union[Any | None, Awaitable[Any | None]]]]
Classes¶
AfterInvokeCallback
¶
Bases: ABC
Abstract base class for after_invoke callbacks.
Called after the AI model, tool, or MCP function is invoked. Allows for output validation and modification.
Methods:
Name | Description |
---|---|
__call__ |
Execute the after_invoke callback. |
Source code in pyagenity/utils/callbacks.py
70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
Functions¶
__call__
abstractmethod
async
¶
__call__(context, input_data, output_data)
Execute the after_invoke callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
CallbackContext
|
Context information about the invocation |
required |
|
T
|
The original input data that was sent |
required |
|
Any
|
The output data returned from the invocation |
required |
Returns:
Type | Description |
---|---|
Any | R
|
Modified output data (can be same type or different type) |
Raises:
Type | Description |
---|---|
Exception
|
If validation fails or modification cannot be performed |
Source code in pyagenity/utils/callbacks.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 |
|
BeforeInvokeCallback
¶
Bases: ABC
Abstract base class for before_invoke callbacks.
Called before the AI model, tool, or MCP function is invoked. Allows for input validation and modification.
Methods:
Name | Description |
---|---|
__call__ |
Execute the before_invoke callback. |
Source code in pyagenity/utils/callbacks.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
Functions¶
__call__
abstractmethod
async
¶
__call__(context, input_data)
Execute the before_invoke callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
CallbackContext
|
Context information about the invocation |
required |
|
T
|
The input data about to be sent to the invocation |
required |
Returns:
Type | Description |
---|---|
T | R
|
Modified input data (can be same type or different type) |
Raises:
Type | Description |
---|---|
Exception
|
If validation fails or modification cannot be performed |
Source code in pyagenity/utils/callbacks.py
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 |
|
CallbackContext
dataclass
¶
Context information passed to callbacks.
Methods:
Name | Description |
---|---|
__init__ |
|
Attributes:
Name | Type | Description |
---|---|---|
function_name |
str | None
|
|
invocation_type |
InvocationType
|
|
metadata |
dict[str, Any] | None
|
|
node_name |
str
|
|
Source code in pyagenity/utils/callbacks.py
36 37 38 39 40 41 42 43 |
|
CallbackManager
¶
Manages registration and execution of callbacks for different invocation types.
Handles before_invoke, after_invoke, and on_error callbacks for AI, TOOL, and MCP invocations.
Methods:
Name | Description |
---|---|
__init__ |
Initialize the CallbackManager with empty callback registries. |
clear_callbacks |
Clear callbacks for a specific invocation type or all types. |
execute_after_invoke |
Execute all after_invoke callbacks for the given context. |
execute_before_invoke |
Execute all before_invoke callbacks for the given context. |
execute_on_error |
Execute all on_error callbacks for the given context. |
get_callback_counts |
Get count of registered callbacks by type for debugging. |
register_after_invoke |
Register an after_invoke callback for a specific invocation type. |
register_before_invoke |
Register a before_invoke callback for a specific invocation type. |
register_on_error |
Register an on_error callback for a specific invocation type. |
Source code in pyagenity/utils/callbacks.py
137 138 139 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 190 191 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 280 281 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 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
Functions¶
__init__
¶
__init__()
Initialize the CallbackManager with empty callback registries.
Source code in pyagenity/utils/callbacks.py
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 |
|
clear_callbacks
¶
clear_callbacks(invocation_type=None)
Clear callbacks for a specific invocation type or all types.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType | None
|
The invocation type to clear, or None for all. |
None
|
Source code in pyagenity/utils/callbacks.py
301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 |
|
execute_after_invoke
async
¶
execute_after_invoke(context, input_data, output_data)
Execute all after_invoke callbacks for the given context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
CallbackContext
|
Context information about the invocation. |
required |
|
Any
|
The original input data sent to the invocation. |
required |
|
Any
|
The output data returned from the invocation. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The modified output data after all callbacks. |
Raises:
Type | Description |
---|---|
Exception
|
If any callback fails. |
Source code in pyagenity/utils/callbacks.py
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 |
|
execute_before_invoke
async
¶
execute_before_invoke(context, input_data)
Execute all before_invoke callbacks for the given context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
CallbackContext
|
Context information about the invocation. |
required |
|
Any
|
The input data to be validated or modified. |
required |
Returns:
Name | Type | Description |
---|---|---|
Any |
Any
|
The modified input data after all callbacks. |
Raises:
Type | Description |
---|---|
Exception
|
If any callback fails. |
Source code in pyagenity/utils/callbacks.py
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 |
|
execute_on_error
async
¶
execute_on_error(context, input_data, error)
Execute all on_error callbacks for the given context.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
CallbackContext
|
Context information about the invocation. |
required |
|
Any
|
The input data that caused the error. |
required |
|
Exception
|
The exception that occurred. |
required |
Returns:
Type | Description |
---|---|
Message | None
|
Message | None: Recovery value from callbacks, or None if not handled. |
Source code in pyagenity/utils/callbacks.py
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 |
|
get_callback_counts
¶
get_callback_counts()
Get count of registered callbacks by type for debugging.
Returns:
Type | Description |
---|---|
dict[str, dict[str, int]]
|
dict[str, dict[str, int]]: Counts of callbacks for each invocation type. |
Source code in pyagenity/utils/callbacks.py
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 |
|
register_after_invoke
¶
register_after_invoke(invocation_type, callback)
Register an after_invoke callback for a specific invocation type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType
|
The type of invocation (AI, TOOL, MCP). |
required |
|
AfterInvokeCallbackType
|
The callback to register. |
required |
Source code in pyagenity/utils/callbacks.py
176 177 178 179 180 181 182 183 184 185 186 |
|
register_before_invoke
¶
register_before_invoke(invocation_type, callback)
Register a before_invoke callback for a specific invocation type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType
|
The type of invocation (AI, TOOL, MCP). |
required |
|
BeforeInvokeCallbackType
|
The callback to register. |
required |
Source code in pyagenity/utils/callbacks.py
164 165 166 167 168 169 170 171 172 173 174 |
|
register_on_error
¶
register_on_error(invocation_type, callback)
Register an on_error callback for a specific invocation type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType
|
The type of invocation (AI, TOOL, MCP). |
required |
|
OnErrorCallbackType
|
The callback to register. |
required |
Source code in pyagenity/utils/callbacks.py
188 189 190 191 192 193 194 195 196 197 198 |
|
InvocationType
¶
OnErrorCallback
¶
Bases: ABC
Abstract base class for on_error callbacks.
Called when an error occurs during invocation. Allows for error handling and logging.
Methods:
Name | Description |
---|---|
__call__ |
Execute the on_error callback. |
Source code in pyagenity/utils/callbacks.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
Functions¶
__call__
abstractmethod
async
¶
__call__(context, input_data, error)
Execute the on_error callback.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
CallbackContext
|
Context information about the invocation |
required |
|
Any
|
The input data that caused the error |
required |
|
Exception
|
The exception that occurred |
required |
Returns:
Type | Description |
---|---|
Any | None
|
Optional recovery value or None to re-raise the error |
Raises:
Type | Description |
---|---|
Exception
|
If error handling fails or if the error should be re-raised |
Source code in pyagenity/utils/callbacks.py
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 |
|
Functions¶
register_after_invoke
¶
register_after_invoke(invocation_type, callback)
Register an after_invoke callback on the global callback manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType
|
The type of invocation (AI, TOOL, MCP). |
required |
|
AfterInvokeCallbackType
|
The callback to register. |
required |
Source code in pyagenity/utils/callbacks.py
353 354 355 356 357 358 359 360 361 362 363 |
|
register_before_invoke
¶
register_before_invoke(invocation_type, callback)
Register a before_invoke callback on the global callback manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType
|
The type of invocation (AI, TOOL, MCP). |
required |
|
BeforeInvokeCallbackType
|
The callback to register. |
required |
Source code in pyagenity/utils/callbacks.py
340 341 342 343 344 345 346 347 348 349 350 |
|
register_on_error
¶
register_on_error(invocation_type, callback)
Register an on_error callback on the global callback manager.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
|
InvocationType
|
The type of invocation (AI, TOOL, MCP). |
required |
|
OnErrorCallbackType
|
The callback to register. |
required |
Source code in pyagenity/utils/callbacks.py
366 367 368 369 370 371 372 373 374 |
|