DSPy: The framework for programming—not prompting—language models
Python
fix(predict): remove corrupting code in ProgramOfThought._parse_code (#9276)
Aditya Singh0a4097c1d6dc
diff --git a/dspy/predict/program_of_thought.py b/dspy/predict/program_of_thought.pyindex 374806016..15d3e3886 100644--- a/dspy/predict/program_of_thought.py+++ b/dspy/predict/program_of_thought.py@@ -136,7 +136,7 @@ class ProgramOfThought(Module):def _parse_code(self, code_data):code = code_data.get("generated_code", "").split("---", 1)[0].split("\n\n\n", 1)[0]code_match = re.search(r"```python[ \n](.*?)[ \n]```?", code, re.DOTALL)- code_block = (code_match.group(1) if code_match else code).replace("\\n", "\n")+ code_block = code_match.group(1) if code_match else codeif not code_block:return code, "Error: Empty code after parsing."if "\n" not in code_block and code_block.count("=") > 1:@@ -145,17 +145,6 @@ class ProgramOfThought(Module):last_line_match = re.match(r"^(\w+)\s*=", lines[-1].strip())if last_line_match and len(lines) > 1:code_block += "\n" + last_line_match.group(1)- else:- code_block = re.sub(- r"([a-zA-Z_]\w* *=.*?)(?=[a-zA-Z_]\w* *=)",- r"\1\n",- code_block,- )- code_block = re.sub(- r"([a-zA-Z_]\w* *=.*?)([a-zA-Z_]\w*)$",- r"\1\n\2",- code_block,- )return code_block, Nonedef _execute_code(self, code):