Skip to content

fix(parser): read PDF bookmarks into remote parse results - #21499

Merged
yuzhichang merged 1 commit into
infiniflow:mainfrom
ccclucky:fix/21221-pdf-outline
Oct 28, 2026
Merged

yuzhichang merged 1 commit into
infiniflow:mainfrom
ccclucky:fix/21221-pdf-outline

Conversation

@ccclucky

Copy link
Copy Markdown
Contributor

Summary

  • What breaks today: every PDF parse method except deepdoc returns a result whose file.outline is empty. The title chunker reads that key to derive heading levels (internal/ingestion/component/chunker/title.go outlineFromUpstream), so on plain_text / MinerU / Docling / OpenDataLoader / SoMark / TCADP and the tenant PaddleOCR / MonkeyOCRv2 / VLM paths it always falls back to flat levels.
  • The fix: read the bookmarks from the source PDF bytes — already in hand at both result boundaries — and fill file.outline. parser.AttachPDFOutlines is called at the two choke points every dispatched PDF result passes through: PDFParser.ParseWithResult and the normalize closure in maybeDispatchPDFVision. A result that already carries an outline is left untouched, so the engine that read the bookmarks (DeepDoc) stays authoritative.
  • Type cleanup: pdfium.ExtractOutlines now returns the shared doctype.Outline instead of its own identical struct, which deletes the hand-rolled field-by-field copy in the pdf_oxide bridge rather than giving it a second caller.
  • Scope: this is the outline-data slice of [Bug]: remove_toc and remove_header_footer have no effect for remote PDF parsers #21221 only. It does not implement remove_toc filtering or remove_header_footer on these paths; the issue's second and third bullets remain open. fix(web): hide unsupported PDF cleanup controls #21440 proposes hiding the unsupported switches in the UI, which is an alternative for those, not for this.

Test plan

  • bash build.sh --test ./internal/parser/parser/ -run 'TestAttachPDFOutlines|TestParseWithResultAttachesOutline' — 4 new unit tests, no-cgo path covered by a !cgo stub
  • gofmt -l, go vet clean, also under CGO_ENABLED=1 for ./internal/parser/parser/ ./internal/ingestion/component/ ./internal/deepdoc/parser/...
  • New native tests: TestPDFOutlineNativeExtractionFeedsRemoteResult (parser) and TestPDFDispatchAttachesPDFOutlines (component) pin both call sites with a hand-written bookmarked PDF. CI-only on this host: the local cgo link fails on ORT/pdfium duplicate symbols. To keep that from being an unverified claim, both fixtures were run against real pdfium through a scratch test in the pdfium package (which links standalone) and returned exactly {Chapter 1, 0, 1} {Section 1.1, 1, 2} {Chapter 2, 0, 2}; the same fixtures pass pdfinfo/pdftotext on 2 pages in reading order.
  • Consumer contract re-checked: File["outline"] survives the component-to-component JSON round trip as json.RawMessage and outlineFromUpstream parses title/level/page_number with anyToInt, which accepts the float64 a JSON decode produces.
  • No real-corpus ingest run was performed (no live MinerU/PaddleOCR service in this environment).

Known cost

A PDF that ends up with no outline is loaded by pdfium twice on the tenant-parser path (ParseWithResult inside the model adapter, then the dispatch closure) because an empty result is indistinguishable from "not attempted". For PDFs that do have bookmarks the second call short-circuits on the guard.

Refs #21221

Every PDF method except deepdoc left file.outline empty, so the title
chunker had no bookmarks to derive heading levels from. Attach them from
the source bytes at the two boundaries that hand out a PDF parse result —
the built-in parse methods and ingestion dispatch — and keep any outline
the engine already produced authoritative.

pdfium now returns the shared doctype.Outline, so the bridge's hand-rolled
field copy is gone instead of gaining a second caller.

Part of infiniflow#21221: outline data only. remove_toc filtering and
remove_header_footer on these paths are not addressed here.
@ccclucky

Copy link
Copy Markdown
Contributor Author

@coderabbitai review

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor

Review in Change Stack →

No actionable comments were generated in the recent review.

Recent review info
Run configuration
  • Source mode: Auto
  • Repository: Unavailable
  • Branch: Unavailable
  • File: No configuration file from this repository applied
  • Commit: Unavailable
  • Layers: Organization UI
  • Review profile: CHILL
  • Plan: Advanced
  • Run ID: dd4fc00d-b95c-4a55-9f3a-8d240434ff72
Commits

Reviewing files that changed from the base of the PR and between 82bd3b2 and 7aeb3bf.

Files selected for processing: 10
  • internal/deepdoc/parser/pdf/pdfium/pdfium.go
  • internal/deepdoc/parser/pdf/pdfoxide_bridge.go
  • internal/ingestion/component/pdf_outline_cgo_test.go
  • internal/ingestion/component/pdf_vision_dispatch.go
  • internal/parser/parser/pdf_outline.go
  • internal/parser/parser/pdf_outline_cgo.go
  • internal/parser/parser/pdf_outline_cgo_test.go
  • internal/parser/parser/pdf_outline_nocgo.go
  • internal/parser/parser/pdf_outline_test.go
  • internal/parser/parser/pdf_parser_common.go

Included review availability: This review used your included allowance. Your plan provides up to 10 included reviews per hour; 9 remain after this review.

Walkthrough

PDFium now returns shared outline values. Successful parser and PDF dispatch results attach extracted outlines when no nonempty outline is already present. The change includes tests for native extraction, parser result handling, and PDF dispatch.

Changes: PDF Outline Attachment

Layer / File(s) Summary
Shared outline type and PDFium extraction
internal/deepdoc/parser/pdf/pdfium/pdfium.go, internal/deepdoc/parser/pdf/pdfoxide_bridge.go, internal/parser/parser/pdf_outline_cgo.go, internal/parser/parser/pdf_outline_nocgo.go, internal/parser/parser/pdf_outline_cgo_test.go, internal/parser/parser/pdf_outline_test.go
PDFium now returns doctype.Outline values, and the PDFOxide bridge returns those values directly. The parser provides cgo and non-cgo extraction helpers. Tests check native extraction of nested bookmarks.
Attach outlines to parser results
internal/parser/parser/pdf_outline.go, internal/parser/parser/pdf_parser_common.go, internal/parser/parser/pdf_outline_test.go
AttachPDFOutlines adds extracted outlines to eligible results and preserves existing nonempty outlines. ParseWithResult calls it after successful parsing. Tests cover existing outlines, missing bookmarks, and error or file-less results.
Attach outlines during PDF dispatch
internal/ingestion/component/pdf_vision_dispatch.go, internal/ingestion/component/pdf_outline_cgo_test.go
PDF vision dispatch attaches outlines from the source PDF after normalization. A CGO-gated test checks that the dispatched result includes its bookmark.

Priority: Normal

Estimated code review effort: 3 (Moderate) | ~20 minutes

Change: Bug fix

Sequence Diagram(s)

sequenceDiagram
  participant Dispatch as maybeDispatchPDFVision
  participant Attach as AttachPDFOutlines
  participant Extract as pdfOutlinesFromData
  participant PDFium as pdfium.ExtractOutlines
  Dispatch->>Attach: normalized result and source PDF bytes
  Attach->>Extract: source PDF bytes
  Extract->>PDFium: extract bookmarks
  PDFium-->>Attach: outline values
  Attach-->>Dispatch: result with outline metadata
Loading

Possibly related issues

  • Issue 21218: The change attaches outlines to dispatched PDF results; the figure, output format, and media-flattening gaps remain.
  • Issue 21221: The change populates outlines on dispatched results; remove_toc and remove_header_footer wiring remains unimplemented.
  • Issue 21215: The change attaches outlines to remote parse results; page positions and page_count are not addressed.
  • Issue 21214: The change attaches outlines to dispatched results; layout positions and image-inlining behavior are not addressed.
  • Issue 21216: The change attaches outlines to remote parse results; missing page positions are not addressed.
  • Issue 21217: The change attaches outlines to remote parse results; missing page positions are not addressed.
  • Issue 21213: The change attaches outlines to dispatched results; image-inlining behavior is not addressed.

Suggested reviewers: jay77721

Merge Risk: Minimal · up to 7aeb3

No merge-blocking issue is established for the PDF outline attachment change.

Pre-merge checks: Passed 5
Check name Status Explanation
Title check Passed The title clearly and concisely describes the main change: reading PDF bookmarks into remote parse results.
Description check Passed The description includes the required summary, background, implementation scope, test plan, known cost, and issue reference. It clearly explains the change and its limitations.
Docstring Coverage Passed Docstring coverage is 86.67% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff. Analyzed 15 functions across 10 files.
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Some tools did not complete. Review the errors below.

🔧 golangci-lint (2.14.0)

level=error msg="[linters_context] typechecking error: build constraints exclude all Go files in /internal/deepdoc/parser/pdf/pdfium"

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

A rabbit finds a bookmark bright,
And hops through chapters left and right.
The pages share their titles clear,
The outline joins the results here.
One leafy trail from start to end,
With nested paths around each bend.

Comment @coderabbitai help to get the list of available commands.

@coderabbitai

coderabbitai Bot commented Oct 26, 2026 •

Copy link
Copy Markdown
Contributor
⚠️ Action not completed

Already reviewed the last commit. Use @coderabbitai full review to rerun a review of the entire changeset.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@ccclucky

Copy link
Copy Markdown
Contributor Author

Ping for CI: no workflow run has ever been created for this PR (head 7aeb3bf45634a4bc554515a1a656b002596df3eb), so the checks panel is empty rather than green. Workflows on fork PRs stay unqueued until a maintainer approves them, so this is the only thing blocking a first real signal on the change.

Could someone approve and run the workflows for this head? The change is Go-only; the unit tier passes locally, and the real-service tiers are exactly what needs CI here. I will rebase and re-request if the head moves.

@xugangqiang xugangqiang left a comment •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@ccclucky

Copy link
Copy Markdown
Contributor Author

Thanks @xugangqiang. Head is still 7aeb3bf45634a4bc554515a1a656b002596df3eb and no workflow run has been created for it yet, so the six ragflow_* checks sit in the awaiting-approval state. Once someone approves them I will rebase and re-request if the head moves.

@ccclucky

Copy link
Copy Markdown
Contributor Author

The two failing jobs are infrastructure, not this change:

  • ragflow_tests_infinity failed in Build ragflow go server at error: RPC failed; curl 92 HTTP/2 stream 5 was not closed cleanly: CANCEL (err 8) / 10675 bytes of body are still expected — a dependency fetch through the proxy was cut off.
  • ragflow_go_integration failed at fetch-pack: unexpected disconnect while reading sideband packet while fetching the test corpus.

Both are network drops on the self-hosted runner. Everything that did run passed (ragflow_preflight, ragflow_python_sdk_unit, ragflow_tests_elasticsearch), and the Go path was verified locally against the changed package. A re-run of the two red jobs would be enough; I do not have permission to trigger it from a fork.

@xugangqiang xugangqiang added ci Continue Integration and removed ci Continue Integration labels Oct 28, 2026

@yuzhichang yuzhichang left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review — head 7aeb3bf

Reviewed merge-base e5a5060..7aeb3bf (10 files, +314/−19) and both result-producing boundaries it patches.

No blocking findings.

  • Both boundaries are covered: PDFParser.ParseWithResult (pdf_parser_common.go:364) and the ingestion normalize closure (pdf_vision_dispatch.go:90-95). Tenant somark/opendataloader reach the former through pdf.ParseWithResult (pdf_parser_model.go:160), and mineru/monkeyocrv2/paddleocr plus the generic VLM path return into the closure, which already seeds an empty outline and File, so the patch applies there too.
  • The plumbed shape matches what the chunker reads: pdfFileMeta seeds outline as []map[string]any{}, AttachPDFOutlines writes the same type, and the chunker reads it back through ChunkerFileMeta.Extra["outline"] (chunker/title.go, chunker/general.go).
  • The type move is safe: pdftype.Outline is already an alias of doctype.Outline, so dropping the duplicate pdfium.Outline struct only deletes a per-item conversion; no other reference exists and no import cycle is introduced.
  • Idempotence holds: an engine-produced non-empty outline short-circuits the reader, and re-entry from the dispatch closure after the tenant parser's own ParseWithResult is a no-op. The cgo/!cgo split matches pdfium's build tag and the new test helpers do not collide.

Below the bar: on the built-in deepdoc path a bookmark-free PDF is loaded into pdfium a second time, because the "outline already present" guard cannot distinguish "read and found none" from "never read"; and the !cgo stub silently reports no bookmarks for the pure-Go fallback. Cost and an undocumented fallback, not correctness.

@yuzhichang
yuzhichang merged commit 0c8af78 into infiniflow:main Oct 28, 2026
11 of 13 checks passed
@ccclucky

Copy link
Copy Markdown
Contributor Author

Status on head 7aeb3bf45634a4bc554515a1a656b002596df3eb: the re-run 40646911350 finished green across the board.

check result
ragflow_preflight success
ragflow_python_sdk_unit success
ragflow_tests_infinity success
ragflow_tests_elasticsearch success
ragflow_go_integration success
ragflow_native_backend success

The two failure entries still visible in the checks panel belong to the earlier run 40619765866, where both jobs died fetching the module cache (curl 92 / curl 56); the same jobs pass on the current run, so nothing in this change was involved. The PR is ready for review.

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

Labels

ci Continue Integration

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants