langextract icon indicating copy to clipboard operation
langextract copied to clipboard

Bug: fenced JSON parsing rejects triple backticks inside extraction strings

Open BichengWang opened this issue 1 month ago • 1 comments

Describe the overall issue and situation

Extracting Markdown code blocks can fail even when the model returns valid fenced JSON. FormatHandler treats triple backticks inside a JSON string as output delimiters. Its own format_extraction_example() output therefore cannot be parsed back with the default settings when an extraction contains a Markdown code block.

Expected behavior

The single outer JSON block should parse, preserving backticks inside JSON string values.

Actual behavior

The reproduction raises FormatParseError: Multiple fenced blocks found. Expected exactly one. There is only one outer output block; the other backticks belong to the extraction value.

Steps to reproduce the issue

Run this offline:

from langextract.core import data, format_handler

value = "```python\nprint(1)\n```"
extractions = [data.Extraction(
    extraction_class="code", extraction_text=value
)]
handler = format_handler.FormatHandler()
output = handler.format_extraction_example(extractions)
print(output)
print(handler.parse_output(output))

As a control, using FormatHandler(use_fences=False) for both formatting and parsing preserves the same value successfully.

Any additional content

Reproduced on main 3a68580f0aede0e96a2e1e03daa2a5f4011e4fcd, Python 3.11.17, macOS, with no model/API calls. The fence regex matches delimiters inside the JSON string before JSON parsing runs. This concerns fenced output containing literal backticks; #414 / #444 concern receiving an outer fenced block when raw JSON mode was requested.

BichengWang avatar Oct 23 '26 20:10 BichengWang

opened #604 with a fix anchoring code fence delimiters.

mohmedmm avatar Nov 02 '26 21:11 mohmedmm