---
title: "Pydantic AI integration"
description: "Run Pydantic AI coding tools in an E2B sandbox with the native E2BSandbox workspace, and keep that workspace across runs and worker restarts."
url: "https://e2b.dev/integrations/pydantic-ai"
---

# Pydantic AI

> Run Pydantic AI's Shell and FileSystem capabilities in an E2B sandbox. The native E2BSandbox workspace keeps its files across runs, pauses and worker restarts.

Agent frameworks & SDKs · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** Pydantic
- **How it works:** Built into pydantic-ai-harness
- **Package:** pydantic-ai-harness[e2b]
- **Language:** Python
- **Upstream:** [pydantic/pydantic-ai · ★ 21k](https://github.com/pydantic/pydantic-ai)
- **Example:** [e2b-cookbook/pydantic-ai-python](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/pydantic-ai-python)

## Get started

Paste the prompt into your coding agent, or follow the steps yourself.

### Paste this into your coding agent

```text
Add E2B sandboxes to this Pydantic AI project.

1. Install pydantic-ai-harness with the e2b extra ("pydantic-ai-harness[e2b]") using the project's package manager. It needs Python 3.11 or newer.
2. The sandbox needs the E2B_API_KEY environment variable. If it isn't set, ask me for my API key from https://console.e2b.dev/?tab=keys. Never hardcode it.
3. Follow the Pydantic AI guide at https://docs.e2b.dev/agents/pydantic-ai. Create an E2BSandboxBackend, give my agent the E2BSandbox, Shell and FileSystem capabilities, pass the backend as workspace=, and destroy the sandbox in a finally block.
4. Run the agent once and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Install the E2B integration

```bash
pip install "pydantic-ai-harness[e2b]>=0.54.0" "pydantic-ai-slim[openai]>=2.54.0"
```

### 2. Set your API keys

```bash
export E2B_API_KEY="your_e2b_api_key"
export OPENAI_API_KEY="your_openai_api_key"
```

### 3. Give the agent an E2B workspace

```python
from pydantic_ai import Agent
from pydantic_ai_harness.e2b_sandbox import E2BSandbox, E2BSandboxBackend
from pydantic_ai_harness.filesystem import FileSystem
from pydantic_ai_harness.shell import Shell

backend = E2BSandboxBackend(
    working_dir="/home/user/log-cli",
    sandbox_timeout=300,
    allow_internet_access=False,
)

agent = Agent(
    "openai:gpt-4.1-mini",
    capabilities=[
        E2BSandbox(),
        Shell(tools=["run_command"], default_timeout=20),
        FileSystem(tools=["read_file", "write_file", "edit_file"], content_hashes=False),
    ],
)
```

### 4. Run it and clean up

```python
import asyncio


async def main():
    try:
        result = await agent.run(
            "Write log_stats.py that counts HTTP 500–599 responses in a log file, "
            "create a small sample log, and run the script on it.",
            workspace=backend,
        )
        print(result.output)
    finally:
        if backend.ref is not None:
            await E2BSandbox().destroy(backend.ref)


asyncio.run(main())
```

## What runs in the sandbox

Commands and file edits run in an isolated sandbox; the agent loop and model calls stay on your machine.

- **Code:** Shell commands and file edits from the Shell, FileSystem and Coder capabilities.
- **State:** Save result.workspace.ref and pass it back as workspace= to reconnect. A paused sandbox resumes with its files.
- **Lifetime:** Sandboxes default to a 3,600-second lifetime and pause on timeout. Pydantic AI never deletes them; call destroy(ref).
- **Secrets:** API keys stay in the process running the agent. The sandbox doesn't inherit your environment.

## More agent frameworks

Other frameworks that run their tools on E2B.

- [Agno](https://e2b.dev/integrations/agno.md): Add Agno's built-in E2BTools so agents can run code and commands in a sandbox.
- [Google ADK](https://e2b.dev/integrations/google-adk.md): Use E2B as the code execution environment for Google ADK agents in Python.
- [OpenAI Agents SDK](https://e2b.dev/integrations/openai-agents-sdk.md): Use the built-in E2B sandbox client in the OpenAI Agents SDK to give agents a real machine.

## Links

- [Get API key](https://console.e2b.dev/?tab=keys)
- [Read the docs](https://docs.e2b.dev/agents/pydantic-ai)
- [E2B documentation](https://docs.e2b.dev)
