Skip to main content

Filesystem sandbox toolset for PydanticAI agents with LLM-friendly errors

Project description

pydantic-ai-filesystem-sandbox

Filesystem sandbox toolset for PydanticAI agents with LLM-friendly errors.

Why This Package?

When building LLM agents that interact with the filesystem, you need:

  1. Sandboxing - Restrict which directories the agent can access
  2. Read/Write Control - Fine-grained permissions per path
  3. LLM-Friendly Errors - Error messages that help the LLM correct its behavior
  4. Approval Integration - Works with human-in-the-loop approval flows

This package provides a FileSandboxImpl toolset that implements all of these as a PydanticAI AbstractToolset.

Installation

pip install pydantic-ai-filesystem-sandbox

Quick Start

from pydantic_ai import Agent
from pydantic_ai_filesystem_sandbox import (
    FileSandboxImpl,
    FileSandboxConfig,
    PathConfig,
)

# Configure sandbox paths
config = FileSandboxConfig(paths={
    "input": PathConfig(root="./data/input", mode="ro"),   # Read-only
    "output": PathConfig(root="./data/output", mode="rw"), # Read-write
})

# Create the sandbox toolset
sandbox = FileSandboxImpl(config)

# Use with PydanticAI agent
agent = Agent("openai:gpt-4", toolsets=[sandbox])

Configuration

PathConfig Options

Option Type Default Description
root str required Root directory path
mode "ro" | "rw" "ro" Access mode
suffixes list[str] | None None Allowed file extensions (None = all)
max_file_bytes int | None None Maximum file size limit
write_approval bool True Require approval for writes
read_approval bool False Require approval for reads

Example Configuration

config = FileSandboxConfig(paths={
    # Read-only input - any file type
    "input": PathConfig(
        root="./data/input",
        mode="ro",
    ),
    # Read-write output - only markdown and text
    "output": PathConfig(
        root="./data/output",
        mode="rw",
        suffixes=[".md", ".txt"],
        max_file_bytes=1_000_000,  # 1MB limit
    ),
    # Config files - read-only, requires approval
    "config": PathConfig(
        root="./config",
        mode="ro",
        read_approval=True,
    ),
})

Available Tools

The sandbox provides three tools to the agent:

read_file

Read a text file from the sandbox.

Path format: 'sandbox_name/relative/path'
Parameters:
  - path: str (required)
  - max_chars: int (default: 20,000)
  - offset: int (default: 0)

write_file

Write a text file to the sandbox (requires mode="rw").

Path format: 'sandbox_name/relative/path'
Parameters:
  - path: str (required)
  - content: str (required)

list_files

List files matching a glob pattern.

Parameters:
  - path: str (default: "." for all sandboxes)
  - pattern: str (default: "**/*")

LLM-Friendly Errors

All errors include guidance on what IS allowed:

# PathNotInSandboxError
"Cannot access 'secret/file.txt': path is outside sandbox.
Readable paths: input, output"

# PathNotWritableError
"Cannot write to 'input/file.txt': path is read-only.
Writable paths: output"

# SuffixNotAllowedError
"Cannot access 'output/data.json': suffix '.json' not allowed.
Allowed suffixes: .md, .txt"

# FileTooLargeError
"Cannot read 'output/huge.txt': file too large (5,000,000 bytes).
Maximum allowed: 1,000,000 bytes"

Approval Integration

Works with pydantic-ai-blocking-approval for human-in-the-loop:

from pydantic_ai_filesystem_sandbox import FileSandboxImpl, FileSandboxConfig, PathConfig
from pydantic_ai_blocking_approval import ApprovalToolset, ApprovalController

# Create sandbox
config = FileSandboxConfig(paths={
    "output": PathConfig(root="./output", mode="rw", write_approval=True),
})
sandbox = FileSandboxImpl(config)

# Wrap with approval
controller = ApprovalController(mode="interactive", approval_callback=my_prompt_fn)
approved_sandbox = ApprovalToolset(
    inner=sandbox,
    prompt_fn=controller.approval_callback,
    memory=controller.memory,
    require_approval=["write_file", "read_file"],
)

agent = Agent(..., toolsets=[approved_sandbox])

The sandbox implements needs_approval() and present_for_approval() for fine-grained approval control.

ReadResult

The read_file tool returns a ReadResult object:

class ReadResult(BaseModel):
    content: str        # The file content read
    truncated: bool     # True if more content exists
    total_chars: int    # Total file size in characters
    offset: int         # Starting position used
    chars_read: int     # Characters actually returned

This allows agents to handle large files by reading in chunks:

# First read
result = sandbox.read("input/large.txt", max_chars=10000)
if result.truncated:
    # Continue reading
    result2 = sandbox.read("input/large.txt", max_chars=10000, offset=10000)

API Reference

Configuration

  • FileSandboxConfig - Top-level configuration with named paths
  • PathConfig - Configuration for a single sandbox path

Toolset

  • FileSandboxImpl - PydanticAI AbstractToolset implementation

Errors

  • FileSandboxError - Base class for all sandbox errors
  • PathNotInSandboxError - Path outside sandbox boundaries
  • PathNotWritableError - Write to read-only path
  • SuffixNotAllowedError - File extension not allowed
  • FileTooLargeError - File exceeds size limit

Types

  • ReadResult - Result of read operations with metadata

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pydantic_ai_filesystem_sandbox-0.1.0.tar.gz (177.1 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pydantic_ai_filesystem_sandbox-0.1.0-py3-none-any.whl (10.6 kB view details)

Uploaded Python 3

File details

Details for the file pydantic_ai_filesystem_sandbox-0.1.0.tar.gz.

File metadata

File hashes

Hashes for pydantic_ai_filesystem_sandbox-0.1.0.tar.gz
Algorithm Hash digest
SHA256 c9914835bbbaecd30823a29863e2e88c2a73211f5629ad06deb5f97a2345fa6d
MD5 f64325e5cb592092ea8b0aec1793d649
BLAKE2b-256 d1138e31d539096e3629381a2f5e8514828c3aa7ba04a54a4d52d579845cb378

See more details on using hashes here.

File details

Details for the file pydantic_ai_filesystem_sandbox-0.1.0-py3-none-any.whl.

File metadata

File hashes

Hashes for pydantic_ai_filesystem_sandbox-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 c4bb37d1c72e8d8948672ee1ed424388b8b95f93050cc8576c928be410e158c8
MD5 83cfa34b314bfdbf959d27e2d074dcbf
BLAKE2b-256 a74ed230097394f2dc5f070bf5b57193e852f4469296bc30fab1bd19cb068c62

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page