API Reference¶
Complete API documentation for pydantic-ai-backend.
Quick Example¶
from dataclasses import dataclass
from pydantic_ai import Agent
from pydantic_ai_backends import LocalBackend, create_console_toolset
@dataclass
class Deps:
backend: LocalBackend
backend = LocalBackend(root_dir=".")
toolset = create_console_toolset()
agent = Agent("openai:gpt-4o", deps_type=Deps).with_toolset(toolset)
result = agent.run_sync("Create hello.py and run it", deps=Deps(backend=backend))
Modules¶
| Module | Description |
|---|---|
| Backends | LocalBackend, StateBackend, CompositeBackend |
| Docker | DockerSandbox, SessionManager, RuntimeConfig |
| Daytona | DaytonaSandbox cloud sandbox |
| Toolsets | Console toolset for pydantic-ai |
| Types | Type definitions |
Import Reference¶
# Toolset for pydantic-ai agents (requires [console] extra)
from pydantic_ai_backends import (
create_console_toolset,
get_console_system_prompt,
ConsoleDeps,
)
# Backends
from pydantic_ai_backends import (
LocalBackend,
StateBackend,
CompositeBackend,
)
# Docker (requires [docker] extra)
from pydantic_ai_backends import (
DockerSandbox,
SessionManager,
RuntimeConfig,
BUILTIN_RUNTIMES,
)
# Types
from pydantic_ai_backends import (
FileInfo,
WriteResult,
EditResult,
ExecuteResponse,
GrepMatch,
)
# Protocols
from pydantic_ai_backends import (
BackendProtocol,
SandboxProtocol,
)
Protocols¶
BackendProtocol¶
All backends implement this interface.
pydantic_ai_backends.protocol.BackendProtocol
¶
Bases: Protocol
Protocol for file storage backends.
All backends must implement these methods for basic file operations. This allows using different storage backends (in-memory, filesystem, Docker, cloud storage) interchangeably.
Example
Source code in src/pydantic_ai_backends/protocol.py
| Python | |
|---|---|
17 18 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 | |
ls_info(path)
¶
List files and directories at the given path.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
Directory path to list. |
required |
Returns:
| Type | Description |
|---|---|
list[FileInfo]
|
List of FileInfo objects for each entry. |
read_bytes(path)
¶
Read raw bytes from a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to read. |
required |
Returns:
| Type | Description |
|---|---|
bytes
|
File content as bytes. |
read(path, offset=0, limit=2000)
¶
Read file content with line numbers.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to read. |
required |
offset
|
int
|
Line number to start reading from (0-indexed). |
0
|
limit
|
int
|
Maximum number of lines to read. |
2000
|
Returns:
| Type | Description |
|---|---|
str
|
File content as a string with line numbers prefixed. |
Source code in src/pydantic_ai_backends/protocol.py
write(path, content)
¶
Write content to a file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to write to. |
required |
content
|
str | bytes
|
Content to write (string or bytes). |
required |
Returns:
| Type | Description |
|---|---|
WriteResult
|
WriteResult with path or error. |
Source code in src/pydantic_ai_backends/protocol.py
edit(path, old_string, new_string, replace_all=False)
¶
Edit a file by replacing strings.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
File path to edit. |
required |
old_string
|
str
|
String to find and replace. |
required |
new_string
|
str
|
Replacement string. |
required |
replace_all
|
bool
|
If True, replace all occurrences. Otherwise, replace only first. |
False
|
Returns:
| Type | Description |
|---|---|
EditResult
|
EditResult with path, error, or occurrence count. |
Source code in src/pydantic_ai_backends/protocol.py
glob_info(pattern, path='/')
¶
Find files matching a glob pattern.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Glob pattern (e.g., "**/*.py"). |
required |
path
|
str
|
Base directory to search from. |
'/'
|
Returns:
| Type | Description |
|---|---|
list[FileInfo]
|
List of matching FileInfo objects. |
Source code in src/pydantic_ai_backends/protocol.py
grep_raw(pattern, path=None, glob=None, ignore_hidden=True)
¶
Search for pattern in files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
pattern
|
str
|
Regex pattern to search for. |
required |
path
|
str | None
|
Specific file or directory to search. |
None
|
glob
|
str | None
|
Glob pattern to filter files. |
None
|
ignore_hidden
|
bool
|
If True, ignore hidden files. |
True
|
Returns:
| Type | Description |
|---|---|
list[GrepMatch] | str
|
List of GrepMatch objects or error string. |
Source code in src/pydantic_ai_backends/protocol.py
SandboxProtocol¶
Extends BackendProtocol with command execution.
pydantic_ai_backends.protocol.SandboxProtocol
¶
Bases: BackendProtocol, Protocol
Extended protocol for backends that support command execution.
In addition to file operations, sandbox backends can execute shell commands. This is useful for running code, installing packages, or any shell operations.
Example
Source code in src/pydantic_ai_backends/protocol.py
id
property
¶
Unique identifier for this sandbox instance.
execute(command, timeout=None)
¶
Execute a shell command.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
Command to execute. |
required |
timeout
|
int | None
|
Maximum execution time in seconds. |
None
|
Returns:
| Type | Description |
|---|---|
ExecuteResponse
|
ExecuteResponse with output, exit code, and truncation status. |
Source code in src/pydantic_ai_backends/protocol.py
| Python | |
|---|---|