RAGFlow is an agent-native database: a Retrieval-Augmented Generation (RAG) engine that grew into context infrastructure.
Go agent-harnessagent-native-databaseagentic-aiagentic-retrievalagentic-searchaiai-agentscontext-enginecontext-engineeringcontext-managementharness-engineeringknowledge-compilationragretrieval-augmented-generationretrieval-harnesssearch-harness
fix(parser): keep PaddleOCR figure regions as media items (#21452)
CCLUCKY84e09c0c0085
diff --git a/internal/entity/models/paddleocr_response.go b/internal/entity/models/paddleocr_response.goindex fe681cdd2d..26289819b9 100644--- a/internal/entity/models/paddleocr_response.go+++ b/internal/entity/models/paddleocr_response.go@@ -123,12 +123,15 @@ func parsePaddleOCRResultBody(body []byte) (*OCRFileResponse, error) {text.WriteString("\n\n")}content := strings.TrimSpace(text.String())- if content == "" {+ if content == "" && !hasOCRMediaBlocks(response.Pages) {if len(response.Warnings) > 0 {return nil, fmt.Errorf("paddleocr result: no usable text; %s", response.Warnings[0])}return nil, fmt.Errorf("paddleocr result: parsed empty text")}+ // A response that only carries media regions has no text; Text stays+ // non-nil and empty so callers can dereference it, while Pages still feeds+ // the PDF media path.response.Text = &contentreturn response, nil}@@ -153,14 +156,27 @@ func paddleOCRPages(result paddleOCRResult, nextPage int) []OCRPage {}page.Blocks = pruned.blocks()hasText := false+ hasMedia := falsefor _, block := range page.Blocks {if strings.TrimSpace(utility.StripMarkdownImages(block.Text)) != "" {hasText = true- break+ }+ if IsOCRMediaLabel(block.Label) || IsOCRImageOnlyText(block.Text) {+ hasMedia = true}}if !hasText && strings.TrimSpace(markdown) != "" {- page.Blocks = []OCRBlock{{Text: markdown}}+ if hasMedia {+ // Media blocks own page regions but no body text. Keep them and+ // append the Markdown only when it carries text of its own, so+ // neither the regions nor text that exists only in the Markdown+ // are dropped.+ if strings.TrimSpace(utility.StripMarkdownImages(markdown)) != "" {+ page.Blocks = append(page.Blocks, OCRBlock{Text: markdown})+ }+ } else {+ page.Blocks = []OCRBlock{{Text: markdown}}+ }}pages = append(pages, page)nextPage = page.Index + 1@@ -176,6 +192,38 @@ func paddleOCRPages(result paddleOCRResult, nextPage int) []OCRPage {return pages}+// IsOCRMediaLabel reports whether a PaddleX layout label denotes a figure-like+// region. Such a block has no body text of its own but still carries a page+// region, so the page fallback must keep it and the PDF projection must emit a+// media item. chart_title and figure_title are captions, not media.+func IsOCRMediaLabel(label string) bool {+ switch strings.ToLower(strings.TrimSpace(label)) {+ case "figure", "image", "chart", "seal":+ return true+ }+ return false+}++// IsOCRImageOnlyText reports whether block text carries image markup but no+// body text. Some providers leave the layout label empty, so the label alone+// cannot decide whether a block is media.+func IsOCRImageOnlyText(text string) bool {+ return strings.TrimSpace(text) != "" && strings.TrimSpace(utility.StripMarkdownImages(text)) == ""+}++// hasOCRMediaBlocks reports whether any page kept a media region. Such a+// response has no text of its own but must still reach the PDF media path.+func hasOCRMediaBlocks(pages []OCRPage) bool {+ for _, page := range pages {+ for _, block := range page.Blocks {+ if IsOCRMediaLabel(block.Label) || IsOCRImageOnlyText(block.Text) {+ return true+ }+ }+ }+ return false+}+func (pruned paddleOCRPrunedResult) blocks() []OCRBlock {var blocks []OCRBlockfor _, raw := range pruned.Blocks {diff --git a/internal/entity/models/paddleocr_test.go b/internal/entity/models/paddleocr_test.goindex 5dba43c51e..8e409d25aa 100644--- a/internal/entity/models/paddleocr_test.go+++ b/internal/entity/models/paddleocr_test.go@@ -501,9 +501,9 @@ func TestPaddleOCRPageMarkdownFallbackWhenBlocksHaveNoText(t *testing.T) {{"only header", `[{"block_label":"header","block_content":"ignored header","block_bbox":[10,20,30,40]}]`, "page fallback", "[]"},{"only page number", `[{"block_label":"number","block_content":"12"}]`, "page fallback", "[]"},{"empty blocks", `[{"block_content":" "}]`, "page fallback", "[]"},- {"markdown image", `[{"block_content":"","block_bbox":[10,20,30,40]}]`, "page fallback", "[]"},- {"parenthesized image destination", `[{"block_content":".png)","block_bbox":[10,20,30,40]}]`, "page fallback", "[]"},- {"html image", `[{"block_content":"<div><img src=\"a.png\"></div>"}]`, "page fallback", "[]"},+ {"markdown image", `[{"block_content":"","block_bbox":[10,20,30,40]}]`, "\npage fallback", "[]"},+ {"parenthesized image destination", `[{"block_content":".png)","block_bbox":[10,20,30,40]}]`, ".png)\npage fallback", "[]"},+ {"html image", `[{"block_content":"<div><img src=\"a.png\"></div>"}]`, "<div><img src=\"a.png\"></div>\npage fallback", "[]"},{"text among images", `[{"block_content":""},{"block_content":"positioned text","block_bbox":[10,20,30,40]}]`, "\npositioned text", "[10 20 30 40]"},} {t.Run(tc.name, func(t *testing.T) {@@ -531,6 +531,64 @@ func TestPaddleOCRPageMarkdownFallbackWhenBlocksHaveNoText(t *testing.T) {}}+func TestPaddleOCRPageFallbackKeepsMediaBlocks(t *testing.T) {+ body := `{"result":{"layoutParsingResults":[{"markdown":{"text":"page fallback"},"prunedResult":{"page_index":0,"parsing_res_list":[{"block_label":"figure","block_content":"","block_bbox":[10,20,30,40]},{"block_label":"chart","block_content":"","block_bbox":[50,60,90,100]}]}}]}}`+ response, err := parsePaddleOCRResultBody([]byte(body))+ if err != nil {+ t.Fatal(err)+ }+ blocks := response.Pages[0].Blocks+ if len(blocks) != 3 {+ t.Fatalf("blocks=%+v", blocks)+ }+ for i, want := range []struct{ label, box string }{{"figure", "[10 20 30 40]"}, {"chart", "[50 60 90 100]"}} {+ if blocks[i].Label != want.label || fmt.Sprint(blocks[i].BBox) != want.box {+ t.Fatalf("block %d = %+v", i, blocks[i])+ }+ }+ if blocks[2].Text != "page fallback" || blocks[2].BBox != nil {+ t.Fatalf("markdown block = %+v", blocks[2])+ }+}++// TestPaddleOCRPageFallbackKeepsUnlabeledImageBlock covers providers that emit+// image markup without a layout label: the region must survive the fallback so+// the PDF projection can emit it as a media item with its bbox.+func TestPaddleOCRPageFallbackKeepsUnlabeledImageBlock(t *testing.T) {+ body := `{"result":{"layoutParsingResults":[{"markdown":{"text":"page fallback"},"prunedResult":{"page_index":0,"parsing_res_list":[{"block_content":"","block_bbox":[10,20,30,40]}]}}]}}`+ response, err := parsePaddleOCRResultBody([]byte(body))+ if err != nil {+ t.Fatal(err)+ }+ blocks := response.Pages[0].Blocks+ if len(blocks) != 2 {+ t.Fatalf("blocks=%+v", blocks)+ }+ if blocks[0].Text != "" || fmt.Sprint(blocks[0].BBox) != "[10 20 30 40]" {+ t.Fatalf("image block = %+v", blocks[0])+ }+ if blocks[1].Text != "page fallback" || blocks[1].BBox != nil {+ t.Fatalf("markdown block = %+v", blocks[1])+ }+}++// TestParsePaddleOCRResultBodyAcceptsMediaOnlyPage covers a page whose blocks+// are media regions with no text and empty Markdown: Text is empty but non-nil,+// and the page must still reach the PDF media path.+func TestParsePaddleOCRResultBodyAcceptsMediaOnlyPage(t *testing.T) {+ body := `{"result":{"layoutParsingResults":[{"markdown":{"text":""},"prunedResult":{"page_index":0,"parsing_res_list":[{"block_label":"figure","block_content":"","block_bbox":[10,20,30,40]}]}}]}}`+ response, err := parsePaddleOCRResultBody([]byte(body))+ if err != nil {+ t.Fatal(err)+ }+ if response.Text == nil || *response.Text != "" {+ t.Fatalf("text=%v, want non-nil empty", response.Text)+ }+ if len(response.Pages) != 1 || len(response.Pages[0].Blocks) != 1 || response.Pages[0].Blocks[0].Label != "figure" {+ t.Fatalf("pages=%+v", response.Pages)+ }+}+func TestPaddleOCRPagesExcludeMarkdownIgnoredLabels(t *testing.T) {for _, label := range []string{"header", "header_image", "footer", "footer_image", "number", "aside_text", "footnote", " HEADER "} {t.Run(label, func(t *testing.T) {diff --git a/internal/entity/models/types.go b/internal/entity/models/types.goindex bbe1286263..306d55d24d 100644--- a/internal/entity/models/types.go+++ b/internal/entity/models/types.go@@ -143,6 +143,7 @@ type OCRBlock struct {}type OCRFileResponse struct {+ // Text is empty (but non-nil) when the response carries only media regions.Text *string `json:"text"`// Pages is optional structured output for internal parsing; nil means unavailable.Pages []OCRPage `json:"-"`diff --git a/internal/ingestion/component/chunker/common.go b/internal/ingestion/component/chunker/common.goindex 5e0cbf7eef..5c934d3037 100644--- a/internal/ingestion/component/chunker/common.go+++ b/internal/ingestion/component/chunker/common.go@@ -19,6 +19,7 @@ package chunkerimport ("crypto/sha256""encoding/hex"+ "encoding/json""fmt""regexp""strings"@@ -220,8 +221,16 @@ func itemDocType(it schema.ChunkDoc) string {// isMediaChunk reports whether a chunk is an image or table region. The// content type lives on doc_type_kwd; layout never describes media.func isMediaChunk(ck schema.ChunkDoc) bool {- typ := itemDocType(ck)- return typ == "image" || typ == "table"+ return isMediaDocType(ck.DocType)+}++// isMediaDocType reports whether a doc_type_kwd value denotes a media region.+func isMediaDocType(docType string) bool {+ switch strings.ToLower(strings.TrimSpace(docType)) {+ case "image", "table":+ return true+ }+ return false}// hasMediaPayload reports whether a chunk still carries retrievable media:@@ -369,12 +378,52 @@ func canonicalChunkID(docID string, ck schema.ChunkDoc) string {// the text alone would give every such chunk in a document the same id// (ChunkID(docID, "")), silently overwriting images in MinIO and merging// index entries, so a stable per-image identity is folded in instead.-// Non-empty text hashes unchanged.+// Non-empty media text also folds its region in: two figures can share a+// caption ("Figure", "图"), and their previews must not collapse onto one id.+// Text chunks hash their text unchanged.func chunkIDSource(ck schema.ChunkDoc) string {- if text := canonicalChunkText(ck); text != "" {+ text := canonicalChunkText(ck)+ if text == "" {+ return mediaChunkIdentity(ck.Image, ck.ImgID, ck.MediaRef)+ }+ return mediaTextIdentity(text, isMediaChunk(ck), mergePositionMatrix(ck.PDFPositions, ck.Positions))+}++// mediaTextIdentity appends a region digest to a media chunk's text identity.+// Non-media chunks and media without a region keep their text-only id.+func mediaTextIdentity(text string, isMedia bool, matrix [][]any) string {+ if !isMedia || len(matrix) == 0 {return text}- return mediaChunkIdentity(ck.Image, ck.ImgID, ck.MediaRef)+ sum := sha256.Sum256(positionMatrixBytes(matrix))+ return text + mediaChunkIDPrefix + "region:" + hex.EncodeToString(sum[:16])+}++// positionMatrixBytes renders a position matrix deterministically for hashing.+// mergePositionMatrix already sorts the rows, so equal regions always encode+// identically.+func positionMatrixBytes(matrix [][]any) []byte {+ raw, err := json.Marshal(matrix)+ if err != nil {+ return nil+ }+ return raw+}++// chunkPositionMatrix decodes the position rows of a chunk map for identity+// computation, using the same decoder as the crop path.+func chunkPositionMatrix(ck map[string]any) [][]any {+ var sources []json.RawMessage+ for _, key := range []string{"_pdf_positions", "positions"} {+ value, ok := ck[key]+ if !ok || value == nil {+ continue+ }+ if raw, err := json.Marshal(value); err == nil {+ sources = append(sources, raw)+ }+ }+ return mergePositionMatrix(sources...)}// mediaChunkIdentity returns the stable identity of a text-less media chunk:diff --git a/internal/ingestion/component/chunker/pdfcrop_cgo_test.go b/internal/ingestion/component/chunker/pdfcrop_cgo_test.goindex 3ddcab0294..ce3e519aa0 100644--- a/internal/ingestion/component/chunker/pdfcrop_cgo_test.go+++ b/internal/ingestion/component/chunker/pdfcrop_cgo_test.go@@ -384,7 +384,7 @@ func TestCropImageChunks_StreamingUpload(t *testing.T) {// matches the expected per-chunk ids rather than the call order.wantIDs := make(map[string]struct{}, len(chunks))for _, ck := range chunks {- wantIDs[common.ChunkID("doc1", ck.Text)] = struct{}{}+ wantIDs[canonicalChunkID("doc1", ck)] = struct{}{}}for _, c := range rec.calls {if c.dataLen == 0 {@@ -398,7 +398,7 @@ func TestCropImageChunks_StreamingUpload(t *testing.T) {// ("<kb_id>-<chunkID>"); keyed by input position, which is deterministic// even though the upload order is not.for i, ck := range out {- wantImgID := "kb1-" + common.ChunkID("doc1", chunks[i].Text)+ wantImgID := "kb1-" + canonicalChunkID("doc1", chunks[i])if ck.ImgID != wantImgID {t.Errorf("chunk %d: ImgID = %q, want %q", i, ck.ImgID, wantImgID)}@@ -492,8 +492,8 @@ func TestCropImageChunks_StreamingUploadUsesFinalizedText(t *testing.T) {// Finalized text = removeTag(ContextAbove + removeTag(Text) + ContextBelow)// = "ABOVE body".- wantID := common.ChunkID("doc1", "ABOVE body")- rawID := common.ChunkID("doc1", "body")+ wantID := canonicalChunkID("doc1", schema.ChunkDoc{DocType: "image", Text: "body", ContextAbove: "ABOVE ", PDFPositions: pos})+ rawID := canonicalChunkID("doc1", schema.ChunkDoc{DocType: "image", Text: "body", PDFPositions: pos})if rec.calls[0].chunkID == rawID {t.Errorf("upload keyed by raw text %q; want finalized-text key %q", rawID, wantID)@@ -506,6 +506,71 @@ func TestCropImageChunks_StreamingUploadUsesFinalizedText(t *testing.T) {}}+// TestCropImageChunks_DistinctIdsForSameTextMedia pins the identity contract+// for media chunks that share a caption: a PaddleOCR/DeepDoc document can emit+// several figures whose text is the same short label ("图", "Figure"), and a+// text-only id collapses them onto one chunk id — one MinIO object overwrites+// the other and the index keeps a single entry. The region digest folded into+// the media identity separates them, end to end: distinct canonical ids,+// distinct streamed upload keys, distinct img_ids.+func TestCropImageChunks_DistinctIdsForSameTextMedia(t *testing.T) {+ ctx := withIngestionGlobals(t, "kb1", "doc1")++ rec := &recordingUploader{}+ orig := ChunkImageUploader+ ChunkImageUploader = rec.upload+ t.Cleanup(func() { ChunkImageUploader = orig })++ eng := mockCropEngine{}+ chunks := []schema.ChunkDoc{+ {DocType: "image", Text: "图", PDFPositions: jsonPositions(t, []float64{1, 10, 100, 10, 100})},+ {DocType: "image", Text: "图", PDFPositions: jsonPositions(t, []float64{1, 200, 300, 400, 500})},+ }++ ids := []string{canonicalChunkID("doc1", chunks[0]), canonicalChunkID("doc1", chunks[1])}+ if ids[0] == ids[1] {+ t.Fatalf("same-text media at different regions share chunk id %q", ids[0])+ }++ out := cropImageChunks(ctx, eng, chunks)+ if len(out) != 2 {+ t.Fatalf("len(out) = %d, want 2", len(out))+ }+ if out[0].ImgID == out[1].ImgID {+ t.Errorf("preview objects collapsed: both chunks ImgID = %q", out[0].ImgID)+ }+ if len(rec.calls) != 2 {+ t.Fatalf("upload calls = %d, want 2", len(rec.calls))+ }+ keys := map[string]struct{}{}+ for _, c := range rec.calls {+ keys[c.chunkID] = struct{}{}+ }+ if len(keys) != 2 {+ t.Errorf("upload keys = %v, want 2 distinct", keys)+ }+ for i, ck := range out {+ if ck.ImgID != "kb1-"+ids[i] {+ t.Errorf("chunk %d: ImgID = %q, want %q", i, ck.ImgID, "kb1-"+ids[i])+ }+ // cropImageChunks stamps the final chunk id alongside img_id; the+ // decorator reuses it, so this is the id the index and retrieval see.+ if ck.ID != ids[i] {+ t.Errorf("chunk %d: ID = %q, want %q", i, ck.ID, ids[i])+ }+ }++ // Text chunks are unaffected by the region digest: the same body at two+ // locations must keep the plain text id.+ textIDs := []string{+ canonicalChunkID("doc1", schema.ChunkDoc{Text: "same body", PDFPositions: chunks[0].PDFPositions}),+ canonicalChunkID("doc1", schema.ChunkDoc{Text: "same body", PDFPositions: chunks[1].PDFPositions}),+ }+ if textIDs[0] != textIDs[1] {+ t.Errorf("text chunk ids diverged by position: %q vs %q", textIDs[0], textIDs[1])+ }+}+// TestCropImageChunks_SkipsEmptyTextMedia verifies that a media chunk whose// finalized text is empty — no caption, no vision description, no media// context — is NOT cropped or uploaded. Such a chunk is not retrievablediff --git a/internal/ingestion/component/chunker/register.go b/internal/ingestion/component/chunker/register.goindex 59c9e7a230..d575614aab 100644--- a/internal/ingestion/component/chunker/register.go+++ b/internal/ingestion/component/chunker/register.go@@ -143,6 +143,9 @@ func (d *imageUploadDecorator) Invoke(ctx context.Context, db *gorm.DB, inputs mimage, _ := ck["image"].(string)imgID, _ := ck["img_id"].(string)text = mediaChunkIdentity(image, imgID, entity.MediaRefFromAny(ck["media_ref"]))+ } else {+ docType, _ := ck["doc_type_kwd"].(string)+ text = mediaTextIdentity(text, isMediaDocType(docType), chunkPositionMatrix(ck))}ck["id"] = common.ChunkID(docID, text)}diff --git a/internal/parser/parser/pdf_parser_paddleocr.go b/internal/parser/parser/pdf_parser_paddleocr.goindex c169f471db..81acddf33b 100644--- a/internal/parser/parser/pdf_parser_paddleocr.go+++ b/internal/parser/parser/pdf_parser_paddleocr.go@@ -59,6 +59,19 @@ func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, respoif err := ctx.Err(); err != nil {return ParseResult{Err: err}}+ label := strings.ToLower(strings.TrimSpace(block.Label))+ if models.IsOCRMediaLabel(label) || models.IsOCRImageOnlyText(block.Text) {+ text := strings.TrimSpace(utility.StripMarkdownImages(block.Text))+ position := paddleOCRBlockPosition(page, block.BBox, size)+ // A block with neither body text nor a crop region has nothing+ // media can act on: a crop preview needs positive-area geometry,+ // and a synthetic text body would index as retrievable text.+ if text == "" && len(position) == 0 {+ continue+ }+ result.JSON = append(result.JSON, paddleOCRMediaItem(page, text, position))+ continue+ }content := strings.TrimSpace(utility.StripMarkdownImages(block.Text))if content == "" {continue@@ -69,7 +82,6 @@ func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, respo}result.Warnings = append(result.Warnings, parsed.Warnings...)position := paddleOCRBlockPosition(page, block.BBox, size)- label := strings.ToLower(strings.TrimSpace(block.Label))isTitle := label == "doc_title" || label == "paragraph_title"if label == "figure_title" || label == "table_title" {label = "caption"@@ -132,6 +144,28 @@ func PaddleOCRPDFResult(ctx context.Context, filename string, data []byte, resporeturn result}+// paddleOCRMediaItem keeps a figure-like block as a media item. The text stays+// empty when the block carries no body text: the region is preserved for the+// vision pass and the on-demand crop, while a synthetic placeholder would+// index as retrievable body text (#21218). The chunker drops a text-less media+// item that never became retrievable, matching the DeepDoc figure path.+func paddleOCRMediaItem(page models.OCRPage, text string, position []any) DocItem {+ fields := map[string]any{+ "doc_type_kwd": "image",+ "layout": "figure",+ "page_number": page.Index + 1,+ "_preserve_order": true,+ }+ if text != "" {+ fields["text"] = text+ }+ if len(position) > 0 {+ matrix := [][]any{position}+ fields["positions"], fields["_pdf_positions"] = matrix, matrix+ }+ return DocItemFromMap(fields)+}+func paddleOCRBlockPosition(page models.OCRPage, bbox []float64, size [2]float64) []any {if page.Index < 0 || !paddleOCRPositive(page.Width) || !paddleOCRPositive(page.Height) || !paddleOCRPositive(size[0]) || !paddleOCRPositive(size[1]) {return nildiff --git a/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go b/internal/parser/parser/pdf_parser_paddleocr_cgo_test.goindex cfc8e627fc..d711ac9ade 100644--- a/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go+++ b/internal/parser/parser/pdf_parser_paddleocr_cgo_test.go@@ -79,6 +79,34 @@ func TestPaddleOCRPDFResultKeepsGeometryAfterOnePageFails(t *testing.T) {}}+func TestPaddleOCRPDFResultLocatesMediaRegions(t *testing.T) {+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")+ if err != nil {+ t.Fatal(err)+ }+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{+ {Label: "figure", Text: "", BBox: []float64{10, 20, 100, 120}},+ {Label: "image", Text: "Figure 1: pipeline", BBox: []float64{10, 130, 100, 230}},+ }}}}+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)+ if result.Err != nil || len(result.JSON) != 2 {+ t.Fatalf("result=%+v", result)+ }+ for i, want := range [][]any{+ {1, float64(5), float64(50), float64(10), float64(60)},+ {1, float64(5), float64(50), float64(65), float64(115)},+ } {+ fields := result.JSON[i].ToMap()+ positions, ok := fields["_pdf_positions"].([][]any)+ if !ok || len(positions) != 1 || !reflect.DeepEqual(positions[0], want) {+ t.Fatalf("item %d positions = %v, want %v", i, fields["_pdf_positions"], want)+ }+ if !reflect.DeepEqual(fields["positions"], fields["_pdf_positions"]) {+ t.Fatalf("item %d positions mismatch: %v vs %v", i, fields["positions"], fields["_pdf_positions"])+ }+ }+}+func TestPaddleOCRPDFResultDropsMismatchedPageGeometry(t *testing.T) {data, err := os.ReadFile("testdata/paddleocr_pages.pdf")if err != nil {@@ -130,3 +158,56 @@ func TestPaddleOCRPDFResultDropsMismatchedPageGeometry(t *testing.T) {})}}++func TestPaddleOCRPDFResultKeepsMediaItems(t *testing.T) {+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")+ if err != nil {+ t.Fatal(err)+ }+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{+ {Label: "figure", Text: "", BBox: []float64{10, 20, 100, 120}},+ {Label: "image", Text: "Figure 1: pipeline", BBox: []float64{10, 130, 100, 230}},+ {Label: "chart", Text: "", BBox: []float64{10, 240, 100, 340}},+ {Label: "seal", Text: "", BBox: []float64{10, 350, 100, 450}},+ {Text: "", BBox: []float64{10, 460, 100, 560}},+ }}}}+ result := PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response)+ if result.Err != nil || len(result.JSON) != 5 {+ t.Fatalf("result=%+v", result)+ }+ for i, wantText := range []string{"", "Figure 1: pipeline", "", "", ""} {+ item := result.JSON[i].ToMap()+ gotText, _ := item["text"].(string)+ if item["doc_type_kwd"] != "image" || item["layout"] != "figure" || gotText != wantText {+ t.Fatalf("item %d = %v", i, item)+ }+ if item["page_number"] != 1 {+ t.Fatalf("item %d page_number = %v", i, item["page_number"])+ }+ if item["positions"] == nil {+ t.Fatalf("item %d lost its crop region: %v", i, item)+ }+ }+}++func TestPaddleOCRMediaItemsSurviveNormalize(t *testing.T) {+ data, err := os.ReadFile("testdata/paddleocr_pages.pdf")+ if err != nil {+ t.Fatal(err)+ }+ response := &models.OCRFileResponse{Pages: []models.OCRPage{{Index: 0, Width: 1191, Height: 1684, Blocks: []models.OCRBlock{+ {Label: "figure", Text: "", BBox: []float64{10, 20, 100, 120}},+ {Label: "image", Text: "Figure 1: pipeline", BBox: []float64{10, 130, 100, 230}},+ }}}}+ result := NewPDFParser().NormalizeResult(t.Context(), "sample.pdf", PaddleOCRPDFResult(t.Context(), "sample.pdf", data, response))+ if result.Err != nil || len(result.JSON) != 2 {+ t.Fatalf("result=%+v", result)+ }+ for i, wantText := range []string{"", "Figure 1: pipeline"} {+ item := result.JSON[i].ToMap()+ gotText, _ := item["text"].(string)+ if item["doc_type_kwd"] != "image" || item["layout"] != "figure" || gotText != wantText || item["page_number"] != 1 {+ t.Fatalf("item %d = %v", i, item)+ }+ }+}diff --git a/internal/parser/parser/pdf_parser_paddleocr_test.go b/internal/parser/parser/pdf_parser_paddleocr_test.goindex 9d4286414d..4c0bca8aa1 100644--- a/internal/parser/parser/pdf_parser_paddleocr_test.go+++ b/internal/parser/parser/pdf_parser_paddleocr_test.go@@ -34,7 +34,7 @@ func TestPaddleOCRBlockPosition(t *testing.T) {}}-func TestPaddleOCRPDFResultKeepsUnlocatedText(t *testing.T) {+func TestPaddleOCRPDFResultDropsUnlocatedMedia(t *testing.T) {data, err := os.ReadFile("testdata/paddleocr_pages.pdf")if err != nil {t.Fatal(err)@@ -48,6 +48,8 @@ func TestPaddleOCRPDFResultKeepsUnlocatedText(t *testing.T) {if item["text"] != "keep me" || item["positions"] != nil {t.Fatalf("item=%v", item)}+ // The image-only block has neither body text nor a crop region: a+ // placeholder would index as body text (#21218), so it is dropped.}func TestPaddleOCRPDFResultKeepsUnstructuredTableText(t *testing.T) {diff --git a/internal/parser/parser/pdf_result.go b/internal/parser/parser/pdf_result.goindex d0ce47c710..62f3d1f01f 100644--- a/internal/parser/parser/pdf_result.go+++ b/internal/parser/parser/pdf_result.go@@ -94,7 +94,14 @@ func (p *PDFParser) NormalizeResult(ctx context.Context, filename string, resultfields["text"] = strings.TrimSpace(tableutil.RenderTableHTML(table) + "\n" + text)delete(fields, "table")}- fields["doc_type_kwd"] = "text"+ // Flattening moves an item's content into text. A crop-only media+ // item has no content to move: rewriting it to text would hide the+ // region from vision enhancement and leave an empty item the+ // chunker drops, so it keeps its media type and stays available to+ // the on-demand cropper.+ if hasPDFTextCarrier(fields) || len(positions) == 0 {+ fields["doc_type_kwd"] = "text"+ }}items = appendPDFItem(items, fields)}@@ -304,6 +311,18 @@ func pdfPositionHasRegion(values [5]float64) bool {return values[2] > values[1] && values[4] > values[3]}+// hasPDFTextCarrier reports whether flattening an item has any content to move+// into text: a body, an inline image payload, or a deferred media reference.+func hasPDFTextCarrier(fields map[string]any) bool {+ if text, _ := fields["text"].(string); strings.TrimSpace(text) != "" {+ return true+ }+ if img, _ := fields["image"].(string); img != "" {+ return true+ }+ return entity.MediaRefFromAny(fields["media_ref"]) != nil+}+// HasPDFCropRegion reports whether the item carries at least one PDF crop// region, i.e. a position with a positive-area box. A page-only position// carries page identity but no region, so it must not trigger a crop.diff --git a/internal/parser/parser/pdf_result_test.go b/internal/parser/parser/pdf_result_test.goindex 10b4a542d6..7eee494b48 100644--- a/internal/parser/parser/pdf_result_test.go+++ b/internal/parser/parser/pdf_result_test.go@@ -160,6 +160,34 @@ func TestPDFNormalizeResultFlattenPreservesContentAndClearsMediaSubtype(t *testi}}+func TestPDFNormalizeResultFlattenKeepsCropOnlyMedia(t *testing.T) {+ p := NewPDFParser()+ p.FlattenMediaToText = true+ result := p.NormalizeResult(t.Context(), "sample.pdf", ParseResult{JSON: MapsToItems([]map[string]any{+ {"doc_type_kwd": "image", "layout": "figure", "positions": [][]any{{1, 10, 30, 20, 40}}},+ {"doc_type_kwd": "image", "layout": "figure", "text": "Figure 1", "positions": [][]any{{1, 10, 30, 50, 70}}},+ {"doc_type_kwd": "image", "layout": "figure"},+ })})+ if result.Err != nil || len(result.JSON) != 3 {+ t.Fatalf("unexpected result: %+v", result)+ }+ // A crop-only media item has no content to flatten; vision enhancement and+ // the on-demand cropper still need its type and region.+ cropOnly := result.JSON[0].ToMap()+ if cropOnly["doc_type_kwd"] != "image" || !HasPDFCropRegion(cropOnly) {+ t.Fatalf("crop-only media lost its type or region: %#v", cropOnly)+ }+ captioned := result.JSON[1].ToMap()+ if captioned["doc_type_kwd"] != "text" || captioned["text"] != "Figure 1" {+ t.Fatalf("captioned media not flattened: %#v", captioned)+ }+ // Without a region there is nothing to crop, so the empty item still+ // flattens to text and is dropped downstream as before.+ if bare := result.JSON[2].ToMap(); bare["doc_type_kwd"] != "text" {+ t.Fatalf("regionless media kept a media type: %#v", bare)+ }+}+func TestPDFNormalizeResultDowngradesUnstructuredTableSubtype(t *testing.T) {result := NewPDFParser().NormalizeResult(t.Context(), "sample.pdf", ParseResult{JSON: MapsToItems([]map[string]any{{"doc_type_kwd": "table", "layout": "table", "text": "Recognized text without cells",