Skip to content

MarkdownHeaderSplitter treats headings inside longer closing fences as headers #12954

Closed

Description

@Harsh23Kashyap

Problem

MarkdownHeaderSplitter excludes headers inside fenced code blocks only if the closing fence has exactly the same length as the opening fence. Markdown permits a closing fence of the same type with at least as many fence characters. With a longer valid closing fence, the splitter treats code comments as document headers and cuts the code block apart.

Reproduction

On current main:

from haystack import Document
from haystack.components.preprocessors import MarkdownHeaderSplitter

text = "# Intro\nBody\n````python\n# fake\n`````\n## Real\nEnd\n"
result = MarkdownHeaderSplitter().run(documents=[Document(content=text)])
print([doc.meta["header"] for doc in result["documents"]])

Actual: ['Intro', 'fake', 'Real']. Expected: ['Intro', 'Real']. I ran it twice with identical results. Replacing the five-backtick closer with a four-backtick closer gives the expected result. The same failure occurs with four opening tildes and five closing tildes.

The current _code_block_pattern uses a backreference to the opening fence, requiring equal lengths. The CommonMark rule is a closing fence of the same type with at least the opening length: https://spec.commonmark.org/0.31.2/#fenced-code-blocks .

Suggested scope

Recognize longer closing fences when excluding code blocks from header matches, while keeping unrelated heading and secondary-split behavior unchanged. Regression tests should cover longer backtick and tilde closers, equal-length closers, and the real header after the block. This should not require a public API change. I did not find a matching open/closed issue or PR and can prepare a patch once the current Haystack PR queue clears.

Activity

  1. Harsh23Kashyap commented on Sep 25, 2026

    @Harsh23Kashyap
    ContributorAuthor

    The same fence matcher also misses an unclosed fence, which Markdown treats as continuing to the end of the document. With # Intro\nBody\n```python\n# code comment\nprint("x")\n, the splitter returns headers ['Intro', 'code comment'] instead of just ['Intro']. Reproduced twice on current main. This is another regression case for the fence-exclusion fix, not a separate issue.

  2. Harsh23Kashyap commented on Sep 25, 2026

    @Harsh23Kashyap
    ContributorAuthor

    One more fence variant for the same fix: CommonMark allows up to three spaces before an opening or closing fence. With # Intro\nBody\n ```python\n# fake\n ```\n## Real\nEnd\n, the splitter returns ['Intro', 'fake', 'Real']; expected ['Intro', 'Real']. The same result repeats on current main. Please cover indentation along with unequal-length and unclosed fences in the regression tests.

  3. AmaedaQ commented on Oct 4, 2026

    @AmaedaQ

    I'd like to pick this up. Thanks @Harsh23Kashyap for the clear report and the CommonMark reference. I reproduced the issue on main at 87dba65.

    The scope is three cases, all with the same root cause in _code_block_pattern. @Harsh23Kashyap had already flagged the unclosed fence and the indentation case earlier in this thread, and I missed those comments when I first posted.

    1. Longer closing fence: a closing fence may be longer than the opener, but it must use the same fence character. A shorter run never closes the block.
    2. Indented fence: a fence indented by up to three spaces is still a fence. Four or more makes it an indented code block, so a # line after it remains a real header.
    3. Unclosed fence: per CommonMark, an unclosed fence runs to the end of the document. The current pattern requires a closing fence to match, so when none exists, every # line inside the block is parsed as a header. This is likely to show up with truncated or streamed markdown.

    Repro for the unclosed case:

    from haystack import Document
    from haystack.components.preprocessors import MarkdownHeaderSplitter
    
    text = "# Intro\nBody\n```python\n# fake\nx = 1\n"
    result = MarkdownHeaderSplitter().run(documents=[Document(content=text)])
    print([doc.meta.get("header") for doc in result["documents"]])
    # Actual:   ['Intro', 'fake']
    # Expected: ['Intro']

    Proposed fix: capture the fence character separately from its length, so the closing fence must repeat the opener's character at least as many times, and allow up to three spaces of indentation on either fence. An unterminated block extends to the end of the document instead of failing the match.

    Tests cover each of the three cases above, plus the existing equal-length behavior as a regression guard.

    There is no public API change. All three belong in one fix, and I'll open a PR shortly.

    Edited to correct scope.

  4. added
    P3
    on Oct 16, 2026
  5. Vedant-Agarwal commented on Oct 18, 2026

    @Vedant-Agarwal
    Contributor

    Hi @AmaedaQ, are you still working on this? If not, I have a fix ready that covers the longer, indented and unclosed fences from this thread, plus a guard so a line that starts with inline code doesn't open a block that would hide every later header. Happy to leave it with you if you're close.

    (AI-assisted: drafted with Claude Code.)

  6. Vedant-Agarwal commented on Oct 20, 2026

    @Vedant-Agarwal
    Contributor

    Opened #13344 for this. Thanks @AmaedaQ for the write-up, it covers the same three cases, and the PR also handles a line that starts with inline code. Happy to close mine if you already have a PR coming.

    (AI-assisted)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

P3

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions