---
title: "DSPy RLMs integration"
description: "Run DSPy RLM-generated Python in E2B sandboxes with full CPython, persistent SQLite and an enforced execution deadline."
url: "https://e2b.dev/integrations/dspy"
---

# DSPy RLMs

> Run the Python that DSPy recursive language models write in an E2B sandbox. You get full CPython with SQLite, and code that loops forever gets killed.

Training & evals · [All integrations](https://e2b.dev/integrations.md)

## About this integration

- **Maintained by:** E2B
- **How it works:** E2BInterpreter via interpreter_factory
- **Package:** dspy 3.4.0
- **Language:** Python
- **Upstream:** [stanfordnlp/dspy · ★ 39k](https://github.com/stanfordnlp/dspy)
- **Example:** [e2b-cookbook/dspy-rlms-python](https://github.com/e2b-dev/e2b-cookbook/tree/main/examples/dspy-rlms-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 DSPy project.

1. Install the dspy-rlms-python example from the E2B cookbook into the project using its package manager. E2BInterpreter comes from that local project, not from PyPI.
2. The interpreter 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 DSPy RLMs guide at https://docs.e2b.dev/agents/dspy and pass an interpreter_factory that returns a new E2BInterpreter to dspy.RLM, so the Python the model writes runs in an E2B sandbox.
4. Run the RLM once and show me the output that came from the sandbox.
```

## Or set it up yourself

### 1. Get the example

```bash
git clone https://github.com/e2b-dev/e2b-cookbook.git
cd e2b-cookbook/examples/dspy-rlms-python
uv sync --frozen
cp .env.example .env
```

### 2. Set your keys in .env

```bash
E2B_API_KEY=e2b_...
OPENAI_API_KEY=...
MODEL=openai/gpt-5.6-luna
```

### 3. Give the RLM an E2B interpreter

```python
import dspy
from dspy_e2b import E2BInterpreter


class IncidentAnalysis(dspy.Signature):
    records: list[dict] = dspy.InputField()
    query: str = dspy.InputField()
    candidate_ids: list[str] = dspy.OutputField(desc="Exact SQL-filtered IDs, sorted")
    queue_incident_ids: list[str] = dspy.OutputField(desc="IDs classified as queue buildup, sorted")
    counts_by_region: dict[str, int] = dspy.OutputField(desc="SQL counts for queue_incident_ids")
    summary: str = dspy.OutputField(desc="Short explanation grounded in the selected records")


def interpreter_factory():
    return E2BInterpreter(execution_timeout=120, sandbox_timeout=600)


interpreter_factory.execution_instructions = E2BInterpreter.execution_instructions

rlm = dspy.RLM(
    IncidentAnalysis,
    interpreter_factory=interpreter_factory,
    max_iters=6,
    max_llm_calls=4,
    max_output_chars=4000,
)
```

### 4. Run the incident analysis

```bash
uv run main.py
```

## What runs in the sandbox

The Python your RLM writes runs in a network-off E2B sandbox. Model calls and keys stay on your host.

- **Code:** Generated Python runs in full CPython on remote Linux, with sqlite3 and subprocess and no outbound network.
- **State:** Imports, variables and SQLite connections persist across steps within one invocation. Each invocation gets its own sandbox.
- **Lifetime:** execution_timeout bounds each step and sandbox_timeout the session. shutdown() deletes the sandbox when DSPy is done.
- **Secrets:** Provider keys stay on the host. llm_query calls from the sandbox are sent back to the host, which calls the model.

## More training & eval tools

Other RL and eval tools that run their rollouts on E2B.

- [Harbor / Terminal-Bench](https://e2b.dev/integrations/harbor.md): Run Harbor and Terminal-Bench trials on E2B and scale evals and rollouts to many parallel sandboxes.
- [Hugging Face OpenEnv](https://e2b.dev/integrations/openenv.md): Run OpenEnv environments like Terminus on E2B and get a first reward without a GPU.
- [Hugging Face TRL](https://e2b.dev/integrations/trl.md): Compute code rewards for TRL GRPO training in E2B sandboxes.

## Links

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