Hooks API¶
Claude Code-style lifecycle hooks run shell commands or Python handlers around tool
execution. Attach them via the hooks parameter on
create_deep_agent. See
Hooks for the conceptual overview.
Hook¶
pydantic_deep.capabilities.hooks.Hook
dataclass
¶
A hook definition that fires on tool lifecycle events.
Either command or handler must be provided (not both).
Command hooks run shell commands via SandboxProtocol.execute().
Handler hooks call async Python functions.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
event
|
HookEvent
|
Which lifecycle event triggers this hook. |
required |
command
|
str | None
|
Shell command to execute (receives HookInput JSON via stdin). |
None
|
handler
|
Callable[[HookInput], Awaitable[HookResult]] | None
|
Async Python function |
None
|
matcher
|
str | None
|
Regex pattern matched against tool_name. None matches all tools. |
None
|
timeout
|
int
|
Command execution timeout in seconds. |
30
|
background
|
bool
|
If True, hook runs as fire-and-forget (non-blocking). |
False
|
HookEvent¶
pydantic_deep.capabilities.hooks.HookEvent
¶
HookInput¶
pydantic_deep.capabilities.hooks.HookInput
dataclass
¶
Data passed to hook commands/handlers as JSON.
Command hooks receive this as JSON via stdin piping. Python handlers receive this as a dataclass instance.
HookResult¶
pydantic_deep.capabilities.hooks.HookResult
dataclass
¶
Result from a hook execution.
For PRE_TOOL_USE: allow=False denies the tool call. For POST_TOOL_USE: modified_result replaces the tool output.
HooksCapability¶
pydantic_deep.capabilities.hooks.HooksCapability
dataclass
¶
Bases: AbstractCapability[Any]
Capability that executes hooks on tool lifecycle events.
Maps tool events to shell commands (via execute()) or Python handlers, following Claude Code's hook conventions: - PRE_TOOL_USE: before tool execution, can deny - POST_TOOL_USE: after successful tool execution - POST_TOOL_USE_FAILURE: after failed tool execution
before_tool_execute(ctx, *, call, tool_def, args)
async
¶
Run PRE_TOOL_USE hooks. First deny raises SkipToolExecution.
after_tool_execute(ctx, *, call, tool_def, args, result)
async
¶
Run POST_TOOL_USE hooks. Can modify result.
on_tool_execute_error(ctx, *, call, tool_def, args, error)
async
¶
Run POST_TOOL_USE_FAILURE hooks.
before_run(ctx)
async
¶
Run BEFORE_RUN hooks at the start of agent.run().
after_run(ctx, *, result)
async
¶
Run AFTER_RUN hooks at the end of agent.run().
on_run_error(ctx, *, error)
async
¶
Run RUN_ERROR hooks when agent.run() fails.
before_model_request(ctx, request_context)
async
¶
Run BEFORE_MODEL_REQUEST hooks before each LLM call.
after_model_request(ctx, *, request_context, response)
async
¶
Run AFTER_MODEL_REQUEST hooks after each LLM response.
dispatch_model_fallback(primary, fallback, error, backend)
async
¶
Dispatch MODEL_FALLBACK_TRIGGERED hooks outside the normal capability lifecycle.
Called by the fallback_on handler in create_deep_agent when FallbackModel switches from the primary to a fallback model.
default_security_hook¶
pydantic_deep.capabilities.hooks.default_security_hook(*, blocked_commands=None, allowed_write_roots=None, blocked_write_paths=None, blocked_read_paths=None, redact_secrets=True, secret_patterns=None, mode='deny')
¶
Return a ready-to-use list of security hooks for create_deep_agent.
The returned hooks gate execute, write_file, edit_file, and
read_file against destructive command patterns, path-traversal writes,
sensitive-path writes, and sensitive-path reads. When redact_secrets is
enabled, a second hook scrubs obvious secret shapes (AWS access keys,
GitHub PATs, OpenAI sk- keys, JWTs) from POST_TOOL_USE output.
The defaults are opt-out: pass mode="warn" to log instead of block,
pass blocked_commands=[] to disable a category, or extend any list with
your own patterns. Lists you pass replace the defaults - concatenate
with DEFAULT_BLOCKED_COMMANDS etc. if you want to keep them.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
blocked_commands
|
Sequence[str] | None
|
Regex patterns matched against the |
None
|
allowed_write_roots
|
Sequence[str | Path] | None
|
If set, |
None
|
blocked_write_paths
|
Sequence[str] | None
|
Regex patterns matched against the |
None
|
blocked_read_paths
|
Sequence[str] | None
|
Regex patterns matched against the |
None
|
redact_secrets
|
bool
|
When True (default), add a |
True
|
secret_patterns
|
Sequence[str] | None
|
Regex patterns redacted from tool output. Defaults to
|
None
|
mode
|
Literal['deny', 'warn']
|
|
'deny'
|
Returns:
| Type | Description |
|---|---|
list[Hook]
|
A list of |
list[Hook]
|
|
list[Hook]
|
|