Toolsets API¶
create_console_toolset¶
pydantic_ai_backends.toolsets.console.create_console_toolset(id=None, backend=None, include_execute=True, include_background=True, require_write_approval=False, require_execute_approval=True, default_ignore_hidden=True, permissions=None, ask_callback=None, ask_fallback='error', max_retries=1, image_support=False, max_image_bytes=DEFAULT_MAX_IMAGE_BYTES, document_support=False, max_document_bytes=DEFAULT_MAX_DOCUMENT_BYTES, edit_format='str_replace', descriptions=None, profile=DEFAULT_PROFILE)
¶
Create a console toolset for file operations and shell execution.
Works with any backend implementing BackendProtocol — LocalBackend,
DockerSandbox, StateBackend and so on.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
id
|
str | None
|
Optional unique ID for the toolset. |
None
|
backend
|
BackendProtocol | AsyncBackendProtocol | None
|
Backend every tool operates on. When omitted, each call reads
|
None
|
include_execute
|
bool
|
Include the |
True
|
include_background
|
bool
|
Include the background-shell tools. Requires a
backend implementing |
True
|
require_write_approval
|
bool
|
Whether |
False
|
require_execute_approval
|
bool
|
Whether |
True
|
default_ignore_hidden
|
bool
|
Default for |
True
|
permissions
|
PermissionRuleset | None
|
Ruleset deciding which tools exist and which need approval: an operation defaulting to "deny" drops its tools entirely, one defaulting to "ask" marks them as requiring approval. |
None
|
max_retries
|
int
|
Times a tool may retry within one run, with the message
fed back to the model — pydantic-ai's own argument validation, and
the mistakes |
1
|
image_support
|
bool
|
Return recognized image files ( |
False
|
max_image_bytes
|
int
|
Largest image returned; bigger ones yield an error. |
DEFAULT_MAX_IMAGE_BYTES
|
document_support
|
bool
|
Return recognized documents ( |
False
|
max_document_bytes
|
int
|
Largest document returned; bigger ones yield an error. |
DEFAULT_MAX_DOCUMENT_BYTES
|
edit_format
|
EditFormat
|
|
'str_replace'
|
descriptions
|
Mapping[str, str | ToolText] | None
|
Per-tool text overrides, keyed by tool name: |
None
|
profile
|
Profile
|
How much guidance the descriptions carry. |
DEFAULT_PROFILE
|
Example
from dataclasses import dataclass
from pydantic_ai_backends import LocalBackend, create_console_toolset
from pydantic_ai_backends.permissions import DEFAULT_RULESET
@dataclass
class MyDeps:
backend: LocalBackend
toolset = create_console_toolset()
deps = MyDeps(backend=LocalBackend("/workspace"))
hashline = create_console_toolset(edit_format="hashline")
multimodal = create_console_toolset(image_support=True, document_support=True)
guarded = create_console_toolset(permissions=DEFAULT_RULESET)
Source code in src/pydantic_ai_backends/toolsets/console.py
| Python | |
|---|---|
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 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 | |
get_console_system_prompt¶
pydantic_ai_backends.toolsets.console.get_console_system_prompt(edit_format='str_replace')
¶
The system prompt describing the console tools.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
edit_format
|
EditFormat
|
Which edit format to describe. |
'str_replace'
|
Source code in src/pydantic_ai_backends/toolsets/console.py
| Python | |
|---|---|
ConsoleDeps¶
pydantic_ai_backends.toolsets.console.ConsoleDeps
¶
Bases: Protocol
Dependencies that provide a backend for the console tools.
Source code in src/pydantic_ai_backends/toolsets/console.py
backend
property
¶
The backend for file operations.
ToolText¶
pydantic_ai_backends.toolsets.descriptions.ToolText
dataclass
¶
Everything the model reads about one tool.
Held as fields rather than one string because the parts have different
destinations: summary, usage, coding and returns are composed into
the tool's description, while args becomes the per-argument text in its
JSON schema. Splitting them is also what lets a host show summary in its
own catalogue and know it is the first sentence the model reads, rather than
a paraphrase written in another repository.
Source code in src/pydantic_ai_backends/toolsets/descriptions.py
summary
instance-attribute
¶
One sentence: what the tool does. Also what a catalogue should show.
usage = ''
class-attribute
instance-attribute
¶
When to use it, when to use another tool, and what it will not do.
coding = ''
class-attribute
instance-attribute
¶
Guidance only an agent working in a repository needs.
Rendered under the "coding" profile and omitted under "agent". Anything
true of any workspace belongs in usage instead.
args = field(default_factory=dict)
class-attribute
instance-attribute
¶
One entry per argument, keyed exactly as the parameter is named.
returns = ''
class-attribute
instance-attribute
¶
The shape of the result, including its failures and its truncation.
render(profile=DEFAULT_PROFILE)
¶
The description handed to the model.
Shaped the way pydantic-ai shapes a docstring that has a Returns:
section - the prose inside <summary>, the return description inside
<returns> - because that is what every tool built from a docstring
already sends, and a host registering these beside its own would
otherwise put two conventions in one tool list. A prose Returns:
paragraph was the first attempt and is what that inconsistency looked
like. tests/test_tool_text.py pins the shape against a tool the
framework renders itself, so a change there fails here rather than
drifting quietly.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
profile
|
Profile
|
Which audience to write for. |
DEFAULT_PROFILE
|
Source code in src/pydantic_ai_backends/toolsets/descriptions.py
docstring()
¶
A Google-style docstring carrying the argument text.
Set on the tool function before it is registered, because per-argument
descriptions reach the JSON schema through the docstring and through
nothing else — which is why they cannot simply live in render. The
summary is repeated here for a reader of the generated docstring; the
model reads render instead, since an explicit description wins over
the docstring's own summary.
Source code in src/pydantic_ai_backends/toolsets/descriptions.py
Console Tools¶
The toolset registers these tools. What each one says — its description and the
text describing every argument — is not written beside the function: it lives in
TOOL_TEXT, keyed by tool name, and is assigned when the tool is registered. Read
TOOL_TEXT["grep"].render() to see exactly what the model is handed.
async def ls(ctx, path: str = ".") -> str: ...
async def read_file(ctx, path: str, offset: int = 0, limit: int = 2000) -> str: ...
async def write_file(ctx, path: str, content: str) -> str: ...
async def edit_file(
ctx, path: str, old_string: str, new_string: str, replace_all: bool = False
) -> str: ...
async def hashline_edit(
ctx,
path: str,
start_line: int,
start_hash: str,
new_content: str,
end_line: int | None = None,
end_hash: str | None = None,
insert_after: bool = False,
) -> str: ...
async def glob(ctx, pattern: str, path: str = ".") -> str: ...
async def grep(
ctx,
pattern: str,
path: str | None = None,
glob_pattern: str | None = None,
output_mode: Literal["content", "files_with_matches", "count"] = "files_with_matches",
ignore_hidden: bool = True,
) -> str: ...
async def execute(ctx, command: str, timeout: int | None = 120) -> str: ...
async def run_in_background(ctx, command: str) -> str: ...
async def read_output(ctx, shell_id: str) -> str: ...
async def kill_shell(ctx, shell_id: str) -> str: ...
async def list_shells(ctx) -> str: ...