Skip to content

armgit / dspy

Public Imported from GitHub
0

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

Python

fix(rlm): fall back to no sub-agents when the interpreter rejects the facade shim with SyntaxError (#10625)

Isaac Miller1dcdf8670eab
diff --git a/dspy/predict/rlm.py b/dspy/predict/rlm.py
index cc9bf813e..3ec865105 100644
--- a/dspy/predict/rlm.py
+++ b/dspy/predict/rlm.py
@@ -588,8 +588,9 @@ class RLM(Module):
)
try:
invocation.install(repl)
- except CodeExecutionError as e:
- # The runtime cannot host the shim (e.g. it runs code in the host's memory): no sub-agents this call.
+ except (CodeExecutionError, SyntaxError) as e:
+ # The runtime cannot host the shim (e.g. it runs code in the host's memory, or a restricted compiler
+ # rejects the shim's `_dspy_*` names): no sub-agents this call.
for name in (CONSTRUCT_TOOL, CALL_TOOL):
repl.tools.pop(name, None)
logger.warning("RLM sub-agents are unavailable on %s: %s", type(repl).__name__, e)
diff --git a/tests/predict/test_rlm.py b/tests/predict/test_rlm.py
index 718508d73..ec46a43e3 100644
--- a/tests/predict/test_rlm.py
+++ b/tests/predict/test_rlm.py
@@ -1989,6 +1989,26 @@ class TestRLMSubAgents:
assert "Sub-agents (dspy)" not in seen[0].instructions
assert "host's memory" in caplog.text
+ def test_interpreter_that_rejects_the_shim_as_syntax_error_runs_without_sub_agents(self, caplog):
+ # Restricted compilers (e.g. RestrictedPython) reject the shim's `_dspy_*` names at compile time.
+ from dspy.primitives import facade
+
+ def execute(code, variables):
+ if code == facade.SHIM_SETUP:
+ raise SyntaxError('"_dspy_sys" is an invalid variable name because it starts with "_"')
+ return FinalOutput({"answer": "42"})
+
+ factory = MockInterpreterFactory(execute_fn=execute)
+ rlm = RLM("query -> answer", max_iters=1, interpreter_factory=factory)
+ rlm.generate_action, seen = _recording_predictor('SUBMIT("42")')
+
+ with caplog.at_level("WARNING", logger="dspy.predict.rlm"):
+ assert rlm(query="q").answer == "42"
+
+ assert "Sub-agents (dspy)" not in seen[0].instructions
+ assert not {facade.CONSTRUCT_TOOL, facade.CALL_TOOL} & factory.instances[0].tools.keys()
+ assert "sub-agents are unavailable on MockInterpreter" in caplog.text
+
def test_facade_reserves_sandbox_names(self):
with pytest.raises(ValueError, match="conflict"):
RLM("dspy -> answer")