DSPy: The framework for programming—not prompting—language models
Python
fix(predict): ProgramOfThought._parse_code drops output-capturing echo for single-line generated code (#10050)
Andrew Chenfe75476cd196
diff --git a/dspy/predict/program_of_thought.py b/dspy/predict/program_of_thought.pyindex 48fbad09d..fbc157846 100644--- a/dspy/predict/program_of_thought.py+++ b/dspy/predict/program_of_thought.py@@ -172,7 +172,7 @@ class ProgramOfThought(Module):return code, "Error: Code format is not correct."lines = code_block.split("\n")last_line_match = re.match(r"^(\w+)\s*=", lines[-1].strip())- if last_line_match and len(lines) > 1:+ if last_line_match:code_block += "\n" + last_line_match.group(1)return code_block, Nonediff --git a/tests/predict/test_program_of_thought.py b/tests/predict/test_program_of_thought.pyindex a08a808d5..5828bd384 100644--- a/tests/predict/test_program_of_thought.py+++ b/tests/predict/test_program_of_thought.py@@ -271,3 +271,27 @@ def test_pot_code_parse_error():):pot(question="What is 1+1?")mock_execute_code.assert_not_called()+++def test_pot_parse_code_appends_echo_for_single_line_assignment():+ """_parse_code appends a trailing bare-name echo of the last assigned variable so the+ interpreter's last-expression value is captured as output. This must also apply when+ the generated code is a single line, not just when it's the last of several lines --+ a bare `answer = 42` is a common, simple case an LM can legitimately generate."""+ pot = ProgramOfThought(BasicQA)++ code_block, error = pot._parse_code({"generated_code": "answer = 42"})++ assert error is None+ assert code_block == "answer = 42\nanswer"+++def test_pot_parse_code_multiline_assignment_still_appends_echo():+ """Regression guard: the multi-line case (already covered before this fix) must keep+ working the same way."""+ pot = ProgramOfThought(BasicQA)++ code_block, error = pot._parse_code({"generated_code": "x = 1\nanswer = x + 41"})++ assert error is None+ assert code_block == "x = 1\nanswer = x + 41\nanswer"