Skip to main content

Training & evals

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.

Maintained by
E2B
How it works
E2BInterpreter via interpreter_factory
Package
dspy 3.4.0
Language
Python

Set it up

Get started

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

Paste this into your coding agent

Add E2B sandboxes to this DSPy project.

  1. 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. 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. 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. 4.Run the RLM once and show me the output that came from the sandbox.

Or set it up yourself

  1. 1. Get the example

    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. 2. Set your keys in .env

    E2B_API_KEY=e2b_...
    OPENAI_API_KEY=...
    MODEL=openai/gpt-5.6-luna
  3. 3. Give the RLM an E2B interpreter

    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. 4. Run the incident analysis

    uv run main.py

Security

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.

  • CodeGenerated Python runs in full CPython on remote Linux, with sqlite3 and subprocess and no outbound network.
  • StateImports, variables and SQLite connections persist across steps within one invocation. Each invocation gets its own sandbox.
  • Lifetimeexecution_timeout bounds each step and sandbox_timeout the session. shutdown() deletes the sandbox when DSPy is done.
  • SecretsProvider keys stay on the host. llm_query calls from the sandbox are sent back to the host, which calls the model.

3B+ sandboxes started

Give every agent a machine