馃敶 Required Information
Is your feature request related to a specific problem?
Context exposes useful execution metadata such as agent_name, node,
node_path, run_id, and function_call_id, but it does not expose which
lifecycle hook is currently being executed.
This makes reusable cross-cutting code harder to implement. A shared security,
audit, or telemetry component invoked from several plugin and agent callbacks
must receive a manually maintained string or enum from every callback in order
to distinguish before_agent, before_model, after_tool, and other stages.
The existing fields cannot be used as a reliable substitute:
agent_name identifies the active agent, not the callback hook.
node and node_path identify the workflow node.
function_call_id identifies a tool call, but not the lifecycle callback.
- Introspecting
__name__ is unreliable for decorators, partials, callable
objects, callback lists, and plugin methods.
This is especially relevant now that callback fields support callback lists
(#5583), because generic wrappers may run from several hook families.
Describe the Solution You'd Like
Expose stable, read-only callback invocation metadata while a callback or
plugin hook is running. At minimum, the context should expose the lifecycle
hook kind. Optionally, it could also expose the owner and callable identity.
For example:
class CallbackHook(StrEnum):
BEFORE_AGENT = "before_agent"
AFTER_AGENT = "after_agent"
BEFORE_MODEL = "before_model"
AFTER_MODEL = "after_model"
BEFORE_TOOL = "before_tool"
AFTER_TOOL = "after_tool"
ON_MODEL_ERROR = "on_model_error"
ON_TOOL_ERROR = "on_tool_error"
@dataclass(frozen=True)
class CallbackInvocationInfo:
hook: CallbackHook
owner: Literal["plugin", "agent", "model", "tool"]
callback_name: str | None = None
context.callback_info # CallbackInvocationInfo | None
The exact API is open for discussion. A stable hook value alone would solve
the primary use case; callback_name can remain optional because not every
callable has a meaningful name.
The metadata should describe the callback that is currently executing. It
should not describe a future Agent-level callback while a Plugin-level hook is
running, and it should not alter the documented plugin/callback execution
order.
Impact on your work
This would make shared safety-policy, audit, and telemetry services easier to
reuse without manually propagating lifecycle-stage strings through every
callback adapter. It would also reduce the risk of recording or applying the
wrong policy stage after callbacks are reordered or composed.
There is no hard delivery deadline.
Willingness to contribute
No current commitment, but I can help validate the proposed behavior and test
cases.
馃煛 Recommended Information
Describe Alternatives You've Considered
- Pass a hook enum explicitly from every callback to shared code. This works,
but duplicates framework metadata and can drift from the actual hook.
- Infer the stage from
agent_name, node, or function_call_id. These fields
identify other execution concepts and are ambiguous.
- Inspect the Python call stack or callable
__name__. This is brittle with
decorators, callback lists, partials, callable objects, and plugin methods.
- Create one dedicated service method per callback hook. This avoids the
ambiguity but adds repetitive adapters for cross-cutting concerns.
Proposed API / Implementation
Set callback metadata immediately before invoking each plugin or object-level
callback, and restore or clear it afterwards. If mutating the shared Context
would be unsafe for nested execution, an immutable callback-scoped context view
or an additional callback invocation argument would provide the same contract.
Tests should cover:
- Plugin-level and object-level hooks.
- Callback lists and short-circuiting.
- Nested agent/workflow execution.
- Async callbacks and concurrent invocations.
- Decorated functions, partials, and callable objects.
Additional Context
Related but distinct issues:
馃敶 Required Information
Is your feature request related to a specific problem?
Contextexposes useful execution metadata such asagent_name,node,node_path,run_id, andfunction_call_id, but it does not expose whichlifecycle hook is currently being executed.
This makes reusable cross-cutting code harder to implement. A shared security,
audit, or telemetry component invoked from several plugin and agent callbacks
must receive a manually maintained string or enum from every callback in order
to distinguish
before_agent,before_model,after_tool, and other stages.The existing fields cannot be used as a reliable substitute:
agent_nameidentifies the active agent, not the callback hook.nodeandnode_pathidentify the workflow node.function_call_ididentifies a tool call, but not the lifecycle callback.__name__is unreliable for decorators, partials, callableobjects, callback lists, and plugin methods.
This is especially relevant now that callback fields support callback lists
(#5583), because generic wrappers may run from several hook families.
Describe the Solution You'd Like
Expose stable, read-only callback invocation metadata while a callback or
plugin hook is running. At minimum, the context should expose the lifecycle
hook kind. Optionally, it could also expose the owner and callable identity.
For example:
The exact API is open for discussion. A stable
hookvalue alone would solvethe primary use case;
callback_namecan remain optional because not everycallable has a meaningful name.
The metadata should describe the callback that is currently executing. It
should not describe a future Agent-level callback while a Plugin-level hook is
running, and it should not alter the documented plugin/callback execution
order.
Impact on your work
This would make shared safety-policy, audit, and telemetry services easier to
reuse without manually propagating lifecycle-stage strings through every
callback adapter. It would also reduce the risk of recording or applying the
wrong policy stage after callbacks are reordered or composed.
There is no hard delivery deadline.
Willingness to contribute
No current commitment, but I can help validate the proposed behavior and test
cases.
馃煛 Recommended Information
Describe Alternatives You've Considered
but duplicates framework metadata and can drift from the actual hook.
agent_name,node, orfunction_call_id. These fieldsidentify other execution concepts and are ambiguous.
__name__. This is brittle withdecorators, callback lists, partials, callable objects, and plugin methods.
ambiguity but adds repetitive adapters for cross-cutting concerns.
Proposed API / Implementation
Set callback metadata immediately before invoking each plugin or object-level
callback, and restore or clear it afterwards. If mutating the shared
Contextwould be unsafe for nested execution, an immutable callback-scoped context view
or an additional callback invocation argument would provide the same contract.
Tests should cover:
Additional Context
Related but distinct issues:
the identity of an already executing callback/plugin hook.