Skip to content

Commit 1ae4596

Browse files
ampagentIsaac Miller
andcommitted
fix(rlm): fall back to no sub-agents when the interpreter rejects the facade shim with SyntaxError
Restricted interpreters (e.g. RestrictedPython) reject the shim's _dspy_* names at compile time and raise SyntaxError, which _setup_facade did not catch, so every RLM call crashed before the first iteration. Treat it like CodeExecutionError. Amp-Thread-ID: https://ampcode.com/threads/T-01a12205-9d20-7108-b7b8-057a602a6ba2 Co-authored-by: Isaac Miller <isaac@cmpnd.ai>
1 parent 9f3fcdc commit 1ae4596

2 files changed

Lines changed: 23 additions & 2 deletions

File tree

‎dspy/predict/rlm.py‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -588,8 +588,9 @@ def _setup_facade(
588588
)
589589
try:
590590
invocation.install(repl)
591-
except CodeExecutionError as e:
592-
# The runtime cannot host the shim (e.g. it runs code in the host's memory): no sub-agents this call.
591+
except (CodeExecutionError, SyntaxError) as e:
592+
# The runtime cannot host the shim (e.g. it runs code in the host's memory, or a restricted compiler
593+
# rejects the shim's `_dspy_*` names): no sub-agents this call.
593594
for name in (CONSTRUCT_TOOL, CALL_TOOL):
594595
repl.tools.pop(name, None)
595596
logger.warning("RLM sub-agents are unavailable on %s: %s", type(repl).__name__, e)

‎tests/predict/test_rlm.py‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1989,6 +1989,26 @@ def test_interpreter_in_the_host_memory_runs_without_sub_agents(self, caplog):
19891989
assert "Sub-agents (dspy)" not in seen[0].instructions
19901990
assert "host's memory" in caplog.text
19911991

1992+
def test_interpreter_that_rejects_the_shim_as_syntax_error_runs_without_sub_agents(self, caplog):
1993+
# Restricted compilers (e.g. RestrictedPython) reject the shim's `_dspy_*` names at compile time.
1994+
from dspy.primitives import facade
1995+
1996+
def execute(code, variables):
1997+
if code == facade.SHIM_SETUP:
1998+
raise SyntaxError('"_dspy_sys" is an invalid variable name because it starts with "_"')
1999+
return FinalOutput({"answer": "42"})
2000+
2001+
factory = MockInterpreterFactory(execute_fn=execute)
2002+
rlm = RLM("query -> answer", max_iters=1, interpreter_factory=factory)
2003+
rlm.generate_action, seen = _recording_predictor('SUBMIT("42")')
2004+
2005+
with caplog.at_level("WARNING", logger="dspy.predict.rlm"):
2006+
assert rlm(query="q").answer == "42"
2007+
2008+
assert "Sub-agents (dspy)" not in seen[0].instructions
2009+
assert not {facade.CONSTRUCT_TOOL, facade.CALL_TOOL} & factory.instances[0].tools.keys()
2010+
assert "sub-agents are unavailable on MockInterpreter" in caplog.text
2011+
19922012
def test_facade_reserves_sandbox_names(self):
19932013
with pytest.raises(ValueError, match="conflict"):
19942014
RLM("dspy -> answer")

0 commit comments

Comments
 (0)