Installation¶
Requirements¶
- Python 3.10+
- uv (recommended) or pip
Install with uv (recommended)¶
Install with pip¶
Optional Dependencies¶
Console Toolset¶
For the ready-to-use pydantic-ai toolset:
Docker Sandbox¶
For isolated code execution in Docker containers:
All Dependencies¶
Environment Setup¶
API Key (for console toolset)¶
If using the console toolset with pydantic-ai, set your model provider's API key:
Docker (for DockerSandbox)¶
For using DockerSandbox:
- Install Docker: Get Docker
- Ensure Docker daemon is running
- Pull a base image:
Verify Installation¶
Basic (LocalBackend)¶
Python
from pydantic_ai_backends import LocalBackend
backend = LocalBackend(root_dir=".")
backend.write("test.txt", "Hello from pydantic-ai-backend!")
print(backend.read("test.txt"))
With Console Toolset¶
Python
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=".", enable_execute=False)
toolset = create_console_toolset(include_execute=False)
agent = Agent("openai:gpt-4o-mini", deps_type=Deps)
agent = agent.with_toolset(toolset)
result = agent.run_sync("List files in current directory", deps=Deps(backend=backend))
print(result.output)
With Docker¶
Python
from pydantic_ai_backends import DockerSandbox
sandbox = DockerSandbox(image="python:3.12-slim")
sandbox.write("/workspace/hello.py", "print('Hello from Docker!')")
result = sandbox.execute("python /workspace/hello.py")
print(result.output) # "Hello from Docker!"
sandbox.stop()
Troubleshooting¶
Import Errors¶
Ensure you have the correct Python version:
Docker Permission Denied¶
On Linux, add your user to the docker group:
Then log out and back in.
pydantic-ai Not Found¶
If using console toolset, install with the [console] extra:
Next Steps¶
- Core Concepts - Learn the fundamentals
- Local Backend Example - Start with local files
- API Reference - Complete API documentation