AgentMiddleware¶
pydantic_ai_middleware.AgentMiddleware
¶
Bases: ABC, Generic[DepsT]
Base middleware class.
Inherit and override only the methods you need. Middleware hooks are called in order for before_* methods and in reverse order for after_* methods.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_names |
set[str] | None
|
Set of tool names this middleware applies to. If None (default), the middleware applies to all tools. |
timeout |
float | None
|
Maximum time in seconds for any hook call. If None (default), no timeout is enforced. |
Source code in src/pydantic_ai_middleware/base.py
| Python | |
|---|---|
19 20 21 22 23 24 25 26 27 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 61 62 63 64 65 66 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 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 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 | |
tool_names = None
class-attribute
instance-attribute
¶
timeout = None
class-attribute
instance-attribute
¶
_should_handle_tool(tool_name)
¶
Check if this middleware should handle the given tool.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The name of the tool to check. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the middleware should handle this tool. |
Source code in src/pydantic_ai_middleware/base.py
| Python | |
|---|---|
before_run(prompt, deps, ctx=None)
async
¶
Called before the agent runs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str | Sequence[Any]
|
The user prompt. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: before_run, read: config/metadata). |
None
|
Returns:
| Type | Description |
|---|---|
str | Sequence[Any]
|
The (possibly modified) prompt. |
Raises:
| Type | Description |
|---|---|
InputBlocked
|
To block the input. |
Source code in src/pydantic_ai_middleware/base.py
after_run(prompt, output, deps, ctx=None)
async
¶
Called after the agent finishes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
prompt
|
str | Sequence[Any]
|
The original user prompt. |
required |
output
|
Any
|
The agent output. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: after_run, read: all previous). |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The (possibly modified) output. |
Source code in src/pydantic_ai_middleware/base.py
before_model_request(messages, deps, ctx=None)
async
¶
Called before each model request.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
messages
|
list[ModelMessage]
|
The messages to send to the model. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: before_model_request, read: before_run). |
None
|
Returns:
| Type | Description |
|---|---|
list[ModelMessage]
|
The (possibly modified) messages. |
Source code in src/pydantic_ai_middleware/base.py
before_tool_call(tool_name, tool_args, deps, ctx=None)
async
¶
Called before a tool is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The name of the tool being called. |
required |
tool_args
|
dict[str, Any]
|
The arguments to the tool. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: before_tool_call, read: before_run, before_model_request). |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, Any] | ToolPermissionResult
|
The (possibly modified) tool arguments, or a ToolPermissionResult |
dict[str, Any] | ToolPermissionResult
|
for structured permission decisions. |
Raises:
| Type | Description |
|---|---|
ToolBlocked
|
To block the tool call. |
Source code in src/pydantic_ai_middleware/base.py
on_tool_error(tool_name, tool_args, error, deps, ctx=None)
async
¶
Called when a tool execution raises an error.
This hook provides tool-specific error context (tool_name, tool_args) that is not available in the generic on_error hook.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The name of the tool that failed. |
required |
tool_args
|
dict[str, Any]
|
The arguments that were passed to the tool. |
required |
error
|
Exception
|
The exception raised by the tool. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: on_tool_error, read: all hooks). |
None
|
Returns:
| Type | Description |
|---|---|
Exception | None
|
A different exception to raise, or None to re-raise the original. |
Source code in src/pydantic_ai_middleware/base.py
after_tool_call(tool_name, tool_args, result, deps, ctx=None)
async
¶
Called after a tool is called.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
The name of the tool that was called. |
required |
tool_args
|
dict[str, Any]
|
The arguments that were passed to the tool. |
required |
result
|
Any
|
The result from the tool. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: after_tool_call, read: all before_* hooks). |
None
|
Returns:
| Type | Description |
|---|---|
Any
|
The (possibly modified) result. |
Source code in src/pydantic_ai_middleware/base.py
on_error(error, deps, ctx=None)
async
¶
Called when an error occurs.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
error
|
Exception
|
The exception that occurred. |
required |
deps
|
DepsT | None
|
The agent dependencies. |
required |
ctx
|
ScopedContext | None
|
Scoped context for data sharing (write: on_error, read: all hooks). |
None
|
Returns:
| Type | Description |
|---|---|
Exception | None
|
A different exception to raise, or None to re-raise the original. |