Skip to content

RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.

fix: enhance PDF figure captions with vision (#19847)

buua4366152af2a4117
diff --git a/internal/parser/parser/pdf_parser_common.go b/internal/parser/parser/pdf_parser_common.go
index 8b18660caf..979e070b99 100644
--- a/internal/parser/parser/pdf_parser_common.go
+++ b/internal/parser/parser/pdf_parser_common.go
@@ -499,6 +499,7 @@ func cropMediaSections(result *deepdoctype.ParseResult) {
continue
}
if sec.LayoutType != deepdoctype.LayoutTypeFigure &&
+ sec.LayoutType != deepdoctype.DLALabelFigureCaption &&
sec.LayoutType != deepdoctype.LayoutTypeTable &&
strings.TrimSpace(sec.LayoutType) != "image" &&
sec.DocTypeKwd != "image" && sec.DocTypeKwd != "table" {
@@ -656,10 +657,18 @@ func normalizePDFDocType(item map[string]any) {
if item == nil {
return
}
+ layoutType, _ := item["layout_type"].(string)
if docType, _ := item["doc_type_kwd"].(string); docType != "" {
+ // A figure caption can carry the cropped figure image after PDF media
+ // sections are rendered. Keep it aligned with Python's media-section
+ // contract so the downstream VLM enhancement can process it.
+ if docType == "text" && layoutType == deepdoctype.DLALabelFigureCaption {
+ if img, _ := item["image"].(string); img != "" {
+ item["doc_type_kwd"] = "image"
+ }
+ }
return
}
- layoutType, _ := item["layout_type"].(string)
switch layoutType {
case "table":
item["doc_type_kwd"] = "table"
diff --git a/internal/parser/parser/pdf_parser_common_test.go b/internal/parser/parser/pdf_parser_common_test.go
index 280772a7c8..9ceed111f0 100644
--- a/internal/parser/parser/pdf_parser_common_test.go
+++ b/internal/parser/parser/pdf_parser_common_test.go
@@ -101,6 +101,26 @@ func TestPDFParseResultToJSON_NormalizesCoreFields(t *testing.T) {
}
}
+func TestPDFParseResultToJSON_ClassifiesFigureCaptionWithImage(t *testing.T) {
+ parsed := &deepdoctype.ParseResult{
+ Sections: []deepdoctype.Section{
+ {
+ Text: "小灰灰",
+ LayoutType: deepdoctype.DLALabelFigureCaption,
+ Image: "aGVsbG8=",
+ },
+ },
+ }
+
+ res := pdfParseResultToJSON("sample.pdf", parsed)
+ if res.Err != nil {
+ t.Fatalf("pdfParseResultToJSON: %v", res.Err)
+ }
+ if got, want := res.JSON[0]["doc_type_kwd"], "image"; got != want {
+ t.Fatalf("doc_type_kwd = %v, want %v", got, want)
+ }
+}
+
// TestNormalizePDFPageNumber_UnconditionalIncrement pins the contract that
// DeepDoc emits 0-indexed page numbers and normalizePDFPageNumber is the
// SINGLE conversion point to 1-indexed. It must add +1 unconditionally —
@@ -514,6 +534,36 @@ func TestPDFParseResultToJSON_CropsMediaSectionsWithEngine(t *testing.T) {
}
}
+func TestPDFParseResultToJSON_CropsFigureCaptionForVisionEnhancement(t *testing.T) {
+ mockEngine := &mockPDFEngineForCommonTest{}
+ parsed := &deepdoctype.ParseResult{
+ Engine: mockEngine,
+ PageHeight: map[int]float64{0: 100},
+ Sections: []deepdoctype.Section{{
+ Text: "Figure caption",
+ LayoutType: deepdoctype.DLALabelFigureCaption,
+ Positions: []deepdoctype.Position{{
+ PageNumbers: []int{0},
+ Left: 10,
+ Right: 50,
+ Top: 10,
+ Bottom: 50,
+ }},
+ }},
+ }
+
+ res := pdfParseResultToJSON("figure-caption.pdf", parsed)
+ if res.Err != nil {
+ t.Fatalf("pdfParseResultToJSON: %v", res.Err)
+ }
+ if got, want := res.JSON[0]["doc_type_kwd"], "image"; got != want {
+ t.Fatalf("doc_type_kwd = %v, want %v", got, want)
+ }
+ if image, _ := res.JSON[0]["image"].(string); image == "" {
+ t.Fatal("figure caption image should be cropped before vision enhancement")
+ }
+}
+
func TestPDFParseResultToJSON_EngineNilGraceful(t *testing.T) {
parsed := &deepdoctype.ParseResult{
Engine: nil,