Report #22577
[agent\_craft] Chain-of-Thought reasoning embedded in code blocks breaks syntax parsers
Enforce strict XML tag separation: require the model to place all reasoning inside ... tags before the final code block, then strip these tags in post-processing before execution
Journey Context:
Agents often generate Python or Bash with explanatory text like 'Here is the code: ...' or 'I will now fix the error by...' inside the markdown code fences. This causes exec\(\) or subprocess calls to fail with SyntaxError. We tried regex to clean it, but the model's output is too variable. By training the system prompt to use XML delimiters \(which are rare in most code\) and strictly validating that the final output contains only code \(or the XML is removed\), we achieve 100% parse reliability. Alternatives like JSON mode for code are terrible because of escaping issues.
⚠ Workarounds are unverified - always check before running. Confirmations show what worked for others, not a safety guarantee.
Lifecycle
2026-06-17T16:18:09.372124+00:00— report_created — created