Skip to content

armgit / dspy

Public Imported from GitHub
0

DSPy: The framework for programming—not prompting—language models

Python

feat(dspy): dspy.settings.interpreter_factory overrides default PythonInterpreter (#10383)

Adriaan Moors6fb74fcf45bd
diff --git a/docs/docs/api/modules/Flex.md b/docs/docs/api/modules/Flex.md
index 0123341c9..3ce26aebe 100644
--- a/docs/docs/api/modules/Flex.md
+++ b/docs/docs/api/modules/Flex.md
@@ -84,7 +84,7 @@ The `program_trace` parameter is opt-in *by declaration*: only metrics that name
## Interpreter Execution
-`Flex` always runs its generated code through a `CodeInterpreter`. `interpreter_factory` defaults to `dspy.PythonInterpreter` (Deno/Pyodide) and must be a **zero-argument factory** returning a fresh interpreter; a bare instance is not accepted, so parallel evaluations receive isolated sessions. The factory is called once per interpreter session, including separate sessions requested by nested code-executing modules. With the default interpreter, optimizer-authored control flow, string work, arithmetic, and supported imports run inside the sandbox, and only provided-tool calls, predictor construction, and predictor calls bridge back to the host, which makes the real LM calls. Custom factories define their own trust boundary: `dspy.LocalInterpreter`, for example, separates process memory and stdout but retains the host user's filesystem, environment, credentials, network, and process authority.
+`Flex` always runs its generated code through a `CodeInterpreter`. `interpreter_factory` defaults to `dspy.PythonInterpreter` (Deno/Pyodide) and must be a **zero-argument factory** returning a fresh interpreter; a bare instance is not accepted, so parallel evaluations receive isolated sessions. `dspy.configure(interpreter_factory=...)` replaces that default. A factory passed to `Flex` wins, unless it is `PythonInterpreter`. The factory is called once per interpreter session, including separate sessions requested by nested code-executing modules. With the default interpreter, optimizer-authored control flow, string work, arithmetic, and supported imports run inside the sandbox, and only provided-tool calls, predictor construction, and predictor calls bridge back to the host, which makes the real LM calls. Custom factories define their own trust boundary: `dspy.LocalInterpreter`, for example, separates process memory and stdout but retains the host user's filesystem, environment, credentials, network, and process authority.
Because the default builds a `PythonInterpreter`, *running* a `Flex` needs [Deno](https://deno.land/) installed; without it, the call raises.
@@ -95,6 +95,13 @@ solve = dspy.Flex(
)
```
+One call puts the same interpreter behind every code-executing module in the program — a
+remote sandbox, say, or one for a host that cannot run Deno:
+
+```python
+dspy.configure(interpreter_factory=MyInterpreter) # every module with no factory of its own
+```
+
Each call owns and shuts down every interpreter session it creates, so a `Flex` holds no live sessions between calls.
## Tools
@@ -122,7 +129,7 @@ restored = dspy.Flex("invoice: str -> total_cents: int")
restored.load("solver.json") # rebinds the saved module_src
```
-The interpreter is a **runtime dependency and is not serialized**. Reconstructing with `dspy.Flex(signature)` restores the default sandbox automatically; if you optimized with a custom `interpreter_factory`, pass the same one when you reconstruct the module before calling `load`.
+The interpreter is a **runtime dependency and is not serialized**. Reconstructing with `dspy.Flex(signature)` restores the configured sandbox automatically; if you optimized with an `interpreter_factory` passed to the constructor, pass the same one when you reconstruct the module before calling `load`.
## Constructor Parameters
@@ -130,7 +137,7 @@ The interpreter is a **runtime dependency and is not serialized**. Reconstructin
|-----------|------|---------|-------------|
| `signature` | `str \| Signature` | required | Declares the module's inputs and outputs (e.g. `"invoice -> total_cents: int"`). |
| `tools` | `list[Callable \| dspy.Tool]` | `None` | Tools the generated code may call. With tools, the baseline is a `dspy.RLM`; without, a `dspy.Predict`. |
-| `interpreter_factory` | `Callable[[], CodeInterpreter]` | `PythonInterpreter` | Zero-arg factory returning a fresh `CodeInterpreter` for each interpreter session; defaults to `dspy.PythonInterpreter` (needs Deno). A bare interpreter instance is not accepted. Supported Python, libraries, and security boundaries are interpreter-dependent. |
+| `interpreter_factory` | `Callable[[], CodeInterpreter]` | `PythonInterpreter` | Zero-arg factory returning a fresh `CodeInterpreter` for each interpreter session; defaults to `dspy.PythonInterpreter` (needs Deno), and `dspy.configure(interpreter_factory=...)` replaces that default. A bare interpreter instance is not accepted. Supported Python, libraries, and security boundaries are interpreter-dependent. |
| `max_predictor_calls` | `int \| None` | `100` | Maximum number of predictor calls the generated code can make in one `forward` — a guard against runaway loops. `None` removes the limit. |
## Notes
diff --git a/docs/docs/api/modules/RLM.md b/docs/docs/api/modules/RLM.md
index bc59bf8ac..5e71fbaf5 100644
--- a/docs/docs/api/modules/RLM.md
+++ b/docs/docs/api/modules/RLM.md
@@ -111,7 +111,7 @@ $20,000,000
| `verbose` | `bool` | `False` | Log detailed execution info |
| `tools` | `list[Union[Callable, dspy.Tool]]` | `None` | Additional tool functions callable from interpreter code |
| `sub_lm` | `dspy.LM` | `None` | LM for sub-queries. Defaults to `dspy.settings.lm`. Use a cheaper model here. |
-| `interpreter_factory` | `Callable[[], CodeInterpreter]` | `PythonInterpreter` | Creates one interpreter per invocation. RLM shuts down each returned interpreter. May expose an optional `execution_instructions` string for the action prompt. |
+| `interpreter_factory` | `Callable[[], CodeInterpreter]` | `PythonInterpreter` | Creates one interpreter per invocation. RLM shuts down each returned interpreter. `dspy.configure(interpreter_factory=...)` replaces the default. A factory passed to the constructor wins, unless it is `PythonInterpreter`. May expose an optional `execution_instructions` string for the action prompt. |
## Built-in Tools
@@ -199,12 +199,20 @@ rlm = dspy.RLM(
)
```
+One call puts the same interpreter behind every code-executing module in the program. Each invocation reads the setting, so `dspy.context(interpreter_factory=...)` scopes the choice and `dspy.configure(...)` reaches modules built before the call. A factory passed to a constructor still wins, unless it is `PythonInterpreter`:
+
+```python
+dspy.configure(interpreter_factory=MyInterpreter)
+```
+
RLM creates and shuts down one interpreter from this factory per invocation. It adds invocation-scoped tools to the returned interpreter's mutable `tools` dictionary, so remote sandboxes need a `CodeInterpreter` adapter that supports that protocol. To reuse a caller-owned interpreter, pass it as the first positional argument when calling the module: `rlm(interpreter, context=data, query=query)`. RLM updates its tools and output metadata but does not shut down or restore it. Reuse is supported only for sequential calls to the same RLM instance; use the factory path for concurrency.
If the factory exposes an `execution_instructions` string, RLM adds it to the action predictor's task instructions,
which DSPy adapters place in the system prompt. Optimizers such as GEPA may therefore adapt the execution guidance
along with the rest of the action policy. Factory classes and configured callable provider objects can expose this
-metadata; anonymous factories without it continue to use the generic action prompt.
+metadata; anonymous factories without it continue to use the generic action prompt. RLM refreshes these instructions
+for each action call, so a `dspy.context(interpreter_factory=...)` override keeps the prompt aligned with the runtime
+that executes the generated code.
### Custom Sandbox-Serializable Inputs
@@ -269,7 +277,7 @@ RLM returns a `Prediction` with:
`interpreter_factory` may be called concurrently and must return a fresh interpreter each time. An interpreter passed as the first positional argument to `rlm(...)` or `rlm.acall(...)` is caller-owned and may be reused only for sequential calls to the same RLM instance. `PythonInterpreter` must also stay on the thread where it was first used.
!!! note "Interpreter Requirements"
- The default `PythonInterpreter` requires [Deno](https://deno.land/) to be installed for the Pyodide WASM sandbox.
+ RLM defaults to `PythonInterpreter`, which requires [Deno](https://deno.land/) to be installed for the Pyodide WASM sandbox.
`LocalInterpreter` requires no additional runtime and provides a separate CPython process, but it is not a
security sandbox: generated code retains the host user's files, environment, credentials, network, and process
authority.
diff --git a/docs/docs/api/utils/configure.md b/docs/docs/api/utils/configure.md
index 73f8e5414..a01cb74c3 100644
--- a/docs/docs/api/utils/configure.md
+++ b/docs/docs/api/utils/configure.md
@@ -31,6 +31,7 @@ Every DSPy module will use these defaults unless you override them with
| `allow_tool_async_sync_conversion` | `False` | Let async tools run in synchronous code. See [Async](../../tutorials/async/index.md). |
| `provide_traceback` | `False` | Include Python tracebacks in error logs. |
| `warn_on_type_mismatch` | `True` | Warn when a module input type does not match the signature. |
+| `interpreter_factory` | `None` | Replaces the default `dspy.PythonInterpreter` factory for code-executing modules ([`dspy.RLM`](../modules/RLM.md), [`dspy.Flex`](../modules/Flex.md), `dspy.ProgramOfThought`, `dspy.CodeAct`). A module's own factory wins, unless it is `PythonInterpreter`. Modules fall back to [`dspy.PythonInterpreter`](../../diving-deeper/rlm.md) when nothing is configured. |
## Examples
diff --git a/docs/docs/diving-deeper/built-in-module-variants.md b/docs/docs/diving-deeper/built-in-module-variants.md
index dd836a303..f2bacfb47 100644
--- a/docs/docs/diving-deeper/built-in-module-variants.md
+++ b/docs/docs/diving-deeper/built-in-module-variants.md
@@ -78,7 +78,7 @@ The constructor mutates the input signature: it prepends one output field (a syn
For tasks where the answer is best computed, not narrated.
-Each module accepts an `interpreter_factory` that is called once per invocation; DSPy shuts down the returned interpreter even when the invocation raises. Passing an interpreter as the first positional argument when calling the module, such as `program(interpreter, **inputs)`, instead uses that caller-owned instance without shutting it down. Caller-owned reuse is sequential; use the factory path for concurrent invocations. A `PythonInterpreter` override must also stay on the thread where it was first used.
+Each module accepts an `interpreter_factory` that is called once per invocation; DSPy shuts down the returned interpreter even when the invocation raises. `PythonInterpreter` is the public default, and `dspy.configure(interpreter_factory=...)` replaces it at each invocation. A module's own factory wins, unless it is `PythonInterpreter`. Passing an interpreter as the first positional argument when calling the module, such as `program(interpreter, **inputs)`, instead uses that caller-owned instance without shutting it down. Caller-owned reuse is sequential; use the factory path for concurrent invocations. A `PythonInterpreter` override must also stay on the thread where it was first used.
**`dspy.ProgramOfThought(signature, max_iters=3, interpreter_factory=PythonInterpreter)`**
Holds three internal `ChainOfThought` predictors: `code_generate` produces Python, `code_regenerate` rewrites it after a recoverable execution error, and `generate_output` extracts the declared output fields from the run’s printed result. The forward loop asks `code_generate` for code, runs it through the `PythonInterpreter`, and feeds `CodeExecutionError` or `SyntaxError` back to `code_regenerate` for up to `max_iters` rounds. A terminal `CodeInterpreterError` propagates immediately. Once execution succeeds, `generate_output` produces the signature’s output fields. If `max_iters` is exhausted, the module raises.
diff --git a/docs/docs/diving-deeper/flex.md b/docs/docs/diving-deeper/flex.md
index b1116893f..4d8a41b8a 100644
--- a/docs/docs/diving-deeper/flex.md
+++ b/docs/docs/diving-deeper/flex.md
@@ -34,7 +34,7 @@ The reflection model authors code, and code can be wrong. A candidate that doesn
### 7. Generated code always runs through an interpreter
-`interpreter_factory` defaults to `dspy.PythonInterpreter` (Deno/Pyodide) and must be a *zero-argument factory* — a bare instance or `None` is rejected. With the default, everything stays in the sandbox except provided-tool calls, predictor construction, and predictor calls, which bridge back to the host. Custom factories define their own trust boundary: `LocalInterpreter` separates process memory and stdout but retains the host user's filesystem, environment, credentials, network, and process authority. The factory creates a fresh interpreter for each session; a forward owns an outer session and nested code-executing modules may request separate sessions. Source portability between custom interpreters is not guaranteed. `max_predictor_calls` caps how many predictor calls the generated code can make in one `forward`.
+`Flex` takes its interpreter from `interpreter_factory`, else from `dspy.settings.interpreter_factory`, else from `dspy.PythonInterpreter` (Deno/Pyodide). Each of those must be a *zero-argument factory* — a bare instance or `None` is rejected. With the default, everything stays in the sandbox except provided-tool calls, predictor construction, and predictor calls, which bridge back to the host. Custom factories define their own trust boundary: `LocalInterpreter` separates process memory and stdout but retains the host user's filesystem, environment, credentials, network, and process authority. The factory creates a fresh interpreter for each session; a forward owns an outer session and nested code-executing modules may request separate sessions. Source portability between custom interpreters is not guaranteed. `max_predictor_calls` caps how many predictor calls the generated code can make in one `forward`.
### 8. The declared output types are enforced at the sandbox boundary
@@ -42,7 +42,7 @@ Everything the generated code returns crosses back as JSON, so a field declared
### 9. The code is state; the interpreter is a runtime dependency
-A saved `Flex` is `{"module_src": ..., "lm": ...}` — the code, plus any LM set directly on the module. The internal predictors are not saved: they are derived from the code, and each `forward` reconstructs them from the bound source. The interpreter is a live runtime resource and is not serialized; `save(path, save_program=True)` cloudpickles the program with the bridge excluded, and loading rebuilds it. Reconstructing with `dspy.Flex(signature)` restores the default sandbox, so you only re-supply the `interpreter_factory` before `load` if you optimized with a custom one.
+A saved `Flex` is `{"module_src": ..., "lm": ...}` — the code, plus any LM set directly on the module. The internal predictors are not saved: they are derived from the code, and each `forward` reconstructs them from the bound source. The interpreter is a live runtime resource and is not serialized; `save(path, save_program=True)` cloudpickles the program with the bridge excluded, and loading rebuilds it. Reconstructing with `dspy.Flex(signature)` restores the configured sandbox, so you only re-supply the `interpreter_factory` before `load` if you optimized with one passed to the constructor.
### 10. Flex is experimental and the interface is in flux
@@ -53,7 +53,7 @@ The class carries the `@experimental` decorator. Treat the API and serialization
### Defining and running a Flex
**`dspy.Flex(signature, *, tools=None, interpreter_factory=PythonInterpreter, max_predictor_calls=100)`**
-Parses the signature and binds the baseline source — a single `dspy.Predict` over the signature, or a `dspy.RLM` when `tools` are given. Validates `interpreter_factory` (a zero-arg factory, defaulting to `dspy.PythonInterpreter`), and sets up the sandbox bridge.
+Parses the signature and binds the baseline source — a single `dspy.Predict` over the signature, or a `dspy.RLM` when `tools` are given. Validates `interpreter_factory` as a zero-arg factory and sets up the sandbox bridge. `dspy.configure(interpreter_factory=...)` replaces the default. A factory passed to `Flex` wins, unless it is `PythonInterpreter`.
**`__call__(**inputs)` / `forward(**inputs)`**
Runs the currently bound source inside the interpreter, bridging predictor calls back to the host. Returns a `dspy.Prediction` over the signature's output fields, and accepts keyword inputs only.
@@ -78,7 +78,7 @@ Add `program_trace=None` as a sixth parameter to your metric and GEPA passes the
Plain functions or `dspy.Tool` instances, referenced by name in the generated code, so each name must be a valid Python identifier. Providing tools makes the baseline a `dspy.RLM` and tells the code proposer they are in scope — it can wire them into `dspy.RLM`/`dspy.ReAct`, call them directly, or supplement them with its own inline helpers.
**`interpreter_factory=...`**
-Defaults to `dspy.PythonInterpreter` (sandboxed, needs Deno). Must be a zero-argument callable returning a fresh `CodeInterpreter` for each interpreter session; parallel evaluations and nested code-executing modules can therefore receive isolated sessions. As in `dspy.RLM`, a bare interpreter instance is not accepted. This low-level hook does not guarantee source, standard-library portability, or a particular security boundary between different interpreters.
+Defaults to `dspy.PythonInterpreter` (sandboxed, needs Deno). `dspy.configure(interpreter_factory=...)` replaces that default on each interpreter session, including for a `Flex` built before the call. Must be a zero-argument callable returning a fresh `CodeInterpreter` for each interpreter session; parallel evaluations and nested code-executing modules can therefore receive isolated sessions. As in `dspy.RLM`, a bare interpreter instance is not accepted. This low-level hook does not guarantee source, standard-library portability, or a particular security boundary between different interpreters.
**`max_predictor_calls`**
The maximum number of predictor calls the generated code can make in one `forward`. It guards against runaway loops. `None` removes the limit.
@@ -109,7 +109,7 @@ Anything outside this surface fails when the candidate runs, and GEPA scores it
### Saving and loading
**`save(path)` / `load(path)` / `dump_state()` / `load_state(state)`**
-`module_src` travels in the serialized state (along with any LM set on the module), so loading restores the optimized code; the predictors are rebuilt from it on each `forward`. The interpreter is not serialized; reconstructing with `dspy.Flex(signature)` restores the default sandbox, so re-supply it in the constructor before `load` only if you used a custom `interpreter_factory`.
+`module_src` travels in the serialized state (along with any LM set on the module), so loading restores the optimized code; the predictors are rebuilt from it on each `forward`. The interpreter is not serialized; reconstructing with `dspy.Flex(signature)` restores the configured sandbox, so re-supply it in the constructor before `load` only if you passed a custom `interpreter_factory` there.
## Cross-links
diff --git a/docs/docs/diving-deeper/rlm.md b/docs/docs/diving-deeper/rlm.md
index 09fb1f928..97526215b 100644
--- a/docs/docs/diving-deeper/rlm.md
+++ b/docs/docs/diving-deeper/rlm.md
@@ -32,7 +32,7 @@ The model steering the loop and the model answering snippets need not be the sam
### 7. Generated code runs in a pluggable interpreter
-The default `PythonInterpreter` executes Python in a Deno and Pyodide WASM sandbox with no filesystem or network access. Each `forward()` creates one interpreter, uses it for the whole REPL loop, and shuts it down afterward. Supply `interpreter_factory=` to configure another `CodeInterpreter`. `LocalInterpreter` offers ordinary CPython in a separate local process, but it is for trusted code because it retains the host user's filesystem, environment, credentials, network, and process authority.
+The default `PythonInterpreter` executes Python in a Deno and Pyodide WASM sandbox with no filesystem or network access. Each `forward()` creates one interpreter, uses it for the whole REPL loop, and shuts it down afterward. Supply `interpreter_factory=` to use another `CodeInterpreter`, or `dspy.configure(interpreter_factory=...)` to choose one for every code-executing module at once. A deployment that cannot run Deno replaces the default that way. `LocalInterpreter` offers ordinary CPython in a separate local process, but it is for trusted code because it retains the host user's filesystem, environment, credentials, network, and process authority.
### 8. `SandboxSerializable` loads large inputs into the sandbox once
@@ -90,7 +90,7 @@ The model for `llm_query` and `llm_query_batched`. Left unset it falls back to `
A list of plain functions or `dspy.Tool` objects. RLM normalizes each to a `Tool`, rejects names that aren’t valid identifiers or that collide with the built-ins, and documents their signatures in the action prompt. The model calls them as ordinary Python inside its code.
**`interpreter_factory=...`**
-A zero-argument callable that returns a fresh `CodeInterpreter` for one invocation. RLM may call the factory concurrently, and it always shuts down the returned interpreter. A class such as `PythonInterpreter` or `LocalInterpreter` is already a factory; use `functools.partial` or a callable provider object when construction needs configuration. RLM adds invocation-scoped tools to the returned interpreter's mutable `tools` dictionary, so remote sandboxes need a `CodeInterpreter` adapter that supports that protocol.
+A zero-argument callable that returns a fresh `CodeInterpreter` for one invocation. RLM may call the factory concurrently, and it always shuts down the returned interpreter. A class such as `PythonInterpreter` or `LocalInterpreter` is already a factory; use `functools.partial` or a callable provider object when construction needs configuration. RLM adds invocation-scoped tools to the returned interpreter's mutable `tools` dictionary, so remote sandboxes need a `CodeInterpreter` adapter that supports that protocol. `dspy.configure(interpreter_factory=...)` replaces the default on each `forward()`, so `dspy.context(interpreter_factory=...)` scopes the choice. If the active factory exposes an `execution_instructions` string, RLM refreshes it for each action call so the prompt matches the runtime executing the generated code.
**`__call__(interpreter, **inputs)` / `acall(interpreter, **inputs)`**
An escape hatch for a caller-owned interpreter, supplied as the first positional argument. RLM mutates its `tools` dictionary and, when supported, its output-field metadata, but does not shut down or restore the instance. Reuse is supported only for sequential calls to the same RLM instance, so retained variables and tool registrations stay within one program and trust boundary. Use `interpreter_factory` for concurrent invocations. A `PythonInterpreter` override must also stay on the thread where it was first used.
diff --git a/docs/docs/diving-deeper/settings-and-context.md b/docs/docs/diving-deeper/settings-and-context.md
index f8962b01e..b05bcd437 100644
--- a/docs/docs/diving-deeper/settings-and-context.md
+++ b/docs/docs/diving-deeper/settings-and-context.md
@@ -86,6 +86,7 @@ Standard keys (defined in `DEFAULT_CONFIG`):
| `stream_listeners` / `send_stream` | `[]` / `None` | Streaming wiring. |
| `allow_tool_async_sync_conversion` | `False` | Permit running async tools inside sync code. |
| `branch_idx` | `0` | Rollout / branch index used by some optimizers. |
+| `interpreter_factory` | `None` (uses `PythonInterpreter`) | Replaces the default `PythonInterpreter` factory for code-executing modules. A module's own factory wins, unless it is `PythonInterpreter`. |
Set-by-DSPy keys (don’t set these yourself):
diff --git a/dspy/dsp/utils/settings.py b/dspy/dsp/utils/settings.py
index 47bceba4d..61aa81ace 100644
--- a/dspy/dsp/utils/settings.py
+++ b/dspy/dsp/utils/settings.py
@@ -35,6 +35,8 @@ DEFAULT_CONFIG = dotdict(
max_history_size=10000,
max_trace_size=10000,
warn_on_type_mismatch=True, # Whether to log warnings when a module's input type doesn't match the signature type.
+ # Replaces `dspy.PythonInterpreter` in code-executing modules that carry no factory of their own.
+ interpreter_factory=None,
)
# Global base configuration and owner tracking
diff --git a/dspy/predict/code_act.py b/dspy/predict/code_act.py
index 118561cde..671efbaf5 100644
--- a/dspy/predict/code_act.py
+++ b/dspy/predict/code_act.py
@@ -37,7 +37,8 @@ class CodeAct(ReAct, ProgramOfThought):
tools (list[Callable]): The tool callables to be used. CodeAct only accepts functions and not callable objects.
max_iters (int): The maximum number of iterations to generate the answer.
interpreter_factory: Zero-argument callable that creates an interpreter for each forward pass. The
- callable may be invoked concurrently, and DSPy shuts down each interpreter it returns.
+ callable may be invoked concurrently, and DSPy shuts down each interpreter it returns. Defaults to
+ ``dspy.PythonInterpreter``. ``dspy.configure(interpreter_factory=...)`` replaces this default.
Examples:
```python
from dspy.predict import CodeAct
diff --git a/dspy/predict/flex/bridge.py b/dspy/predict/flex/bridge.py
index cc3a63b1f..ea163839d 100644
--- a/dspy/predict/flex/bridge.py
+++ b/dspy/predict/flex/bridge.py
@@ -3,7 +3,8 @@
A ``Flex`` never executes its optimizer-authored ``module_src`` in the host process. Instead,
every ``forward``:
-1. creates a fresh interpreter from the Flex's ``interpreter_factory`` (``BridgeRuntime.forward``);
+1. creates a fresh interpreter from the Flex's ``interpreter_factory``, or from
+ ``dspy.settings.interpreter_factory`` when Flex uses its default (``BridgeRuntime.forward``);
2. injects the sandbox-side shim (``_sandbox_shim.py``), which fakes a tiny ``dspy`` module whose
predictor constructors and calls are proxies;
3. executes ``module_src`` and drives its ``forward`` with the call's inputs.
@@ -358,8 +359,9 @@ class BridgeRuntime:
# A code-executing sub-predictor should run its inner code in the backend chosen for Flex, so
# hand it the Flex interpreter factory (it makes and tears down a fresh interpreter per forward).
# The sandbox code can't set this itself, since a live interpreter can't cross the boundary.
+ # The resolver lets a configured factory override the default for Flex and its
+ # code-executing sub-predictors alike.
if "interpreter_factory" not in extra and _accepts_interpreter_factory(cls):
factory = self._sub_interpreter_factory()
- if factory is not None:
- extra["interpreter_factory"] = factory
+ extra["interpreter_factory"] = factory
return cls(_resolve_signature(signature, self._flex._flex_ctx.custom_types()), **extra)
diff --git a/dspy/predict/flex/flex.py b/dspy/predict/flex/flex.py
index ea3586fc3..ecdd5d850 100644
--- a/dspy/predict/flex/flex.py
+++ b/dspy/predict/flex/flex.py
@@ -26,8 +26,9 @@ class Flex(Module, Parameter):
plus plain Python instead of only tuning instructions.
The optimizer-authored code runs inside an interpreter. ``Flex`` never
- runs it in the host Python process. ``interpreter_factory`` defaults to ``dspy.PythonInterpreter``
- (Deno/Pyodide) and must be a zero-argument callable returning a new ``CodeInterpreter``.
+ runs it in the host Python process. ``interpreter_factory`` defaults to
+ ``dspy.PythonInterpreter`` (Deno/Pyodide) and must be a zero-argument callable returning a new
+ ``CodeInterpreter``. ``dspy.configure(interpreter_factory=...)`` replaces that default.
Flex may validate or lower source and install its guest shim before execution; a custom
interpreter therefore defines the Python and standard-library subset available to that source.
The optimizer-authored glue runs isolated; only provided-tool calls, predictor construction,
@@ -38,6 +39,7 @@ class Flex(Module, Parameter):
tools: ``dspy.Tool`` instances or named callables.
interpreter_factory: Zero-argument callable returning a fresh ``CodeInterpreter`` for each
interpreter session. Defaults to ``dspy.PythonInterpreter`` (sandbox, requires Deno).
+ ``dspy.configure(interpreter_factory=...)`` replaces the default.
max_predictor_calls: Maximum number of predictor calls the optimizer-authored code can
make per ``forward`. ``None`` removes the limit.
"""
diff --git a/dspy/predict/program_of_thought.py b/dspy/predict/program_of_thought.py
index fbc157846..599764822 100644
--- a/dspy/predict/program_of_thought.py
+++ b/dspy/predict/program_of_thought.py
@@ -52,7 +52,8 @@ class ProgramOfThought(Module):
signature: The signature of the module.
max_iters: The maximum number of iterations to retry code generation and execution.
interpreter_factory: Zero-argument callable that creates an interpreter for each forward pass. The
- callable may be invoked concurrently, and DSPy shuts down each interpreter it returns.
+ callable may be invoked concurrently, and DSPy shuts down each interpreter it returns. Defaults to
+ ``dspy.PythonInterpreter``. ``dspy.configure(interpreter_factory=...)`` replaces this default.
"""
super().__init__()
warnings.warn(
diff --git a/dspy/predict/rlm.py b/dspy/predict/rlm.py
index 559dcd2f2..334b177fc 100644
--- a/dspy/predict/rlm.py
+++ b/dspy/predict/rlm.py
@@ -34,6 +34,7 @@ from dspy.primitives.code_interpreter import (
_create_interpreter,
_validate_interpreter,
_validate_interpreter_factory,
+ resolve_interpreter_factory,
)
from dspy.primitives.module import Module
from dspy.primitives.prediction import Prediction
@@ -120,12 +121,13 @@ class RLM(Module):
through code execution. The LLM writes Python code to examine data, call
sub-LLMs for semantic analysis, and build up answers iteratively.
- The default interpreter is PythonInterpreter (Deno/Pyodide/WASM), but
- ``interpreter_factory`` can create another CodeInterpreter implementation,
- such as an adapter for a remote sandbox. RLM updates the interpreter's
- mutable ``tools`` dictionary with invocation-scoped tools before execution.
- A caller-owned interpreter may be reused sequentially with the same RLM
- instance, but must not be shared by overlapping invocations.
+ ``interpreter_factory`` defaults to ``PythonInterpreter`` (Deno/Pyodide/WASM), and
+ ``dspy.configure(interpreter_factory=...)`` replaces that default. Either route
+ accepts an adapter for a remote sandbox.
+ RLM updates the interpreter's mutable ``tools`` dictionary with
+ invocation-scoped tools before execution. A caller-owned interpreter may be
+ reused sequentially with the same RLM instance, but must not be shared by
+ overlapping invocations.
Examples:
```python
@@ -162,7 +164,10 @@ class RLM(Module):
interpreter_factory: Zero-argument callable that creates an interpreter for each forward pass. The
callable may be invoked concurrently, and DSPy shuts down each interpreter it returns. RLM updates
the returned interpreter's mutable ``tools`` dictionary before execution. The callable may expose
- an ``execution_instructions`` string describing its runtime for the action prompt.
+ an ``execution_instructions`` string describing its runtime for the action prompt. RLM applies the
+ active factory's instructions to each action call, so ``dspy.context`` can switch runtimes
+ without changing the shared predictor signature. Defaults to ``dspy.PythonInterpreter``;
+ ``dspy.configure(interpreter_factory=...)`` replaces the default.
"""
super().__init__()
_validate_interpreter_factory(interpreter_factory)
@@ -178,6 +183,7 @@ class RLM(Module):
# Build the action and extract signatures
action_sig, extract_sig = self._build_signatures()
+ self._action_signature = action_sig
self.generate_action = dspy.Predict(action_sig)
self.extract = dspy.Predict(extract_sig)
@@ -351,10 +357,13 @@ class RLM(Module):
# Format tool documentation for user-provided tools
tool_docs = self._format_tool_docs(self._user_tools)
- execution_instructions = getattr(self._interpreter_factory, "execution_instructions", "")
- if not isinstance(execution_instructions, str):
- raise TypeError("interpreter_factory.execution_instructions must be a string")
- interpreter_rules = f"\nExecution environment:\n{execution_instructions}\n" if execution_instructions else ""
+ # Seed with the factory active at construction; each action call refreshes this
+ # fragment, so a later dspy.context() can select a different runtime.
+ factory = resolve_interpreter_factory(self._interpreter_factory)
+ execution_instructions = self._get_execution_instructions(factory)
+ self._initial_execution_instructions = execution_instructions
+ self._initial_interpreter_rules = self._format_interpreter_rules(execution_instructions)
+ interpreter_rules = self._initial_interpreter_rules
action_sig = (
dspy.Signature({}, task_instructions + ACTION_INSTRUCTIONS_TEMPLATE.format(
@@ -388,6 +397,42 @@ class RLM(Module):
return action_sig, extract_sig
+ @staticmethod
+ def _format_interpreter_rules(execution_instructions: str) -> str:
+ return f"\nExecution environment:\n{execution_instructions}\n" if execution_instructions else ""
+
+ @staticmethod
+ def _get_execution_instructions(factory: Callable[[], CodeInterpreter]) -> str:
+ execution_instructions = getattr(factory, "execution_instructions", "")
+ if not isinstance(execution_instructions, str):
+ raise TypeError("interpreter_factory.execution_instructions must be a string")
+ return execution_instructions
+
+ def _action_signature_for_current_factory(self) -> type[Signature]:
+ """Return an action signature whose runtime guidance matches the active interpreter."""
+ factory = resolve_interpreter_factory(self._interpreter_factory)
+ execution_instructions = self._get_execution_instructions(factory)
+ # getattr, because generate_action may have been replaced by a predictor that
+ # carries no signature of its own.
+ current_signature = getattr(self.generate_action, "signature", self._action_signature)
+
+ # Same runtime as at construction: no derived signature per iteration, and an
+ # optimizer's revised instructions stay untouched.
+ if execution_instructions == self._initial_execution_instructions:
+ return current_signature
+
+ # Replace only DSPy's own fragment, so an override cannot stack on stale guidance.
+ current_instructions = current_signature.instructions
+ current_rules = self._format_interpreter_rules(execution_instructions)
+ if self._initial_interpreter_rules and self._initial_interpreter_rules in current_instructions:
+ instructions = current_instructions.replace(self._initial_interpreter_rules, current_rules, 1)
+ elif current_rules:
+ # An optimizer dropped that fragment, so append the active guidance once instead.
+ instructions = current_instructions + current_rules
+ else:
+ instructions = current_instructions
+ return current_signature.with_instructions(instructions)
+
# =========================================================================
# Input/Output Processing
# =========================================================================
@@ -674,7 +719,10 @@ class RLM(Module):
) -> Prediction | REPLHistory:
"""Execute one iteration. Returns Prediction if done, else updated REPLHistory."""
variables_info = [variable.format() for variable in variables]
+ # A per-call signature, not a mutation of generate_action.signature, keeps a
+ # dspy.context override local to this invocation.
action = self.generate_action(
+ signature=self._action_signature_for_current_factory(),
variables_info=variables_info,
repl_history=history,
iteration=f"{iteration + 1}/{self.max_iters}",
@@ -768,6 +816,7 @@ class RLM(Module):
"""Async version: Execute one iteration."""
variables_info = [variable.format() for variable in variables]
pred = await self.generate_action.acall(
+ signature=self._action_signature_for_current_factory(),
variables_info=variables_info,
repl_history=history,
iteration=f"{iteration + 1}/{self.max_iters}",
diff --git a/dspy/primitives/__init__.py b/dspy/primitives/__init__.py
index dc768cfa2..e8e1d556c 100644
--- a/dspy/primitives/__init__.py
+++ b/dspy/primitives/__init__.py
@@ -1,5 +1,11 @@
from dspy.primitives.base_module import BaseModule
-from dspy.primitives.code_interpreter import CodeExecutionError, CodeInterpreter, CodeInterpreterError, FinalOutput
+from dspy.primitives.code_interpreter import (
+ CodeExecutionError,
+ CodeInterpreter,
+ CodeInterpreterError,
+ FinalOutput,
+ resolve_interpreter_factory,
+)
from dspy.primitives.example import Example
from dspy.primitives.local_interpreter import LocalInterpreter
from dspy.primitives.module import Module
@@ -20,4 +26,5 @@ __all__ = [
"Prediction",
"PythonInterpreter",
"SandboxSerializable",
+ "resolve_interpreter_factory",
]
diff --git a/dspy/primitives/code_interpreter.py b/dspy/primitives/code_interpreter.py
index a5f232227..0197240c9 100644
--- a/dspy/primitives/code_interpreter.py
+++ b/dspy/primitives/code_interpreter.py
@@ -6,10 +6,15 @@ code-executing modules to work with different interpreter implementations:
- PythonInterpreter: Local Deno/Pyodide WASM interpreter
- LocalInterpreter: Persistent local CPython worker
- MockInterpreter: Scriptable responses for testing
+
+It also resolves which implementation a module gets: a module's own
+``interpreter_factory`` argument, else ``dspy.settings.interpreter_factory``, else
+``PythonInterpreter``. See :func:`resolve_interpreter_factory`.
"""
from typing import Any, Callable, Protocol, runtime_checkable
+from dspy.dsp.utils.settings import settings
from dspy.utils.exceptions import DSPyError
# Types that can be used directly in Python function signatures for SUBMIT()
@@ -149,24 +154,53 @@ class CodeInterpreter(Protocol):
...
-def _validate_interpreter_factory(factory: Any) -> None:
+def _validate_interpreter_factory(factory: Any, name: str = "interpreter_factory") -> None:
"""Validate the configured provider without invoking it."""
if not isinstance(factory, type) and isinstance(factory, CodeInterpreter):
raise TypeError(
- "interpreter_factory received an object that already implements CodeInterpreter, so its ownership "
+ f"{name} received an object that already implements CodeInterpreter, so its ownership "
"is ambiguous. Pass an existing interpreter as the first positional argument when calling the module. "
"If this object also creates interpreters, pass a dedicated zero-argument creation callable instead."
)
if not callable(factory):
raise TypeError(
- "interpreter_factory must be a zero-argument callable that creates a CodeInterpreter, "
+ f"{name} must be a zero-argument callable that creates a CodeInterpreter, "
f"not {type(factory).__name__}."
)
-def _create_interpreter(factory: Callable[[], CodeInterpreter]) -> CodeInterpreter:
- """Create an interpreter and validate the factory's return value."""
- interpreter = factory()
+def resolve_interpreter_factory(
+ factory: Callable[[], CodeInterpreter] | None = None,
+) -> Callable[[], CodeInterpreter]:
+ """Return the factory that creates the next interpreter.
+
+ The module's own factory wins, then ``dspy.settings.interpreter_factory``, then
+ ``PythonInterpreter``. ``None`` and ``PythonInterpreter`` both mean "unset", since the
+ code-executing modules carry ``PythonInterpreter`` as their literal default argument.
+
+ Call this where you create an interpreter, not earlier, so that
+ ``dspy.context(interpreter_factory=...)`` scopes the way callers expect.
+
+ Raises:
+ TypeError: If ``dspy.settings.interpreter_factory`` is not a zero-argument callable.
+ """
+ # Imported here because python_interpreter imports this module.
+ from dspy.primitives.python_interpreter import PythonInterpreter
+
+ if factory is not None and factory is not PythonInterpreter:
+ return factory
+
+ configured = settings.get("interpreter_factory")
+ if configured is None:
+ return PythonInterpreter
+
+ _validate_interpreter_factory(configured, name="dspy.settings.interpreter_factory")
+ return configured
+
+
+def _create_interpreter(factory: Callable[[], CodeInterpreter] | None) -> CodeInterpreter:
+ """Create an interpreter from ``factory``, or from the configured one, and validate it."""
+ interpreter = resolve_interpreter_factory(factory)()
if not isinstance(interpreter, CodeInterpreter):
raise TypeError(
"interpreter_factory must return a CodeInterpreter, "
diff --git a/tests/flex/test_flex_interpreter.py b/tests/flex/test_flex_interpreter.py
index 1ed0848f8..76da1e92c 100644
--- a/tests/flex/test_flex_interpreter.py
+++ b/tests/flex/test_flex_interpreter.py
@@ -26,7 +26,11 @@ import pytest
import dspy
from dspy.predict.flex import Flex, bridge
-from dspy.primitives.code_interpreter import CodeExecutionError, CodeInterpreterError
+from dspy.primitives.code_interpreter import (
+ CodeExecutionError,
+ CodeInterpreterError,
+ resolve_interpreter_factory,
+)
from dspy.utils.dummies import DummyLM
from dspy.utils.exceptions import LMError, LMRateLimitError
from tests.mock_interpreter import MockInterpreter
@@ -59,8 +63,7 @@ class ShoutSig(dspy.Signature):
def test_default_interpreter_factory_is_python_interpreter() -> None:
- # Like dspy.RLM, interpreter_factory defaults to dspy.PythonInterpreter (a class used as a
- # zero-arg factory). Inspect the constructor default so this needs no Deno.
+ # PythonInterpreter is the public default. The resolver lets a configured factory override it.
default = inspect.signature(Flex.__init__).parameters["interpreter_factory"].default
assert default is dspy.PythonInterpreter
@@ -71,7 +74,7 @@ def test_default_is_sandbox() -> None:
flex = Flex(Doubler)
assert flex._bridge is not None
assert flex._interpreter_factory is dspy.PythonInterpreter
- assert isinstance(flex._interpreter_factory(), dspy.PythonInterpreter)
+ assert isinstance(resolve_interpreter_factory(flex._interpreter_factory)(), dspy.PythonInterpreter)
def test_bare_instance_is_rejected() -> None:
@@ -82,7 +85,7 @@ def test_bare_instance_is_rejected() -> None:
def test_none_interpreter_factory_is_rejected() -> None:
- # None used to mean "run in-process"; that path is gone, so None is now invalid.
+ # None once meant "run in-process"; that path is gone, so it remains invalid.
with pytest.raises(TypeError):
Flex(Doubler, interpreter_factory=None) # type: ignore[arg-type]
diff --git a/tests/predict/test_rlm.py b/tests/predict/test_rlm.py
index b6e50b2a9..704a3dccb 100644
--- a/tests/predict/test_rlm.py
+++ b/tests/predict/test_rlm.py
@@ -15,7 +15,12 @@ import pytest
import dspy
from dspy.adapters.types.tool import Tool
from dspy.predict.rlm import RLM, _strip_code_fences
-from dspy.primitives.code_interpreter import CodeExecutionError, CodeInterpreterError, FinalOutput
+from dspy.primitives.code_interpreter import (
+ CodeExecutionError,
+ CodeInterpreterError,
+ FinalOutput,
+ resolve_interpreter_factory,
+)
from dspy.primitives.prediction import Prediction
from dspy.primitives.python_interpreter import PythonInterpreter
from dspy.primitives.repl_types import REPLEntry, REPLHistory, REPLVariable
@@ -229,7 +234,10 @@ class TestRLMInitialization:
rlm = RLM("context -> answer")
assert rlm.max_llm_calls == 50
assert rlm.sub_lm is None
+ # PythonInterpreter is the public default; the resolver lets the configured factory
+ # override it on each forward.
assert rlm._interpreter_factory is PythonInterpreter
+ assert resolve_interpreter_factory(rlm._interpreter_factory) is PythonInterpreter
# Test custom values
mock_lm = dspy.LM("openai/gpt-4o-mini")
diff --git a/tests/primitives/test_interpreter_factory_config.py b/tests/primitives/test_interpreter_factory_config.py
new file mode 100644
index 000000000..ce6288e97
--- /dev/null
+++ b/tests/primitives/test_interpreter_factory_config.py
@@ -0,0 +1,224 @@
+"""`dspy.settings.interpreter_factory` replaces the default interpreter for code-executing modules.
+
+A module's non-default `interpreter_factory` still wins. The public default remains
+`PythonInterpreter`; internally, the configured factory replaces it at runtime, so a deployment that cannot run a wasm
+sandbox with no subprocesses, say) configures one interpreter once rather than passing it
+to every module or patching DSPy's internals.
+"""
+
+import inspect
+import re
+import warnings
+
+import pytest
+
+import dspy
+from dspy.predict.flex.flex import Flex
+from dspy.primitives.code_interpreter import (
+ FinalOutput,
+ _create_interpreter,
+ resolve_interpreter_factory,
+)
+from dspy.primitives.python_interpreter import PythonInterpreter
+from tests.mock_interpreter import MockInterpreter, MockInterpreterFactory
+
+
+class Doubler(dspy.Signature):
+ value: int = dspy.InputField()
+ result: int = dspy.OutputField()
+
+
+class BasicQA(dspy.Signature):
+ question: str = dspy.InputField()
+ answer: str = dspy.OutputField()
+
+
+class StaticPredictor:
+ def __init__(self, **fields):
+ self.fields = fields
+
+ def __call__(self, **kwargs):
+ return dspy.Prediction(**self.fields)
+
+
+def _program_of_thought(**kwargs):
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", DeprecationWarning)
+ return dspy.ProgramOfThought(BasicQA, **kwargs)
+
+
+def _code_act(**kwargs):
+ def add(a: float, b: float) -> float:
+ """add two numbers"""
+ return a + b
+
+ with warnings.catch_warnings():
+ warnings.simplefilter("ignore", DeprecationWarning)
+ return dspy.CodeAct(BasicQA, tools=[add], **kwargs)
+
+
+# =============================================================================
+# resolve_interpreter_factory
+# =============================================================================
+
+
+def test_python_interpreter_is_the_unconfigured_default():
+ assert dspy.settings.interpreter_factory is None
+ assert resolve_interpreter_factory() is PythonInterpreter
+
+
+def test_code_executing_module_signatures_keep_python_interpreter_as_the_default():
+ assert inspect.signature(dspy.RLM.__init__).parameters["interpreter_factory"].default is PythonInterpreter
+ assert inspect.signature(dspy.Flex.__init__).parameters["interpreter_factory"].default is PythonInterpreter
+ assert inspect.signature(dspy.ProgramOfThought.__init__).parameters["interpreter_factory"].default is PythonInterpreter
+ assert inspect.signature(dspy.CodeAct.__init__).parameters["interpreter_factory"].default is PythonInterpreter
+
+
+def test_configured_factory_is_used_when_the_caller_has_none():
+ factory = MockInterpreterFactory()
+ dspy.configure(interpreter_factory=factory)
+
+ assert resolve_interpreter_factory(None) is factory
+ assert isinstance(_create_interpreter(None), MockInterpreter)
+
+
+def test_configured_factory_overrides_python_interpreter_default():
+ configured = MockInterpreterFactory()
+ dspy.configure(interpreter_factory=configured)
+
+ assert resolve_interpreter_factory(PythonInterpreter) is configured
+ assert resolve_interpreter_factory(MockInterpreter) is MockInterpreter
+
+
+def test_context_scopes_the_choice_and_restores_it():
+ outer = MockInterpreterFactory()
+ inner = MockInterpreterFactory()
+ dspy.configure(interpreter_factory=outer)
+
+ with dspy.context(interpreter_factory=inner):
+ assert resolve_interpreter_factory() is inner
+
+ assert resolve_interpreter_factory() is outer
+
+
+def test_a_configured_interpreter_instance_is_rejected_by_name():
+ dspy.configure(interpreter_factory=MockInterpreter())
+
+ with pytest.raises(TypeError, match=re.escape("dspy.settings.interpreter_factory received an object")):
+ resolve_interpreter_factory()
+
+
+def test_a_configured_non_callable_is_rejected_by_name():
+ dspy.configure(interpreter_factory=123)
+
+ with pytest.raises(TypeError, match=re.escape("dspy.settings.interpreter_factory must be a zero-argument callable")):
+ resolve_interpreter_factory()
+
+
+# =============================================================================
+# The modules that execute code
+# =============================================================================
+
+
+def test_rlm_forward_uses_the_configured_factory():
+ factory = MockInterpreterFactory(responses=[FinalOutput({"answer": "42"})])
+ dspy.configure(interpreter_factory=factory)
+
+ rlm = dspy.RLM("context -> answer")
+ assert rlm._interpreter_factory is PythonInterpreter
+ rlm.generate_action = StaticPredictor(reasoning="submit", code='SUBMIT("42")')
+
+ assert rlm(context="ignored").answer == "42"
+ assert len(factory.instances) == 1
+
+
+def test_rlm_reads_execution_instructions_from_the_configured_factory():
+ # The initial action prompt includes the instructions from the factory active at construction.
+ class Factory(MockInterpreterFactory):
+ execution_instructions = "Use this runtime."
+
+ dspy.configure(interpreter_factory=Factory())
+
+ assert "Use this runtime." in dspy.RLM("query -> answer").generate_action.signature.instructions
+
+
+def test_rlm_refreshes_execution_instructions_for_context_factory():
+ class Factory(MockInterpreterFactory):
+ def __init__(self, instructions, responses=None):
+ super().__init__(responses=responses)
+ self.execution_instructions = instructions
+
+ class CapturingAction:
+ def __call__(self, **kwargs):
+ self.signature = kwargs["signature"]
+ return dspy.Prediction(reasoning="submit", code='SUBMIT("42")')
+
+ dspy.configure(interpreter_factory=Factory("global runtime"))
+ rlm = dspy.RLM("query -> answer")
+ action = CapturingAction()
+ rlm.generate_action = action
+
+ context_factory = Factory("context runtime", responses=[FinalOutput({"answer": "42"})])
+ with dspy.context(interpreter_factory=context_factory):
+ assert rlm(query="ignored").answer == "42"
+
+ assert "context runtime" in action.signature.instructions
+ assert "global runtime" not in action.signature.instructions
+ assert len(context_factory.instances) == 1
+
+
+def test_program_of_thought_forward_uses_the_configured_factory():
+ factory = MockInterpreterFactory(responses=[FinalOutput({"answer": "2"})])
+ dspy.configure(interpreter_factory=factory)
+
+ pot = _program_of_thought()
+ assert pot._interpreter_factory is PythonInterpreter
+ pot.code_generate = StaticPredictor(generated_code="SUBMIT({'answer': 2})")
+ pot.generate_output = StaticPredictor(answer="2")
+
+ assert pot(question="What is 1+1?").answer == "2"
+ assert len(factory.instances) == 1
+
+
+def test_code_act_forward_uses_the_configured_factory():
+ factory = MockInterpreterFactory(responses=["", "2\n"])
+ dspy.configure(interpreter_factory=factory)
+
+ program = _code_act()
+ assert program._interpreter_factory is PythonInterpreter
+ program.codeact = StaticPredictor(generated_code="print(add(1, 1))", finished=True)
+ program.extractor = StaticPredictor(answer="2")
+
+ assert program(question="What is 1+1?").answer == "2"
+ assert len(factory.instances) == 1
+
+
+def test_flex_forward_uses_the_configured_factory():
+ def execute_fn(code, variables):
+ # The shim setup and the class definition come with no variables; only the driver
+ # code carries the call's inputs, and it is the one that must return the prediction.
+ return '{"result": 4}' if variables else ""
+
+ factory = MockInterpreterFactory(execute_fn=execute_fn)
+ dspy.configure(interpreter_factory=factory)
+
+ flex = Flex(Doubler)
+ assert flex._interpreter_factory is PythonInterpreter
+ flex._bind_code(
+ "class Doubling(dspy.Module):\n"
+ " def forward(self, value):\n"
+ " return dspy.Prediction(result=value * 2)\n"
+ )
+
+ assert flex(value=2).result == 4
+ assert len(factory.instances) == 1
+
+
+def test_flex_lets_a_sub_predictor_resolve_the_configured_factory_itself():
+ # Flex hands its default factory to code-executing sub-predictors, whose resolver lets
+ # the configured factory override it at their own forward.
+ dspy.configure(interpreter_factory=MockInterpreterFactory())
+
+ flex = Flex(Doubler)
+ assert flex._bridge._sub_interpreter_factory() is PythonInterpreter
+ assert flex._bridge._build_predictor("RLM", "query -> answer", {})._interpreter_factory is PythonInterpreter