ConsoleCapability API¶
Console capability for pydantic-ai agents.
Provides ConsoleCapability that bundles console toolset + instructions +
permission enforcement via the pydantic-ai capabilities API.
Example
ConsoleCapability
dataclass
¶
Bases: AbstractCapability[Any]
Capability providing filesystem tools with permission enforcement.
Bundles the console toolset (ls, read_file, write_file, edit_file, glob, grep, execute) with dynamic instructions and per-tool permission control.
When a permission ruleset is provided:
- Tools for denied operations are dropped from the toolset entirely, and
hidden again from each request's tool definitions
- Per-path/command permissions are checked before each tool execution
- "ask" permissions go to ask_callback, and are refused or raised per
ask_fallback when there is none
Example
from pydantic_ai import Agent
from pydantic_ai_backends import ConsoleCapability, DockerSandbox
from pydantic_ai_backends.permissions import READONLY_RULESET
# Read-only agent — write/edit/execute tools are hidden
agent = Agent(
"openai:gpt-4.1",
capabilities=[ConsoleCapability(permissions=READONLY_RULESET)],
)
# A sandbox the capability owns, for an agent whose deps type is not ours
agent = Agent(
"openai:gpt-4.1",
capabilities=[ConsoleCapability(backend=DockerSandbox(runtime="python-web"))],
)
Source code in src/pydantic_ai_backends/capability.py
| Python | |
|---|---|
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 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 | |
backend = None
class-attribute
instance-attribute
¶
Backend the tools operate on.
When omitted, each call reads ctx.deps.backend, which requires the agent's
deps to satisfy ConsoleDeps. Set it when the host owns its deps type and
cannot add a backend field — the capability then carries the backend
itself, which is also what lets one agent hold a sandbox of its own.
include_execute = True
class-attribute
instance-attribute
¶
Whether to include the execute tool.
include_background = True
class-attribute
instance-attribute
¶
Whether to include the background-shell tools.
Separate from include_execute because a backend may support commands
without supporting long-lived ones — and because an agent that should not
start a process it cannot see finish wants these off while keeping execute.
edit_format = 'str_replace'
class-attribute
instance-attribute
¶
Edit format: 'str_replace' or 'hashline'.
image_support = False
class-attribute
instance-attribute
¶
Return recognized images from read_file as BinaryContent.
Without it a multimodal model reading a .png gets the bytes as garbled
text. With it the agent can look at a chart it rendered a moment ago, which
is the loop that makes producing one useful.
max_image_bytes = DEFAULT_MAX_IMAGE_BYTES
class-attribute
instance-attribute
¶
Largest image returned; bigger ones yield an error.
document_support = False
class-attribute
instance-attribute
¶
Return recognized documents (.pdf) as BinaryContent.
Separate from image_support because the model support is separate: a model
that sees images does not necessarily read PDFs natively.
max_document_bytes = DEFAULT_MAX_DOCUMENT_BYTES
class-attribute
instance-attribute
¶
Largest document returned; bigger ones yield an error.
descriptions = None
class-attribute
instance-attribute
¶
Per-tool text overrides, keyed by tool name.
A host that lists these tools in its own catalogue needs the text it shows and the text the model reads to be the same string. Without this the two are written in different repositories and drift, and the drift is invisible: the person choosing what to allow and the model choosing when to act are reading different descriptions of the same tool.
A string replaces the tool's description and leaves its argument text alone;
a ToolText replaces both. An unknown tool name raises rather than being
ignored.
profile = DEFAULT_PROFILE
class-attribute
instance-attribute
¶
How much guidance the tool descriptions carry.
"coding" includes what an agent working in a repository needs — git,
dependencies, reading a failed command's output. "agent" leaves it out,
which is what an agent whose workspace is scratch space for one conversation
should be paying for.
permissions = None
class-attribute
instance-attribute
¶
Permission ruleset for controlling tool access.
ask_callback = None
class-attribute
instance-attribute
¶
Async approval callback for operations the ruleset resolves to "ask".
Without one an "ask" cannot be answered, and ask_fallback decides what
happens instead. Every shipped preset except PERMISSIVE_RULESET has at
least one operation defaulting to "ask", so a ruleset supplied without this
or ask_fallback="deny" refuses those operations by raising.
ask_fallback = 'error'
class-attribute
instance-attribute
¶
What an unanswerable "ask" does — "deny" refuses it, "error" raises.
__post_init__()
¶
Create the underlying console toolset and permission checker.
Source code in src/pydantic_ai_backends/capability.py
get_serialization_name()
classmethod
¶
get_toolset()
¶
get_instructions()
¶
prepare_tools(ctx, tool_defs)
async
¶
Hide tools for denied operations.
Source code in src/pydantic_ai_backends/capability.py
before_tool_execute(ctx, *, call, tool_def, args)
async
¶
Check this call's path or command against the ruleset.
Raises:
| Type | Description |
|---|---|
PermissionDeniedError
|
If the ruleset denies the operation. |