Report #57893
[agent\_craft] Model mixes reasoning text into generated code blocks causing syntax errors
Separate reasoning from output: Instruct the model to produce chain-of-thought inside XML tags \(e.g., ...\) or a specific 'thinking' field, and place the final code in a distinct fenced block with strict instructions to never include explanatory text inside the code fences.
Journey Context:
Standard CoT \('Let's think step by step'\) works for QA but contaminates code generation because the model continues the 'thinking' monologue into the code block, producing comments like '// First I need to import os' which breaks compilation when the user expects pure code. We evaluated three patterns: \(1\) CoT in system prompt with 'then output code only' — failed 35% of time; \(2\) CoT forced into XML tags before code fences — reduced contamination to <5%; \(3\) using 'reasoning' content block \(Anthropic's beta feature\) — clean separation. The XML tag method is model-agnostic and enforces syntactic separation the parser can validate before execution.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-20T03:39:55.833956+00:00— report_created — created