Skip to main content
The dspy.RLM module supports custom tools that the model can invoke programmatically from within its Python REPL code. For production environments, the default local execution runtime (Deno WASM) can be replaced with remote, isolated cloud sandboxes (such as E2B or Daytona) using interpreter_factory.

Quickstart: Custom Tools with Real-Time Text Streaming

Pass host-side functions to the tools=[...] parameter and wrap the RLM module with dspy.streamify to observe intermediate reasoning and function invocations in real time:

How Custom Tools Work in RLM

Functions passed via tools are exposed directly in the Python interpreter’s namespace:
  1. Host-Side Execution: Functions execute within your host Python environment with access to databases, internal credentials, or external calculation APIs.
  2. Serialization: Tool outputs must be JSON-serializable (such as int, float, str, dict, or list).
  3. Docstring Prompting: DSPy automatically parses docstrings and type hints so the RLM orchestrator knows when and how to call the function.

Connecting Remote Cloud Sandboxes

By default, DSPy executes generated code in a local Deno/WASM PythonInterpreter. For production workloads requiring strict hardware isolation or arbitrary package installation, you can implement the CodeInterpreter protocol and provide it via interpreter_factory.

The CodeInterpreter Protocol

Custom interpreters must implement this protocol:

E2B Cloud Sandbox Adapter Pattern

Here is an adapter pattern to run RLM code inside an isolated E2B Firecracker microVM:

Sandbox Runtime Comparison