Skip to content

PDF extraction assigns wrong heading levels (merged, inconsistent, or invented headings) #2142

Open

Description

@jope-bm

Problem

PDF text extraction gives headings the wrong level, or none:

  • A bold section heading can be merged into the previous paragraph as inline bold.
  • Two headings of the same size can come out at different levels.
  • Plain body text can become a heading.

The extracted-text note then has a misleading outline (H1/H2) and loses real section breaks.

Extraction runs through pdf_inspector.extract_pages_markdown_bytes in src/basic_memory/document_ingestion/pdf_inspector_worker.py, with pdf-inspector 0.2.6.

Repro

A two-page PDF with standard Type 1 fonts:

Page Text Font
1 "Quarterly Field Notes" Helvetica-Bold 20pt
1 "Observations from the Q3 site visits to the coastal monitoring stations." Helvetica 12pt
1 "Station summary" Helvetica-Bold 14pt
1 Three lines starting with "- " Helvetica 12pt
2 "Follow-ups" Helvetica-Bold 14pt
2 Two body lines Helvetica 12pt
2 "Contact: field team lead." Helvetica 12pt

pdf_inspector.extract_pages_markdown_bytes(data) returns:

# Quarterly Field Notes

Observations from the Q3 site visits to the coastal monitoring stations. **Station summary**

- North Point: sensors calibrated, battery at 82%.
- Harbor Mouth: salinity drift of 0.4 ppt since last visit.
- Kelp Bed C: camera housing replaced.
# Follow-ups

Order two spare housings before the winter storms.

Re-run the salinity calibration at Harbor Mouth in October.

## Contact: field team lead.

Expected

  • "Station summary" and "Follow-ups" are both 14pt bold, so they get the same heading level, below the 20pt title (for example H2).
  • "Station summary" starts its own block instead of joining the paragraph before it.
  • "Contact: field team lead." stays body text. It is the same 12pt regular font as the other body lines.

Possibly relevant

  • Levels may be per page. The 14pt heading becomes H1 on page 2, where it is the largest text. That suggests levels are assigned page by page rather than across the whole document.
  • The last short line on a page may be read as a heading. These are guesses from one PDF, not confirmed against pdf-inspector's code.

Options

  • Fix it upstream in pdf-inspector. This may need a report there.
  • Normalize after extraction in pdf_inspector_worker.py, using document-wide font sizes, if pdf-inspector exposes them.

Found while testing PDF uploads.

🤖 Generated with Claude Code

Activity

  1. added
    bug
    on Nov 20, 2026
  2. claude commented on Nov 20, 2026

    @claude
    Contributor

    Claude finished @jope-bm's task in 13s —— View job


    Triage

    • Type: Bug (label bug kept; component none)
    • Component: Document ingestion (src/basic_memory/document_ingestion/pdf_inspector_worker.py), which calls pdf-inspector 0.2.6
    • Priority: Medium. Extraction still returns the text. The outline is wrong, and section breaks can be lost.
    • Complexity: Medium. If the root cause is in pdf-inspector, the real fix is upstream. A local fix needs font-size data that may not be exposed.
    • Duplicates: I searched open and closed issues for "pdf heading" and "pdf_inspector" and found none.

    Notes

    The repro is clear and specific: the font table, the exact output, and the expected output. It covers three separate symptoms:

    1. A bold heading merges into the preceding paragraph as inline bold.
    2. Equal-size headings get different levels. This looks like per-page level assignment.
    3. A short last line on a page is promoted to a heading.

    Suggested next steps for whoever picks this up:

    • Check whether pdf-inspector exposes per-span font size and weight. If it does, normalize levels in the worker using document-wide sizes. If it doesn't, file an upstream report with this repro PDF.
    • Save the repro PDF as a test fixture, so a regression test can assert the three expected outcomes.

    I did not make any code changes. This was triage only.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions