Resolved

Markdown code fences appear in PDF output from LLM responses

Python
AI/ML
Markdown
PDF
LLM
Text Processing
Debugging
Post-Processing

LLM-generated markdown content included unwanted ```markdown code fences causing visible artifacts in PDF rendering | Fixed via prompt engineering and post-processing utility

Issue

LLM-generated markdown content contained unwanted triple-backtick code fences (markdown), causing literal code fence syntax to appear visibly in the PDF output. This resulted in rendering artifacts where raw markdown syntax (e.g., markdown) was displayed at the top of PDF documents instead of being processed as formatting instructions.

Culprit

The LLM automatically wrapped markdown responses in code fences as a default behavior, producing outputs like:

# CV Content
...

Instead of the required clean markdown:

# CV Content
...

This occurred because the system prompts lacked explicit instructions about the required output formatting for PDF conversion, leading the LLM to apply generic markdown documentation practices.

Solution

Implemented a two-layer defense system to address the issue:

1. Prompt Engineering

Added explicit OUTPUT FORMAT instructions to system prompts in evaluator-anni4-fastmcp.py (lines 278-281 and 427-430) to instruct the LLM:

  • Do not wrap responses in code fences (e.g., no markdown or ).
  • Ensure clean markdown output suitable for direct PDF rendering.

2. Post-Processing Utility

Created a strip_code_fences() function to serve as a safety net:

  • Uses regex to detect and remove leading/trailing ```markdown blocks.
  • Preserves all internal markdown formatting (e.g., headings, lists, bold/italics).
  • Handles variations of single and multi-line code fence artifacts.

Applied the utility to both generate_cover_letter() and generate_cv() workflows before saving LLM responses to files.

Results

  • Clean PDF Output: No visible code fence artifacts in generated PDF documents.
  • Preserved Formatting: All markdown formatting (headings, lists, etc.) remained intact.
  • Robust Pipeline: Combination of prompt engineering and post-processing ensured reliable results.