fix(dspy): count only true assignments in ProgramOfThought._parse_code single-line guard - #82
Conversation
…e single-line guard
Greptile SummaryThis PR adjusts Confidence Score: 4/5The PR should not merge until newly accepted comparison-first single-line programs capture the actual assigned result rather than the comparison’s left operand. The comparison stripping fixes the reported ordinary assignment form, but it broadens accepted input to a realistic valid statement sequence that can silently return the wrong value. Files Needing Attention: dspy/predict/program_of_thought.py Important Files Changed
Reviews (1): Last reviewed commit: "fix(dspy): count only true assignments i..." | Re-trigger Greptile |
| true_assigns = re.sub(r"==|!=|<=|>=", "", true_assigns) | ||
| if true_assigns.count("=") > 1: |
There was a problem hiding this comment.
Comparison selects wrong output
Stripping comparisons before counting assignments newly allows a comparison-first statement such as True == False; result = 2. The existing echo regex then mistakes the first = in == for an assignment and appends True, so the interpreter returns True instead of the intended result value. Restrict the accepted single-line forms or identify the actual assignment before appending its output variable.
Knowledge Base Used: Prediction programs and agent workflows
Warning
GitHub issue creation failed
Detail attempted to publish this bug to GitHub, but the issue could not be created. This fix PR was created without that issue, and missing tracker references are shown as
Unknown issue.You can review and merge this PR normally. Please review your tracker integration settings before the next publish run.
Detail bug report: View on Detail
📝 Changes Description
ProgramOfThought._parse_coderejected valid single-line Python that contains a comparison operator (==,!=,<=,>=). Its single-line guard usedcode_block.count("=") > 1, which counts every=character including those inside comparison operators, so a perfectly executable one-liner likeresult = 5 == 3was rejected with''"Error: Code format is not correct."''before the interpreter was ever reached. If the LM regenerated the same shape across retries,forwardwasted hops and could raiseRuntimeError: Max hops reached.... The bug was reachable on the deprecated single-line "old-style" path (noSUBMIT()), whereresult = 5 > 3ran but the semantically identicalresult = 5 == 3did not — an inconsistency left in place by the recent single-line echo fix (fe75476c).//=,**=,<<=,>>=,+=,-=,*=,/=,%=,&=,|=,^=,@=) to a single=, strip the comparison operators (==,!=,<=,>=), then count=. Comparisons now contribute zero and augmented assignments contribute one, so a single assignment containing comparisons is accepted, while genuine multi-assignment single-liners are still rejected.a = b = 5,a = 1; b = 2,x += 1; y -= 2,x <<= 1; y >>= 2, andinvalid=python=codestill raise the format error (the existingtest_pot_code_parse_errorstill passes, now for the right reason).MockInterpreter) confirming the comparison code reaches the interpreter on the first hop; and a@pytest.mark.denoend-to-end test mirroringtest_old_style_potconfirming the parsed code evaluates toFalseunder the real Deno/Pyodide interpreter.Closes Unknown issue
✅ Contributor Checklist
ruff check --fix-only --exit-non-zero-on-fixandruff format --checkboth clean on the changed files.fix(dspy): ...matches(label)(dspy): message.{label}(dspy): {message}—fix(dspy): count only true assignments in ProgramOfThought._parse_code single-line guard._parse_code, the fix was reviewed and understood line-by-line, and all verification was run locally. The human author reviewed and is submitting this PR.ProgramOfThoughtis deprecated (emitsDeprecationWarning, will be removed in 3.5 in favor of RLM), so this fix targets a legacy path; it keeps that path consistent with the single-line support rehabilitated infe75476c.tests/predict/test_program_of_thought.py(29 passed, 7 deno deselected); the broadertests/predict/suite (264 passed, 6 skipped, 0 failed — no regressions); ruff lint and format clean. The new@pytest.mark.denoend-to-end test was executed against a real Deno/Pyodide interpreter (deno 2.9.5) and passed (7 passed under-m deno --deno), confirming the parsed comparison code evaluates toFalseand flows throughgenerate_output.Automatic Fixes PRs can be configured here.