pydantic_ai.environments
Execution environment abstractions for agents.
This package provides:
ExecutionEnvironment— abstract base class for execution environmentsExecutionProcess— interactive process handle with bidirectional I/OExecutionEnvironmentToolset— toolset exposing coding-agent-style tools backed by an environmentExecutionResult— result type
Implementations:
environments.docker.DockerEnvironment— Docker container-based sandbox (isolated)environments.local.LocalEnvironment— local subprocess environment (no isolation, for dev/testing)environments.memory.MemoryEnvironment— in-memory environment for testing
EnvToolName
module-attribute
EnvToolName = Literal[
"shell", "read_file", "write_file", "edit_file"
]
Tool name for an environment capability.
Used in capabilities to declare which methods an environment implements,
and by ExecutionEnvironmentToolset for include/exclude filtering.
ExecutionEnvironment
Bases: ABC
Abstract base class for execution environments.
An execution environment provides a place where agents can execute commands, read/write files, and search the filesystem.
Implementations range from in-memory (for testing) to local subprocess, Docker containers, and cloud-hosted VMs.
The only abstract member is capabilities; all tool methods raise
NotImplementedError by default. Concrete subclasses override the
methods that match their declared capabilities.
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
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 | |
capabilities
abstractmethod
property
capabilities: frozenset[EnvToolName]
Capabilities this environment supports (high-level).
Used by toolsets to decide which tools to register. Only methods corresponding to declared capabilities need to be implemented.
shell
async
shell(
command: str,
*,
timeout: float | None = 120,
env: dict[str, str] | None = None
) -> ExecutionResult
Execute a shell command and return the result.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The shell command to execute. |
required |
timeout
|
float | None
|
Maximum seconds to wait for completion.
Pass |
120
|
env
|
dict[str, str] | None
|
Additional environment variables for this command. Merged with (and overrides) any baseline environment variables. |
None
|
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
An |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
read_file
async
Read a file from the environment.
For text files, returns a string with cat -n style line numbers.
For binary files (images), returns raw bytes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path within the environment. |
required |
offset
|
int
|
The line number to start reading from (0-indexed). Ignored for binary files. |
0
|
limit
|
int
|
Maximum number of lines to read. Ignored for binary files. |
2000
|
Returns:
| Type | Description |
|---|---|
str | bytes
|
Text content with line numbers ( |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 | |
write_file
async
Create or overwrite a file in the environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path within the environment. |
required |
content
|
str | bytes
|
The file content (text or binary). |
required |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
217 218 219 220 221 222 223 224 | |
replace_str
async
Edit a file by exact string replacement.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str
|
The file path within the environment. |
required |
old
|
str
|
The exact text to find. |
required |
new
|
str
|
The replacement text. |
required |
replace_all
|
bool
|
If True, replace all occurrences. If False, the old string must appear exactly once or an error is raised. |
False
|
Returns:
| Type | Description |
|---|---|
int
|
The number of replacements made. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If the file does not exist. |
ValueError
|
If |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
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 | |
create_process
async
create_process(
command: str, *, env: dict[str, str] | None = None
) -> ExecutionProcess
Create an interactive process with streaming stdin/stdout.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The shell command to run. |
required |
env
|
dict[str, str] | None
|
Additional environment variables for this process. |
None
|
Returns:
| Type | Description |
|---|---|
ExecutionProcess
|
An |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 | |
__aenter__
async
__aenter__() -> Self
Start the environment (e.g., create a Docker container).
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
274 275 276 | |
__aexit__
async
__aexit__(*args: Any) -> None
Stop the environment and clean up resources.
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
278 279 | |
ExecutionEnvironmentToolset
Bases: FunctionToolset[Any]
Toolset providing coding-agent-style tools backed by an ExecutionEnvironment.
Tool names and schemas are designed to match what popular coding agents expose, so models are well-trained on them.
Tools are dynamically registered based on the environment's capabilities,
filtered by include/exclude.
The environment can be:
- Passed directly at construction time via shared_environment (shared across concurrent runs)
- Created per-run via environment_factory (isolated concurrent runs)
- Set/overridden via context var using use_environment() (for testing or per-call-site config)
Usage
from pydantic_ai import Agent
from pydantic_ai.environments import ExecutionEnvironmentToolset
from pydantic_ai.environments.docker import DockerEnvironment
env = DockerEnvironment(image='python:3.12-slim')
toolset = ExecutionEnvironmentToolset(env)
agent = Agent('openai:gpt-5.2', toolsets=[toolset])
async with env:
result = await agent.run('Write a script that prints hello')
Source code in pydantic_ai_slim/pydantic_ai/toolsets/execution_environment.py
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 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 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 | |
__init__
__init__(
shared_environment: ExecutionEnvironment | None = None,
*,
environment_factory: (
Callable[[], ExecutionEnvironment] | None
) = None,
include: Sequence[EnvToolName] | None = None,
exclude: Sequence[EnvToolName] | None = None,
require_shell_approval: bool = False,
require_write_approval: bool = False,
image_support: bool = True,
max_image_bytes: int = 50 * 1024 * 1024,
max_retries: int = 1,
id: str | None = None
)
Create a new execution environment toolset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
shared_environment
|
ExecutionEnvironment | None
|
A shared execution environment for tool execution.
All concurrent runs share this single environment instance.
Can also be set later via |
None
|
environment_factory
|
Callable[[], ExecutionEnvironment] | None
|
A callable that creates a fresh environment per
|
None
|
include
|
Sequence[EnvToolName] | None
|
Tool names to include. |
None
|
exclude
|
Sequence[EnvToolName] | None
|
Tool names to exclude. |
None
|
require_shell_approval
|
bool
|
Whether the |
False
|
require_write_approval
|
bool
|
Whether |
False
|
image_support
|
bool
|
Whether |
True
|
max_image_bytes
|
int
|
Maximum image file size to return as BinaryContent. |
50 * 1024 * 1024
|
max_retries
|
int
|
Maximum retries per tool call. |
1
|
id
|
str | None
|
Optional unique ID for the toolset (required for durable execution). |
None
|
Source code in pydantic_ai_slim/pydantic_ai/toolsets/execution_environment.py
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 | |
environment
property
environment: ExecutionEnvironment | None
The active execution environment, or None if not configured.
Checks the context var override first (which includes per-run factory environments), then falls back to the shared environment.
required_environment
property
required_environment: ExecutionEnvironment
The active execution environment, raising if not configured.
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no environment is available. |
use_environment
use_environment(
environment: ExecutionEnvironment,
) -> Iterator[None]
Override the execution environment for the current context.
Useful for testing or using different environments at different call sites.
Usage
with toolset.use_environment(test_env):
result = await agent.run('test prompt', toolsets=[toolset])
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
environment
|
ExecutionEnvironment
|
The execution environment to use within this context. |
required |
Source code in pydantic_ai_slim/pydantic_ai/toolsets/execution_environment.py
300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 | |
ExecutionProcess
Bases: ABC
Handle to a running process with bidirectional streaming I/O.
Used for interactive execution where a script outputs data, waits for input, processes it, and outputs more data.
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
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 | |
send
abstractmethod
async
send(data: bytes) -> None
Write data to the process's stdin.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
bytes
|
The bytes to write to stdin. |
required |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
51 52 53 54 55 56 57 | |
recv
abstractmethod
async
Read available output from stdout.
Blocks until data is available, the process exits, or the timeout expires.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum seconds to wait for data. None means wait indefinitely. |
None
|
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the timeout expires with no data available. |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
59 60 61 62 63 64 65 66 67 68 69 70 | |
recv_stderr
abstractmethod
async
Read available output from stderr.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum seconds to wait for data. None means wait indefinitely. |
None
|
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the timeout expires with no data available. |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
72 73 74 75 76 77 78 79 80 81 | |
returncode
abstractmethod
property
returncode: int | None
Return code if the process has exited, None if still running.
wait
abstractmethod
async
Wait for the process to exit.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
float | None
|
Maximum seconds to wait. None means wait indefinitely. |
None
|
Returns:
| Type | Description |
|---|---|
int
|
The process exit code. |
Raises:
| Type | Description |
|---|---|
TimeoutError
|
If the timeout expires before the process exits. |
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
88 89 90 91 92 93 94 95 96 97 98 99 100 | |
kill
abstractmethod
async
kill() -> None
Kill the process.
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
102 103 104 | |
ExecutionResult
dataclass
Result of a completed command execution.
Source code in pydantic_ai_slim/pydantic_ai/environments/_base.py
33 34 35 36 37 38 39 40 41 | |
pydantic_ai.environments.local
Local subprocess-based execution environment for development and testing.
Runs commands directly on the host machine within a specified root directory.
No isolation — use DockerEnvironment for untrusted code.
LocalEnvironment
Bases: ExecutionEnvironment
Local subprocess-based execution environment for development and testing.
Runs commands directly on the host machine within a specified root
directory. Provides no isolation — use DockerEnvironment for untrusted code.
Usage
async with LocalEnvironment(root_dir='/tmp/workspace') as env:
result = await env.shell('python script.py')
print(result.output)
Source code in pydantic_ai_slim/pydantic_ai/environments/local.py
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 | |
__init__
__init__(
root_dir: str | Path = ".",
*,
env_vars: dict[str, str] | None = None,
inherit_env: bool = True
) -> None
Create a local execution environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
root_dir
|
str | Path
|
The working directory for all operations. Defaults to the current directory. |
'.'
|
env_vars
|
dict[str, str] | None
|
Baseline environment variables for all commands. |
None
|
inherit_env
|
bool
|
Whether to inherit the host's environment variables.
When True (default), |
True
|
Source code in pydantic_ai_slim/pydantic_ai/environments/local.py
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 | |
shell
async
shell(
command: str,
*,
timeout: float | None = 120,
env: dict[str, str] | None = None
) -> ExecutionResult
Execute a command using subprocess for simplicity and reliability.
Source code in pydantic_ai_slim/pydantic_ai/environments/local.py
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 | |
pydantic_ai.environments.docker
Docker container-based environment for isolated code execution.
Requires the docker package: pip install pydantic-ai-slim[docker-environment]
DockerEnvironment
Bases: ExecutionEnvironment
Docker container-based environment for isolated code execution.
Provides isolated code execution with configurable resource limits, network access, and persistent or ephemeral workspaces.
Usage
async with DockerEnvironment(image='python:3.12-slim') as env:
result = await env.shell('python -c "print(42)"')
print(result.output)
Source code in pydantic_ai_slim/pydantic_ai/environments/docker.py
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 | |
__init__
__init__(
*,
image: str = "python:3.12-slim",
env_vars: dict[str, str] | None = None,
work_dir: str = "/workspace",
volumes: dict[str, dict[str, str]] | None = None,
memory_limit: str | None = None,
cpu_limit: float | None = None,
pids_limit: int | None = None,
network_disabled: bool = False,
read_only: bool = False,
cap_drop: list[str] | None = None,
security_opt: list[str] | None = None,
user: str | None = None,
tmpfs: dict[str, str] | None = None,
init: bool = False
) -> None
Create a Docker environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
str
|
Docker image to use. Pre-build custom images with any required packages before passing them here. |
'python:3.12-slim'
|
env_vars
|
dict[str, str] | None
|
Baseline environment variables to set in the container. |
None
|
work_dir
|
str
|
Working directory inside the container. |
'/workspace'
|
volumes
|
dict[str, dict[str, str]] | None
|
Volume mounts (Docker format). |
None
|
memory_limit
|
str | None
|
Memory limit (e.g. '512m', '1g'). |
None
|
cpu_limit
|
float | None
|
CPU limit (e.g. 1.0 for one CPU). |
None
|
pids_limit
|
int | None
|
Maximum number of PIDs in the container (e.g. 256). Prevents fork bombs. |
None
|
network_disabled
|
bool
|
Whether to disable network access. |
False
|
read_only
|
bool
|
Whether to mount the root filesystem as read-only.
Use with |
False
|
cap_drop
|
list[str] | None
|
Linux capabilities to drop (e.g. |
None
|
security_opt
|
list[str] | None
|
Security options (e.g. |
None
|
user
|
str | None
|
User to run as inside the container (e.g. |
None
|
tmpfs
|
dict[str, str] | None
|
tmpfs mounts as |
None
|
init
|
bool
|
Whether to use |
False
|
Source code in pydantic_ai_slim/pydantic_ai/environments/docker.py
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 | |
hardened
classmethod
hardened(
*,
image: str = "python:3.12-slim",
env_vars: dict[str, str] | None = None,
work_dir: str = "/workspace",
memory_limit: str = "512m",
cpu_limit: float = 1.0,
pids_limit: int = 256
) -> DockerEnvironment
Create a hardened Docker environment with security best practices.
This is a convenience constructor that sets sensible security defaults:
network disabled, read-only root filesystem, all capabilities dropped,
no privilege escalation, runs as nobody, and uses an init process.
The root filesystem is read-only; writable tmpfs mounts are provided at
/tmp and the working directory.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
image
|
str
|
Docker image to use. |
'python:3.12-slim'
|
env_vars
|
dict[str, str] | None
|
Baseline environment variables to set in the container. |
None
|
work_dir
|
str
|
Working directory inside the container. |
'/workspace'
|
memory_limit
|
str
|
Memory limit (e.g. '512m', '1g'). |
'512m'
|
cpu_limit
|
float
|
CPU limit (e.g. 1.0 for one CPU). |
1.0
|
pids_limit
|
int
|
Maximum number of PIDs in the container. |
256
|
Source code in pydantic_ai_slim/pydantic_ai/environments/docker.py
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 | |
shell
async
shell(
command: str,
*,
timeout: float | None = 120,
env: dict[str, str] | None = None
) -> ExecutionResult
Execute a command in the container.
Source code in pydantic_ai_slim/pydantic_ai/environments/docker.py
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 | |
is_alive
async
is_alive() -> bool
Check if the container is running.
Returns:
| Type | Description |
|---|---|
bool
|
True if the container is running, False otherwise. |
Source code in pydantic_ai_slim/pydantic_ai/environments/docker.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 | |
pydantic_ai.environments.memory
In-memory execution environment for testing.
All file operations use an in-memory dictionary. Shell commands are handled
by an optional callback — if not provided, shell() raises RuntimeError.
MemoryEnvironment
Bases: ExecutionEnvironment
In-memory execution environment for testing.
File operations use an in-memory dictionary, making tests fast and isolated with no filesystem access. Shell commands can optionally be handled by a user-provided callback.
This is the testing counterpart to LocalEnvironment, analogous to
how TestModel and FunctionModel relate to real model classes.
Usage
from pydantic_ai.environments.memory import MemoryEnvironment
env = MemoryEnvironment(files={'main.py': 'print("hello")'})
async with env:
content = await env.read_file('main.py')
assert 'hello' in content
Source code in pydantic_ai_slim/pydantic_ai/environments/memory.py
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 | |
__init__
__init__(
files: dict[str, str | bytes] | None = None,
*,
command_handler: (
Callable[[str], ExecutionResult] | None
) = None
) -> None
Create an in-memory execution environment.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
files
|
dict[str, str | bytes] | None
|
Initial files to populate the environment with. Keys are file paths, values are file contents (str or bytes). |
None
|
command_handler
|
Callable[[str], ExecutionResult] | None
|
Optional callback for |
None
|
Source code in pydantic_ai_slim/pydantic_ai/environments/memory.py
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
files
property
Read-only view of the in-memory file system.
Keys are normalized file paths, values are file contents.
Useful for test assertions against raw file content without the
line-number formatting that [read_file()][pydantic_ai.environments.memory.MemoryEnvironment.read_file] adds.
shell
async
shell(
command: str,
*,
timeout: float | None = 120,
env: dict[str, str] | None = None
) -> ExecutionResult
Execute a command using the configured handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The shell command to execute. |
required |
timeout
|
float | None
|
Ignored for MemoryEnvironment. |
120
|
env
|
dict[str, str] | None
|
Ignored for MemoryEnvironment. |
None
|
Returns:
| Type | Description |
|---|---|
ExecutionResult
|
The result from the command handler. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If no command_handler was provided. |
Source code in pydantic_ai_slim/pydantic_ai/environments/memory.py
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 | |