Skip to content

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

Fix: add and use paddleOCR.net model (#18696)

Lynn1711d37b96cf
diff --git a/internal/entity/models/paddleocr.go b/internal/entity/models/paddleocr.go
index 0ff2c6561a..de50a2a9bb 100644
--- a/internal/entity/models/paddleocr.go
+++ b/internal/entity/models/paddleocr.go
@@ -25,9 +25,12 @@ import (
"io"
"mime/multipart"
"net/http"
- "ragflow/internal/common"
"strings"
"time"
+
+ "ragflow/internal/common"
+
+ "go.uber.org/zap"
)
type PaddleOCRModel struct {
@@ -102,7 +105,10 @@ type paddlePollResponse struct {
}
type paddleJsonlLine struct {
- Result struct {
+ LogId string `json:"logId"`
+ ErrorCode int `json:"errorCode"`
+ ErrorMsg string `json:"errorMsg"`
+ Result struct {
LayoutParsingResults []struct {
Markdown struct {
Text string `json:"text"`
@@ -116,6 +122,127 @@ type paddleJsonlLine struct {
} `json:"result"`
}
+// maxErrorBodyBytes caps how much of a failed HTTP response body is read and
+// logged so an oversized or erroneous payload cannot consume unbounded memory
+// or produce unbounded log lines / error strings.
+const maxErrorBodyBytes = 64 * 1024
+
+// readErrorBody drains at most maxErrorBodyBytes of an error response body for
+// logging. It must be called before the body is otherwise consumed.
+func readErrorBody(body io.Reader) string {
+ data, _ := io.ReadAll(io.LimitReader(body, maxErrorBodyBytes))
+ return string(data)
+}
+
+// logBody truncates an already-buffered response payload to maxErrorBodyBytes
+// so it is safe for log fields and error strings.
+func logBody(body []byte) string {
+ if len(body) > maxErrorBodyBytes {
+ return string(body[:maxErrorBodyBytes]) + "…(truncated)"
+ }
+ return string(body)
+}
+
+// appendOCRText appends the markdown or OCR text fragments carried by one
+// result entry to fullMarkdown and reports whether any text was added.
+// Entries whose errorCode != 0 are treated as errored rather than silently
+// empty so a partial/errored page is not invisible to the caller.
+func (p *PaddleOCRModel) appendOCRText(fullMarkdown *strings.Builder, entry paddleJsonlLine) (added, errored bool) {
+ if entry.ErrorCode != 0 {
+ return false, true
+ }
+ before := fullMarkdown.Len()
+ for _, layoutRes := range entry.Result.LayoutParsingResults {
+ fullMarkdown.WriteString(layoutRes.Markdown.Text)
+ fullMarkdown.WriteString("\n\n")
+ }
+
+ // Fallback to ocrResults for models like PP-OCRv6
+ if len(entry.Result.LayoutParsingResults) == 0 {
+ for _, ocrRes := range entry.Result.OcrResults {
+ for _, text := range ocrRes.PrunedResult.RecTexts {
+ text = strings.TrimSpace(text)
+ if text != "" {
+ fullMarkdown.WriteString(text)
+ fullMarkdown.WriteString("\n")
+ }
+ }
+ }
+ }
+ return fullMarkdown.Len() > before, false
+}
+
+// parseOCRResultBody extracts markdown/OCR text from the downloaded result
+// payload. The online service returns a JSON array of page objects (list of
+// dict), while older endpoints may return newline-delimited JSON objects;
+// both forms are accepted. It reports how the payload was interpreted
+// (arrayParsed) together with parse accounting counters so callers can log
+// why the result was empty: entries dropped by json.Unmarshal (skippedLines),
+// decoded entries carrying no text fragment (emptyResultLines), entries whose
+// errorCode != 0 (erroredLines), and entries that yielded text (contentLines).
+func (p *PaddleOCRModel) parseOCRResultBody(rawBody []byte, fullMarkdown *strings.Builder) (arrayParsed bool, scannedLines, skippedLines, emptyResultLines, contentLines, erroredLines int, err error) {
+ var entries []paddleJsonlLine
+ err = json.Unmarshal(rawBody, &entries)
+ if err == nil {
+ arrayParsed = true
+ scannedLines = len(entries)
+ for _, entry := range entries {
+ added, errored := p.appendOCRText(fullMarkdown, entry)
+ switch {
+ case errored:
+ erroredLines++
+ p.logErroredEntry(entry)
+ case added:
+ contentLines++
+ default:
+ emptyResultLines++
+ }
+ }
+ return
+ }
+
+ scanner := bufio.NewScanner(bytes.NewReader(rawBody))
+ scanner.Buffer(make([]byte, 64*1024), 1024*1024)
+ for scanner.Scan() {
+ line := strings.TrimSpace(scanner.Text())
+ if line == "" {
+ continue
+ }
+ scannedLines++
+
+ var lineData paddleJsonlLine
+ if err := json.Unmarshal([]byte(line), &lineData); err != nil {
+ skippedLines++
+ continue
+ }
+ added, errored := p.appendOCRText(fullMarkdown, lineData)
+ switch {
+ case errored:
+ erroredLines++
+ p.logErroredEntry(lineData)
+ case added:
+ contentLines++
+ default:
+ emptyResultLines++
+ }
+ }
+ if err := scanner.Err(); err != nil {
+ return false, 0, 0, 0, 0, 0, fmt.Errorf("error reading jsonl: %w", err)
+ }
+ // The array-parse error stored in err is expected on this path; the jsonl
+ // fallback succeeded, so clear it before the bare return.
+ return false, scannedLines, skippedLines, emptyResultLines, contentLines, erroredLines, nil
+}
+
+// logErroredEntry makes an entry with a non-zero errorCode visible instead of
+// silently dropping the page as empty.
+func (p *PaddleOCRModel) logErroredEntry(entry paddleJsonlLine) {
+ common.Warn("paddleocr.net result: entry errored",
+ zap.String("log_id", entry.LogId),
+ zap.Int("error_code", entry.ErrorCode),
+ zap.String("error_msg", entry.ErrorMsg))
+}
+
func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content []byte, fileURL *string, apiConfig *APIConfig, ocrConfig *OCRConfig, modelUsage *common.ModelUsage) (*OCRFileResponse, error) {
if err := p.baseModel.APIConfigCheck(apiConfig); err != nil {
return nil, err
@@ -180,19 +307,41 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
}
req.Header.Set("Client-Platform", "ragflow")
+ common.Info("paddleocr.net submit: sending job",
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.String("model", *modelName),
+ zap.Int("content_bytes", len(content)),
+ zap.Bool("by_url", fileURL != nil && strings.HasPrefix(*fileURL, "http")))
+
resp, err := p.baseModel.httpClient.Do(req)
if err != nil {
+ common.Error("paddleocr.net submit: request failed",
+ err,
+ zap.String("driver", p.Name()),
+ zap.String("url", url))
return nil, fmt.Errorf("failed to submit job: %w", err)
}
defer resp.Body.Close()
- respBody, _ := io.ReadAll(resp.Body)
if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("submit job failed: %s", string(respBody))
+ errBody := readErrorBody(resp.Body)
+ common.Error("paddleocr.net submit: non-200",
+ fmt.Errorf("status %d", resp.StatusCode),
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.Int("status", resp.StatusCode),
+ zap.String("body", errBody))
+ return nil, fmt.Errorf("submit job failed: %s", errBody)
}
+ respBody, _ := io.ReadAll(resp.Body)
var submitResp paddleSubmitResponse
if err := json.Unmarshal(respBody, &submitResp); err != nil {
+ common.Error("paddleocr.net submit: parse failed",
+ err,
+ zap.String("driver", p.Name()),
+ zap.String("url", url))
return nil, fmt.Errorf("failed to parse submit response: %w", err)
}
@@ -201,6 +350,11 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
return nil, fmt.Errorf("failed to get jobId from response")
}
+ common.Info("paddleocr.net submit: ok",
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.String("job_id", jobId))
+
pollUrl := fmt.Sprintf("%s/%s", url, jobId)
var jsonlUrl string
@@ -208,7 +362,9 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
const pollMultiplier = 1.5
maxPollInterval := 15 * time.Second
+ attempt := 0
for {
+ attempt++
pollReq, err := http.NewRequestWithContext(ctx, "GET", pollUrl, nil)
if err != nil {
return nil, fmt.Errorf("failed to create poll request: %w", err)
@@ -220,36 +376,72 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
pollResp, err := p.baseModel.httpClient.Do(pollReq)
if err != nil {
+ common.Error("paddleocr.net poll: request failed",
+ err,
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt))
return nil, fmt.Errorf("failed to poll job status: %w", err)
}
- pollBody, _ := io.ReadAll(pollResp.Body)
- pollResp.Body.Close()
-
if pollResp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("poll job failed: %s", string(pollBody))
+ errBody := readErrorBody(pollResp.Body)
+ pollResp.Body.Close()
+ common.Error("paddleocr.net poll: non-200",
+ fmt.Errorf("status %d", pollResp.StatusCode),
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt),
+ zap.Int("status", pollResp.StatusCode),
+ zap.String("body", errBody))
+ return nil, fmt.Errorf("poll job failed: %s", errBody)
}
+ pollBody, _ := io.ReadAll(pollResp.Body)
+ pollResp.Body.Close()
var pollData paddlePollResponse
if err = json.Unmarshal(pollBody, &pollData); err != nil {
+ common.Error("paddleocr.net poll: parse failed",
+ err,
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt))
return nil, fmt.Errorf("failed to parse poll response: %w", err)
}
// end if 'done' or 'failed'
state := pollData.Data.State
if state == "done" {
+ common.Info("paddleocr.net poll: done",
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt),
+ zap.String("state", state))
jsonlUrl = pollData.Data.ResultUrl.JsonUrl
break
} else if state == "failed" {
+ common.Error("paddleocr.net poll: failed",
+ fmt.Errorf("job failed on server"),
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt),
+ zap.String("state", state),
+ zap.String("error_msg", pollData.Data.ErrorMsg))
return nil, fmt.Errorf("ocr job failed on server: %s", pollData.Data.ErrorMsg)
}
+ common.Debug("paddleocr.net poll: in progress",
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt),
+ zap.String("state", state),
+ zap.Duration("next_poll_in", pollInterval))
+
// Exponential backoff
pollInterval = min(time.Duration(float64(pollInterval)*pollMultiplier), maxPollInterval)
select {
case <-time.After(pollInterval):
case <-ctx.Done():
+ common.Error("paddleocr.net poll: context done while waiting",
+ ctx.Err(),
+ zap.String("job_id", jobId),
+ zap.Int("attempt", attempt),
+ zap.String("last_state", state))
return nil, ctx.Err()
}
}
@@ -258,6 +450,10 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
return nil, fmt.Errorf("job done but jsonl url is empty")
}
+ common.Info("paddleocr.net result: downloading",
+ zap.String("job_id", jobId),
+ zap.String("jsonl_url", jsonlUrl))
+
resReq, err := http.NewRequestWithContext(ctx, "GET", jsonlUrl, nil)
if err != nil {
return nil, fmt.Errorf("failed to create request for jsonl: %w", err)
@@ -265,53 +461,71 @@ func (p *PaddleOCRModel) OCRFile(ctx context.Context, modelName *string, content
resResp, err := p.baseModel.httpClient.Do(resReq)
if err != nil {
+ common.Error("paddleocr.net result: download request failed",
+ err,
+ zap.String("job_id", jobId),
+ zap.String("jsonl_url", jsonlUrl))
return nil, fmt.Errorf("failed to download jsonl result: %w", err)
}
defer resResp.Body.Close()
if resResp.StatusCode != http.StatusOK {
+ common.Error("paddleocr.net result: non-200",
+ fmt.Errorf("status %d", resResp.StatusCode),
+ zap.String("job_id", jobId),
+ zap.String("jsonl_url", jsonlUrl),
+ zap.Int("status", resResp.StatusCode))
return nil, fmt.Errorf("failed to download jsonl, status: %d", resResp.StatusCode)
}
- var fullMarkdown strings.Builder
- scanner := bufio.NewScanner(resResp.Body)
- scanner.Buffer(make([]byte, 64*1024), 1024*1024)
-
- for scanner.Scan() {
- line := strings.TrimSpace(scanner.Text())
- if line == "" {
- continue
- }
-
- var lineData paddleJsonlLine
- if err := json.Unmarshal([]byte(line), &lineData); err != nil {
- continue
- }
-
- for _, layoutRes := range lineData.Result.LayoutParsingResults {
- fullMarkdown.WriteString(layoutRes.Markdown.Text)
- fullMarkdown.WriteString("\n\n")
- }
+ common.Info("paddleocr.net result: downloaded",
+ zap.String("job_id", jobId),
+ zap.String("jsonl_url", jsonlUrl))
- // Fallback to ocrResults for models like PP-OCRv6
- if len(lineData.Result.LayoutParsingResults) == 0 {
- for _, ocrRes := range lineData.Result.OcrResults {
- for _, text := range ocrRes.PrunedResult.RecTexts {
- text = strings.TrimSpace(text)
- if text != "" {
- fullMarkdown.WriteString(text)
- fullMarkdown.WriteString("\n")
- }
- }
- }
- }
+ rawBody, err := io.ReadAll(resResp.Body)
+ if err != nil {
+ return nil, fmt.Errorf("failed to read result body: %w", err)
}
- if err = scanner.Err(); err != nil {
- return nil, fmt.Errorf("error reading jsonl: %w", err)
+ var fullMarkdown strings.Builder
+ arrayParsed, scannedLines, skippedLines, emptyResultLines, contentLines, erroredLines, err := p.parseOCRResultBody(rawBody, &fullMarkdown)
+ if err != nil {
+ return nil, err
}
extractedText := strings.TrimSpace(fullMarkdown.String())
+ if extractedText == "" {
+ common.Warn("paddleocr.net result: parsed empty text",
+ zap.String("job_id", jobId),
+ zap.Bool("array_parsed", arrayParsed),
+ zap.Int("body_bytes", len(rawBody)),
+ zap.Int("scanned_lines", scannedLines),
+ zap.Int("skipped_lines", skippedLines),
+ zap.Int("errored_lines", erroredLines),
+ zap.Int("empty_result_lines", emptyResultLines),
+ zap.Int("content_lines", contentLines))
+ return nil, fmt.Errorf("paddleocr.net result: parsed empty text (scanned_lines=%d, skipped_lines=%d, errored_lines=%d, empty_result_lines=%d, content_lines=%d)",
+ scannedLines, skippedLines, erroredLines, emptyResultLines, contentLines)
+ }
+ common.Info("paddleocr.net result: parsed",
+ zap.String("job_id", jobId),
+ zap.Bool("array_parsed", arrayParsed),
+ zap.Int("body_bytes", len(rawBody)),
+ zap.Int("text_len", len(extractedText)),
+ zap.Int("scanned_lines", scannedLines),
+ zap.Int("skipped_lines", skippedLines),
+ zap.Int("errored_lines", erroredLines),
+ zap.Int("empty_result_lines", emptyResultLines),
+ zap.Int("content_lines", contentLines))
+ // OCR text can contain sensitive document content; keep any preview out of
+ // default (Info) logs and only surface it when debug logging is enabled.
+ preview := extractedText
+ if len(preview) > 200 {
+ preview = preview[:200]
+ }
+ common.Debug("paddleocr.net result: text preview",
+ zap.String("job_id", jobId),
+ zap.String("text_preview", preview))
return &OCRFileResponse{Text: &extractedText}, nil
}
diff --git a/internal/entity/models/paddleocr_local.go b/internal/entity/models/paddleocr_local.go
index 4a153db226..fb7719c879 100644
--- a/internal/entity/models/paddleocr_local.go
+++ b/internal/entity/models/paddleocr_local.go
@@ -24,8 +24,11 @@ import (
"fmt"
"io"
"net/http"
- "ragflow/internal/common"
"strings"
+
+ "ragflow/internal/common"
+
+ "go.uber.org/zap"
)
type PaddleOCRLocalModel struct {
@@ -146,30 +149,69 @@ func (p *PaddleOCRLocalModel) OCRFile(ctx context.Context, modelName *string, co
req.Header.Set("Authorization", auth)
}
+ algorithm := ""
+ if ocrConfig != nil {
+ algorithm = ocrConfig.Algorithm
+ }
+ common.Info("paddleocr local submit: sending",
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.String("model", *modelName),
+ zap.String("algorithm", algorithm),
+ zap.Int("content_bytes", len(content)))
+
resp, err := p.baseModel.httpClient.Do(req)
if err != nil {
+ common.Error("paddleocr local submit: request failed",
+ err,
+ zap.String("driver", p.Name()),
+ zap.String("url", url))
return nil, fmt.Errorf("failed to send request to local PaddleOCR: %w", err)
}
defer resp.Body.Close()
+ if resp.StatusCode != http.StatusOK {
+ errBody := readErrorBody(resp.Body)
+ common.Error("paddleocr local submit: non-200",
+ fmt.Errorf("status %d", resp.StatusCode),
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.Int("status", resp.StatusCode),
+ zap.String("body", errBody))
+ return nil, fmt.Errorf("local PaddleOCR failed with status %d: %s", resp.StatusCode, errBody)
+ }
+
respBody, err := io.ReadAll(resp.Body)
if err != nil {
return nil, fmt.Errorf("failed to read response body: %w", err)
}
- if resp.StatusCode != http.StatusOK {
- return nil, fmt.Errorf("local PaddleOCR failed with status %d: %s", resp.StatusCode, string(respBody))
- }
-
var ocrResp paddleLocalOCRResponse
if err := json.Unmarshal(respBody, &ocrResp); err != nil {
- return nil, fmt.Errorf("failed to parse local PaddleOCR response: %w, raw: %s", err, string(respBody))
+ common.Error("paddleocr local submit: parse failed",
+ err,
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.String("body", logBody(respBody)))
+ return nil, fmt.Errorf("failed to parse local PaddleOCR response: %w, raw: %s", err, logBody(respBody))
}
if ocrResp.ErrorCode != 0 {
+ common.Error("paddleocr local submit: task failed",
+ fmt.Errorf("errorCode %d", ocrResp.ErrorCode),
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.Int("error_code", ocrResp.ErrorCode),
+ zap.String("error_msg", ocrResp.ErrorMsg))
return nil, fmt.Errorf("local PaddleOCR task failed: %s (errorCode: %d)", ocrResp.ErrorMsg, ocrResp.ErrorCode)
}
+ common.Info("paddleocr local submit: ok",
+ zap.String("driver", p.Name()),
+ zap.String("url", url),
+ zap.Int("status", resp.StatusCode),
+ zap.String("log_id", ocrResp.LogId))
+
var fullMarkdown strings.Builder
for _, layoutRes := range ocrResp.Result.LayoutParsingResults {
if layoutRes.Markdown.Text != "" {
diff --git a/internal/entity/models/paddleocr_test.go b/internal/entity/models/paddleocr_test.go
new file mode 100644
index 0000000000..a36212b158
--- /dev/null
+++ b/internal/entity/models/paddleocr_test.go
@@ -0,0 +1,196 @@
+package models
+
+import (
+ "strings"
+ "testing"
+)
+
+// sampleArrayResult mirrors the actual response shape observed from the
+// paddle_ocr.net online service: a JSON array of page objects where each
+// element carries result.layoutParsingResults[].markdown.text plus
+// logId/errorCode/errorMsg at the top level.
+const sampleArrayResult = `[
+ {
+ "logId": "de52e6c7-225e-4b0e-9a6b-8b4f5c74a3c3",
+ "result": {
+ "layoutParsingResults": [
+ {
+ "prunedResult": {"page_count": null, "width": 998, "height": 1418},
+ "markdown": {"text": "## 第一章\n\n道可道,非常道。", "images": {}},
+ "outputImages": {"layout_det_res": "https://example.com/layout_0.jpg"},
+ "inputImage": "https://example.com/input_0.jpg"
+ }
+ ],
+ "dataInfo": {"numPages": 1, "type": "pdf"},
+ "preprocessedImages": ["https://example.com/preprocessed_0.jpg"]
+ },
+ "errorCode": 0,
+ "errorMsg": "Success"
+ },
+ {
+ "logId": "de52e6c7-225e-4b0e-9a6b-8b4f5c74a3c3",
+ "result": {
+ "layoutParsingResults": [
+ {
+ "prunedResult": {"page_count": null, "width": 998, "height": 1418},
+ "markdown": {"text": "第二章 天下皆知美之为美", "images": {}},
+ "outputImages": {},
+ "inputImage": "https://example.com/input_1.jpg"
+ }
+ ],
+ "dataInfo": {"numPages": 1, "type": "pdf"}
+ },
+ "errorCode": 0,
+ "errorMsg": "Success"
+ }
+]`
+
+func TestParseOCRResultBodyArray(t *testing.T) {
+ p := &PaddleOCRModel{}
+ var md strings.Builder
+ arrayParsed, scanned, skipped, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(sampleArrayResult), &md)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if !arrayParsed {
+ t.Fatalf("expected array parse, got jsonl fallback")
+ }
+ if scanned != 2 {
+ t.Errorf("scannedLines = %d, want 2", scanned)
+ }
+ if skipped != 0 {
+ t.Errorf("skippedLines = %d, want 0", skipped)
+ }
+ if emptyRes != 0 {
+ t.Errorf("emptyResultLines = %d, want 0", emptyRes)
+ }
+ if content != 2 {
+ t.Errorf("contentLines = %d, want 2", content)
+ }
+ if errored != 0 {
+ t.Errorf("erroredLines = %d, want 0", errored)
+ }
+ out := md.String()
+ if !strings.Contains(out, "道可道,非常道") {
+ t.Errorf("markdown missing first page text: %q", out)
+ }
+ if !strings.Contains(out, "天下皆知美之为美") {
+ t.Errorf("markdown missing second page text: %q", out)
+ }
+}
+
+func TestParseOCRResultBodyJSONL(t *testing.T) {
+ // Older payloads are one JSON object per line; the fallback path must
+ // handle them too.
+ jsonl := `{"logId":"a","result":{"layoutParsingResults":[{"markdown":{"text":"line one"}}]},"errorCode":0}
+{"logId":"b","result":{"layoutParsingResults":[{"markdown":{"text":"line two"}}]},"errorCode":0}`
+ p := &PaddleOCRModel{}
+ var md strings.Builder
+ arrayParsed, scanned, skipped, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(jsonl), &md)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if arrayParsed {
+ t.Fatalf("expected jsonl fallback, got array parse")
+ }
+ if scanned != 2 {
+ t.Errorf("scannedLines = %d, want 2", scanned)
+ }
+ if skipped != 0 {
+ t.Errorf("skippedLines = %d, want 0", skipped)
+ }
+ if emptyRes != 0 {
+ t.Errorf("emptyResultLines = %d, want 0", emptyRes)
+ }
+ if content != 2 {
+ t.Errorf("contentLines = %d, want 2", content)
+ }
+ if errored != 0 {
+ t.Errorf("erroredLines = %d, want 0", errored)
+ }
+ out := md.String()
+ if !strings.Contains(out, "line one") || !strings.Contains(out, "line two") {
+ t.Errorf("markdown missing jsonl text: %q", out)
+ }
+}
+
+func TestParseOCRResultBodyEmpty(t *testing.T) {
+ tests := []struct {
+ name string
+ body string
+ wantArr bool
+ wantEmpty int
+ }{
+ {"empty array", `[]`, true, 0},
+ {"entries without text", `[{"logId":"a","result":{"layoutParsingResults":[]},"errorCode":0}]`, true, 1},
+ {"garbage line", `not json`, false, 0},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ p := &PaddleOCRModel{}
+ var md strings.Builder
+ arrayParsed, _, _, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(tt.body), &md)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if errored != 0 {
+ t.Errorf("erroredLines = %d, want 0", errored)
+ }
+ if arrayParsed != tt.wantArr {
+ t.Errorf("arrayParsed = %v, want %v", arrayParsed, tt.wantArr)
+ }
+ if content != 0 {
+ t.Errorf("contentLines = %d, want 0", content)
+ }
+ // Structurally valid entries that decode but carry no text are
+ // counted as empty; unparseable content must never crash.
+ if emptyRes != tt.wantEmpty {
+ t.Errorf("emptyResultLines = %d, want %d", emptyRes, tt.wantEmpty)
+ }
+ if strings.TrimSpace(md.String()) != "" {
+ t.Errorf("expected empty markdown, got %q", md.String())
+ }
+ })
+ }
+}
+
+// TestParseOCRResultBodyErrored verifies that entries with a non-zero
+// errorCode are counted as errored (not silently counted as empty), produce no
+// text, and are handled identically on the array and jsonl paths.
+func TestParseOCRResultBodyErrored(t *testing.T) {
+ tests := []struct {
+ name string
+ body string
+ }{
+ {"array path", `[{"logId":"l1","result":{"layoutParsingResults":[{"markdown":{"text":"should not surface"}}]},"errorCode":1001,"errorMsg":"page failed"}]`},
+ {"jsonl path", `{"logId":"l1","result":{"layoutParsingResults":[{"markdown":{"text":"should not surface"}}]},"errorCode":1001,"errorMsg":"page failed"}`},
+ }
+ for _, tt := range tests {
+ t.Run(tt.name, func(t *testing.T) {
+ p := &PaddleOCRModel{}
+ var md strings.Builder
+ _, scanned, skipped, emptyRes, content, errored, err := p.parseOCRResultBody([]byte(tt.body), &md)
+ if err != nil {
+ t.Fatalf("unexpected error: %v", err)
+ }
+ if scanned != 1 {
+ t.Errorf("scannedLines = %d, want 1", scanned)
+ }
+ if skipped != 0 {
+ t.Errorf("skippedLines = %d, want 0", skipped)
+ }
+ if emptyRes != 0 {
+ t.Errorf("emptyResultLines = %d, want 0 (errored entry must not masquerade as empty)", emptyRes)
+ }
+ if content != 0 {
+ t.Errorf("contentLines = %d, want 0", content)
+ }
+ if errored != 1 {
+ t.Errorf("erroredLines = %d, want 1", errored)
+ }
+ if strings.TrimSpace(md.String()) != "" {
+ t.Errorf("errored entry must not contribute text, got %q", md.String())
+ }
+ })
+ }
+}
diff --git a/internal/ingestion/component/dispatch_model.go b/internal/ingestion/component/dispatch_model.go
index 5dded69815..f0789f656a 100644
--- a/internal/ingestion/component/dispatch_model.go
+++ b/internal/ingestion/component/dispatch_model.go
@@ -76,6 +76,63 @@ func defaultResolveTenantModelByType(ctx context.Context, db *gorm.DB, tenantID
return resolveModelConfig(ctx, db, tenantID, modelType, modelID)
}
+// resolveTenantOCRModelByProvider resolves the tenant's first active OCR
+// model under the named provider (e.g. "PaddleOCR"), mirroring Python's
+// get_first_provider_model_name(tenant_id, provider_name, LLMType.OCR).
+// It walks provider -> instances -> models and returns the first model whose
+// model_type includes the OCR bit, resolved through resolveModelConfigByID.
+var resolveTenantOCRModelByProvider = defaultResolveTenantOCRModelByProvider
+
+func defaultResolveTenantOCRModelByProvider(ctx context.Context, db *gorm.DB, tenantID string, providerName string) (modelModule.ModelDriver, string, *modelModule.APIConfig, int, error) {
+ providerDAO := dao.NewTenantModelProviderDAO()
+ provider, err := providerDAO.GetByTenantIDAndProviderName(ctx, db, tenantID, providerName)
+ if err != nil {
+ // Some OCR providers are registered under sibling names: the local
+ // "PaddleOCR" provider and the cloud "PaddleOCR.Net" provider expose
+ // the same OCR capability. Tolerate the alternate spelling before
+ // giving up so a tenant configured with either name resolves.
+ for _, alias := range paddleOCRProviderAliases() {
+ if alias == providerName {
+ continue
+ }
+ if p, aerr := providerDAO.GetByTenantIDAndProviderName(ctx, db, tenantID, alias); aerr == nil {
+ provider, err = p, nil
+ break
+ }
+ }
+ if err != nil {
+ return nil, "", nil, 0, fmt.Errorf("tenant %s has no %s provider: %w", tenantID, providerName, err)
+ }
+ }
+ instanceDAO := dao.NewTenantModelInstanceDAO()
+ instances, err := instanceDAO.GetAllInstancesByProviderID(ctx, db, provider.ID)
+ if err != nil {
+ return nil, "", nil, 0, err
+ }
+ modelDAO := dao.NewTenantModelDAO()
+ for _, instance := range instances {
+ models, err := modelDAO.GetModelsByInstanceID(ctx, db, instance.ID)
+ if err != nil {
+ return nil, "", nil, 0, err
+ }
+ for _, model := range models {
+ if model.Status != "active" || !entity.ModelType(model.ModelType).Has(entity.ModelTypeOCR) {
+ continue
+ }
+ return resolveModelConfigByID(ctx, db, tenantID, entity.ModelTypeOCR, model.ID)
+ }
+ }
+ return nil, "", nil, 0, fmt.Errorf("tenant %s has no active %s OCR model", tenantID, providerName)
+}
+
+// paddleOCRProviderAliases returns the registered provider names that expose
+// PaddleOCR OCR models: the local "PaddleOCR" provider and the cloud
+// "PaddleOCR.Net" provider. Both carry OCR-typed models and route through the
+// PaddleOCR PDF dispatch regardless of which spelling a tenant configured.
+func paddleOCRProviderAliases() []string {
+ return []string{"PaddleOCR", "PaddleOCR.Net"}
+}
+
func tenantModelIDByType(tenant *entity.Tenant, modelType entity.ModelType) string {
if tenant == nil {
return ""
diff --git a/internal/ingestion/component/parser_dispatch_test.go b/internal/ingestion/component/parser_dispatch_test.go
index f1f9badb43..900431aa77 100644
--- a/internal/ingestion/component/parser_dispatch_test.go
+++ b/internal/ingestion/component/parser_dispatch_test.go
@@ -29,7 +29,10 @@ import (
"archive/zip"
"bytes"
"context"
+ "encoding/base64"
+ "encoding/json"
"fmt"
+ "io"
"net/http"
"net/http/httptest"
"os"
@@ -723,7 +726,7 @@ func (d *mineruTestDriver) ShowTask(ctx context.Context, taskID string, apiConfi
return nil, fmt.Errorf("not implemented")
}
-func TestDispatch_PDFPaddleOCRMarkdown_UsesConfiguredBackend(t *testing.T) {
+func TestDispatch_PDFPaddleOCRMarkdown_UsesTenantModel(t *testing.T) {
withSSRFBypass(t)
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodPost || r.URL.Path != "/layout-parsing" {
@@ -739,18 +742,30 @@ func TestDispatch_PDFPaddleOCRMarkdown_UsesConfiguredBackend(t *testing.T) {
}))
defer server.Close()
+ // Mock resolveTenantOCRModelByProvider to return a PaddleOCR driver
+ // pointing at the test server.
+ origResolver := resolveTenantOCRModelByProvider
+ defer func() { resolveTenantOCRModelByProvider = origResolver }()
+ baseURL := server.URL
+ apiKey := "paddle-secret"
+ resolveTenantOCRModelByProvider = func(ctx context.Context, db *gorm.DB, tenantID string, providerName string) (models.ModelDriver, string, *models.APIConfig, int, error) {
+ if got, want := providerName, "PaddleOCR"; got != want {
+ t.Fatalf("providerName = %q, want %q", got, want)
+ }
+ return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, 0, nil
+ }
+
param := schema.ParserParam{}.Defaults()
setups := defaultSetups()
setups["pdf"]["parse_method"] = "PaddleOCR"
setups["pdf"]["output_format"] = "markdown"
- setups["pdf"]["paddleocr_base_url"] = server.URL
- setups["pdf"]["paddleocr_api_key"] = "paddle-secret"
c := &ParserComponent{Param: param, Setups: setups}
out, err := c.Invoke(t.Context(), nil, map[string]any{
"binary": []byte("%PDF-1.4"),
"file_type": "pdf",
"name": "sample.pdf",
+ "tenant_id": "test-tenant",
})
if err != nil {
t.Fatalf("Invoke: %v", err)
@@ -764,6 +779,297 @@ func TestDispatch_PDFPaddleOCRMarkdown_UsesConfiguredBackend(t *testing.T) {
}
}
+func TestDispatch_PDFPaddleOCR_NoTenantModel_HardErrors(t *testing.T) {
+ withSSRFBypass(t)
+ origResolver := resolveTenantOCRModelByProvider
+ defer func() { resolveTenantOCRModelByProvider = origResolver }()
+ resolveTenantOCRModelByProvider = func(ctx context.Context, db *gorm.DB, tenantID string, providerName string) (models.ModelDriver, string, *models.APIConfig, int, error) {
+ return nil, "", nil, 0, fmt.Errorf("no active PaddleOCR OCR model")
+ }
+
+ param := schema.ParserParam{}.Defaults()
+ setups := defaultSetups()
+ setups["pdf"]["parse_method"] = "paddleocr"
+ setups["pdf"]["output_format"] = "markdown"
+ c := &ParserComponent{Param: param, Setups: setups}
+
+ _, err := c.Invoke(t.Context(), nil, map[string]any{
+ "binary": []byte("%PDF-1.4"),
+ "file_type": "pdf",
+ "name": "sample.pdf",
+ "tenant_id": "test-tenant",
+ })
+ if err == nil || !strings.Contains(err.Error(), "parser: PaddleOCR model") {
+ t.Fatalf("Invoke error = %v, want PaddleOCR model error", err)
+ }
+}
+
+// TestDispatch_PDFPaddleOCR_BareModelUUID_UsesExactModel pins the routing of
+// a bare tenant model UUID in layout_recognizer — the value the web UI writes
+// when a user picks an OCR model for PDF parsing — to the PaddleOCR dispatch
+// path. The raw UUID carries no "@provider" hint in the string, so it must be
+// resolved first (mirroring Python's get_composite_model_name_by_id before
+// normalize_layout_recognizer). Previously the UUID fell through to the
+// image2text VLM path and failed with "cannot be used as image2text model"
+// for OCR-typed models such as the cloud "PaddleOCR.Net" provider's.
+func TestDispatch_PDFPaddleOCR_BareModelUUID_UsesExactModel(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost || r.URL.Path != "/layout-parsing" {
+ http.NotFound(w, r)
+ return
+ }
+ if got, want := r.Header.Get("Authorization"), "Bearer paddle-secret"; got != want {
+ t.Errorf("Authorization = %q, want %q", got, want)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"errorCode":0,"result":{"layoutParsingResults":[{"markdown":{"text":"# Cloud Paddle Title\n\nCloud body.\n"}}]}}`))
+ }))
+ defer server.Close()
+
+ origIsLayout := isPaddleOCRLayoutModelID
+ origResolve := resolvePaddleOCRModelForDispatch
+ defer func() {
+ isPaddleOCRLayoutModelID = origIsLayout
+ resolvePaddleOCRModelForDispatch = origResolve
+ }()
+
+ modelID := "d13ffec6c1e34b1abc30e540b692d83d"
+ isPaddleOCRLayoutModelID = func(ctx context.Context, db *gorm.DB, tenantID, layout string) bool {
+ if got, want := layout, modelID; got != want {
+ t.Fatalf("layout = %q, want %q", got, want)
+ }
+ return true
+ }
+ baseURL := server.URL
+ apiKey := "paddle-secret"
+ resolvePaddleOCRModelForDispatch = func(ctx context.Context, db *gorm.DB, tenantID, mid string) (models.ModelDriver, string, *models.APIConfig, error) {
+ if got, want := mid, modelID; got != want {
+ t.Fatalf("modelID = %q, want %q", got, want)
+ }
+ return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, nil
+ }
+
+ param := schema.ParserParam{}.Defaults()
+ setups := defaultSetups()
+ setups["pdf"]["layout_recognizer"] = modelID
+ setups["pdf"]["output_format"] = "markdown"
+ c := &ParserComponent{Param: param, Setups: setups}
+
+ out, err := c.Invoke(t.Context(), nil, map[string]any{
+ "binary": []byte("%PDF-1.4"),
+ "file_type": "pdf",
+ "name": "sample.pdf",
+ "tenant_id": "test-tenant",
+ })
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+ if got, want := out["output_format"], "markdown"; got != want {
+ t.Fatalf("output_format = %v, want %v", got, want)
+ }
+ md, ok := out["markdown"].(string)
+ if !ok || !strings.Contains(md, "Cloud Paddle Title") {
+ t.Fatalf("markdown payload = %#v, want Cloud Paddle Title content", out["markdown"])
+ }
+}
+
+// TestDispatch_PDFPaddleOCR_BareModelUUID_InParseMethod pins the routing of a
+// bare tenant model UUID carried in parse_method (with layout_recognizer
+// empty) to the PaddleOCR dispatch path. Previously only layout_recognizer
+// was probed for a UUID, so a UUID in parse_method fell through to the
+// image2text VLM path and failed with "cannot be used as image2text model".
+func TestDispatch_PDFPaddleOCR_BareModelUUID_InParseMethod(t *testing.T) {
+ withSSRFBypass(t)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if r.Method != http.MethodPost || r.URL.Path != "/layout-parsing" {
+ http.NotFound(w, r)
+ return
+ }
+ if got, want := r.Header.Get("Authorization"), "Bearer paddle-secret"; got != want {
+ t.Errorf("Authorization = %q, want %q", got, want)
+ return
+ }
+ w.Header().Set("Content-Type", "application/json")
+ _, _ = w.Write([]byte(`{"errorCode":0,"result":{"layoutParsingResults":[{"markdown":{"text":"# Cloud Paddle Title\n\nCloud body.\n"}}]}}`))
+ }))
+ defer server.Close()
+
+ origIsLayout := isPaddleOCRLayoutModelID
+ origResolve := resolvePaddleOCRModelForDispatch
+ defer func() {
+ isPaddleOCRLayoutModelID = origIsLayout
+ resolvePaddleOCRModelForDispatch = origResolve
+ }()
+
+ modelID := "d13ffec6c1e34b1abc30e540b692d83d"
+ isPaddleOCRLayoutModelID = func(ctx context.Context, db *gorm.DB, tenantID, selector string) bool {
+ if got, want := selector, modelID; got != want {
+ t.Fatalf("selector = %q, want %q", got, want)
+ }
+ return true
+ }
+ baseURL := server.URL
+ apiKey := "paddle-secret"
+ resolvePaddleOCRModelForDispatch = func(ctx context.Context, db *gorm.DB, tenantID, mid string) (models.ModelDriver, string, *models.APIConfig, error) {
+ if got, want := mid, modelID; got != want {
+ t.Fatalf("modelID = %q, want %q", got, want)
+ }
+ return &paddleocrTestDriver{}, "PaddleOCR-VL", &models.APIConfig{ApiKey: &apiKey, BaseURL: &baseURL}, nil
+ }
+
+ param := schema.ParserParam{}.Defaults()
+ setups := defaultSetups()
+ setups["pdf"]["parse_method"] = modelID
+ setups["pdf"]["output_format"] = "markdown"
+ c := &ParserComponent{Param: param, Setups: setups}
+
+ out, err := c.Invoke(t.Context(), nil, map[string]any{
+ "binary": []byte("%PDF-1.4"),
+ "file_type": "pdf",
+ "name": "sample.pdf",
+ "tenant_id": "test-tenant",
+ })
+ if err != nil {
+ t.Fatalf("Invoke: %v", err)
+ }
+ if got, want := out["output_format"], "markdown"; got != want {
+ t.Fatalf("output_format = %v, want %v", got, want)
+ }
+ md, ok := out["markdown"].(string)
+ if !ok || !strings.Contains(md, "Cloud Paddle Title") {
+ t.Fatalf("markdown payload = %#v, want Cloud Paddle Title content", out["markdown"])
+ }
+}
+
+// paddleocrTestDriver is a minimal ModelDriver mock whose Name() returns "paddleocr".
+type paddleocrTestDriver struct{}
+
+func (d *paddleocrTestDriver) NewInstance(baseURL map[string]string) models.ModelDriver { return d }
+func (d *paddleocrTestDriver) Name() string { return "paddleocr" }
+func (d *paddleocrTestDriver) ChatWithMessages(ctx context.Context, modelName string, messages []models.Message, apiConfig *models.APIConfig, chatModelConfig *models.ChatConfig, usage *common.ModelUsage) (*models.ChatResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) ChatStreamlyWithSender(ctx context.Context, modelName string, messages []models.Message, apiConfig *models.APIConfig, modelConfig *models.ChatConfig, usage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) Embed(ctx context.Context, modelName *string, request models.EmbedRequest, apiConfig *models.APIConfig, embeddingConfig *models.EmbeddingConfig, usage *common.ModelUsage) ([]models.EmbeddingData, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+
+func (d *paddleocrTestDriver) Rerank(ctx context.Context, modelName *string, request models.RerankRequest, apiConfig *models.APIConfig, rerankConfig *models.RerankConfig, usage *common.ModelUsage) (*models.RerankResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) TranscribeAudio(ctx context.Context, modelName *string, file *string, apiConfig *models.APIConfig, asrConfig *models.ASRConfig, usage *common.ModelUsage) (*models.ASRResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) TranscribeAudioWithSender(ctx context.Context, modelName *string, file *string, apiConfig *models.APIConfig, asrConfig *models.ASRConfig, usage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) AudioSpeech(ctx context.Context, modelName *string, audioContent *string, apiConfig *models.APIConfig, ttsConfig *models.TTSConfig, usage *common.ModelUsage) (*models.TTSResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) AudioSpeechWithSender(ctx context.Context, modelName *string, audioContent *string, apiConfig *models.APIConfig, ttsConfig *models.TTSConfig, usage *common.ModelUsage, sender func(*string, *string) error) error {
+ return fmt.Errorf("not implemented")
+}
+
+// OCRFile mimics the local PaddleOCRLocalModel protocol: a synchronous
+// JSON POST to {baseURL}/layout-parsing carrying the file as base64, with
+// Bearer auth when the API config provides a key.
+func (d *paddleocrTestDriver) OCRFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *models.APIConfig, ocrConfig *models.OCRConfig, usage *common.ModelUsage) (*models.OCRFileResponse, error) {
+ if apiConfig == nil || apiConfig.BaseURL == nil || *apiConfig.BaseURL == "" {
+ return nil, fmt.Errorf("missing base url")
+ }
+ endpoint := strings.TrimRight(*apiConfig.BaseURL, "/") + "/layout-parsing"
+ reqData := map[string]any{
+ "file": base64.StdEncoding.EncodeToString(content),
+ "fileType": 0,
+ }
+ if ocrConfig != nil && strings.TrimSpace(ocrConfig.Algorithm) != "" {
+ reqData["algorithm"] = ocrConfig.Algorithm
+ }
+ jsonData, err := json.Marshal(reqData)
+ if err != nil {
+ return nil, err
+ }
+ req, err := http.NewRequestWithContext(ctx, http.MethodPost, endpoint, bytes.NewBuffer(jsonData))
+ if err != nil {
+ return nil, err
+ }
+ req.Header.Set("Content-Type", "application/json")
+ if auth := models.BearerAuth(apiConfig); auth != "" {
+ req.Header.Set("Authorization", auth)
+ }
+ resp, err := http.DefaultClient.Do(req)
+ if err != nil {
+ return nil, err
+ }
+ defer resp.Body.Close()
+ body, err := io.ReadAll(resp.Body)
+ if err != nil {
+ return nil, err
+ }
+ if resp.StatusCode != http.StatusOK {
+ return nil, fmt.Errorf("status %d: %s", resp.StatusCode, string(body))
+ }
+ var ocrResp struct {
+ Result struct {
+ LayoutParsingResults []struct {
+ Markdown struct {
+ Text string `json:"text"`
+ } `json:"markdown"`
+ } `json:"layoutParsingResults"`
+ } `json:"result"`
+ }
+ if err := json.Unmarshal(body, &ocrResp); err != nil {
+ return nil, err
+ }
+ var md strings.Builder
+ for _, lr := range ocrResp.Result.LayoutParsingResults {
+ if lr.Markdown.Text != "" {
+ md.WriteString(lr.Markdown.Text)
+ md.WriteString("\n\n")
+ }
+ }
+ text := strings.TrimSpace(md.String())
+ return &models.OCRFileResponse{Text: &text}, nil
+}
+func (d *paddleocrTestDriver) ParseFile(ctx context.Context, modelName *string, content []byte, url *string, apiConfig *models.APIConfig, parseFileConfig *models.ParseFileConfig, usage *common.ModelUsage) (*models.ParseFileResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) ListModels(ctx context.Context, apiConfig *models.APIConfig) ([]models.ListModelResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) Balance(ctx context.Context, apiConfig *models.APIConfig) (map[string]interface{}, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) CheckConnection(ctx context.Context, apiConfig *models.APIConfig) error {
+ return fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) ListTasks(ctx context.Context, apiConfig *models.APIConfig) ([]models.ListTaskStatus, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+func (d *paddleocrTestDriver) ShowTask(ctx context.Context, taskID string, apiConfig *models.APIConfig) (*models.TaskResponse, error) {
+ return nil, fmt.Errorf("not implemented")
+}
+
+func TestIsPaddleOCRDriver(t *testing.T) {
+ for _, tc := range []struct {
+ name string
+ d models.ModelDriver
+ want bool
+ }{
+ {"local", &paddleocrTestDriver{}, true},
+ {"remote", &models.PaddleOCRModel{}, true},
+ {"dummy", &models.DummyModel{}, false},
+ } {
+ if got := isPaddleOCRDriver(tc.d); got != tc.want {
+ t.Errorf("isPaddleOCRDriver(%s) = %v, want %v", tc.name, got, tc.want)
+ }
+ }
+}
+
func TestDispatch_PDFDoclingMarkdown_UsesConfiguredBackend(t *testing.T) {
withSSRFBypass(t)
var requestCount int
diff --git a/internal/ingestion/component/pdf_vision_dispatch.go b/internal/ingestion/component/pdf_vision_dispatch.go
index f844990a0d..9dcf78fb28 100644
--- a/internal/ingestion/component/pdf_vision_dispatch.go
+++ b/internal/ingestion/component/pdf_vision_dispatch.go
@@ -15,11 +15,13 @@ import (
"sync"
"time"
+ "ragflow/internal/common"
"ragflow/internal/entity"
modelModule "ragflow/internal/entity/models"
"ragflow/internal/ingestion/component/schema"
"ragflow/internal/utility"
+ "go.uber.org/zap"
"gorm.io/gorm"
)
@@ -63,13 +65,17 @@ func maybeDispatchPDFVision(
method := getStringOr(setup, "parse_method", "")
layout := getStringOr(setup, "layout_recognizer", "")
+ tenantID := getStringOr(inputs, "tenant_id", "")
// MinerU dispatch: parse_method "mineru" or layout_recognizer "@MinerU"
layoutLower := strings.ToLower(strings.TrimSpace(layout))
if strings.EqualFold(strings.TrimSpace(method), "mineru") ||
strings.HasPrefix(layoutLower, "mineru") ||
strings.Contains(layoutLower, "@mineru") {
- tenantID := getStringOr(inputs, "tenant_id", "")
+ common.Info("pdf vision dispatch: MinerU branch matched",
+ zap.String("parse_method", method),
+ zap.String("layout_recognizer", layout),
+ zap.String("tenant_id", tenantID))
if tenantID == "" {
return parserDispatchResult{}, true,
fmt.Errorf("parser: MinerU requires tenant_id")
@@ -81,11 +87,54 @@ func maybeDispatchPDFVision(
return res, true, nil
}
+ // PaddleOCR dispatch: parse_method "paddleocr", a layout_recognizer whose
+ // provider/selectors name PaddleOCR, or a bare tenant model UUID (in
+ // either parse_method or layout_recognizer) that resolves to a PaddleOCR
+ // OCR model. A bare UUID carries no provider spelling in the string, so it
+ // is resolved first — mirroring Python's get_composite_model_name_by_id +
+ // normalize_layout_recognizer chain, which converts the raw model UUID
+ // into model@instance@provider before choosing the dispatch path.
+ isPaddleOCRMatch := strings.EqualFold(strings.TrimSpace(method), "paddleocr") ||
+ strings.HasPrefix(layoutLower, "paddleocr") ||
+ strings.Contains(layoutLower, "@paddleocr")
+ // The UUID may be placed in either parse_method or layout_recognizer;
+ // probe the non-empty one (layout takes precedence, then parse_method).
+ // The selector is used only for the UUID probe: a string-matched
+ // "paddleocr"/"@paddleocr" selector is a method name, not a model UUID,
+ // so it must never reach the model resolver. Named parse methods (e.g.
+ // "deepdoc") are likewise never probed as model UUIDs.
+ paddleOCRSelector := layout
+ if strings.TrimSpace(paddleOCRSelector) == "" {
+ paddleOCRSelector = method
+ }
+ isPaddleOCRByUUID := false
+ if !isPaddleOCRMatch && strings.TrimSpace(paddleOCRSelector) != "" &&
+ !isNamedPDFParseMethod(paddleOCRSelector) {
+ isPaddleOCRByUUID = isPaddleOCRLayoutModelID(ctx, db, tenantID, paddleOCRSelector)
+ }
+ if isPaddleOCRMatch || isPaddleOCRByUUID {
+ if tenantID == "" {
+ return parserDispatchResult{}, true,
+ fmt.Errorf("parser: PaddleOCR requires tenant_id")
+ }
+ // Only a resolved UUID may be passed to the model resolver; a string
+ // match keeps the empty modelID so the tenant's PaddleOCR model is
+ // resolved by provider instead.
+ dispatchModelID := ""
+ if isPaddleOCRByUUID {
+ dispatchModelID = paddleOCRSelector
+ }
+ res, err := dispatchPaddleOCRPdf(ctx, db, filename, binary, tenantID, setup, dispatchModelID)
+ if err != nil {
+ return parserDispatchResult{}, true, err
+ }
+ return res, true, nil
+ }
+
modelID, useVision := resolvePDFVisionModelID(setup)
if !useVision {
return parserDispatchResult{}, false, nil
}
- tenantID := getStringOr(inputs, "tenant_id", "")
if tenantID == "" {
return parserDispatchResult{}, true, fmt.Errorf(
`parser: pdf parse_method %q requires tenant_id to resolve VLM model`, modelID)
@@ -151,13 +200,126 @@ func dispatchMinerUPDF(
}
md := strings.Join(parts, "\n")
- outputFormat := getStringOr(setup, "output_format", "markdown")
+ // MinerU always returns rendered markdown text (md), regardless of
+ // the requested output_format; label the payload as markdown so the
+ // downstream chunker consumes it instead of a nil JSONResult.
+ if format := strings.TrimSpace(getStringOr(setup, "output_format", "markdown")); !strings.EqualFold(format, "markdown") {
+ common.Warn("mineru parser: output_format %q requested but backend only returns markdown; treating result as markdown",
+ zap.String("output_format", format))
+ }
return parserDispatchResult{
- OutputFormat: outputFormat,
+ OutputFormat: "markdown",
Markdown: md,
}, nil
}
+// resolvePaddleOCRModelForDispatch resolves the OCR model used by the
+// PaddleOCR PDF dispatch. modelID is the raw layout_recognizer value: a bare
+// tenant model UUID (no "@") selects that exact model regardless of provider
+// spelling ("PaddleOCR" or "PaddleOCR.Net"); a composite name or empty value
+// falls back to the tenant's first PaddleOCR OCR model, mirroring Python's
+// by_paddleocr which uses get_first_provider_model_name(tenant, "PaddleOCR").
+//
+// Known limitation: composite names such as "some-model@[email protected]"
+// (an explicit cloud selection) are not recognized here. Any value containing
+// "@" falls through to resolveTenantOCRModelByProvider("PaddleOCR"), which
+// returns the tenant's first active PaddleOCR OCR model — potentially the
+// local one — when both the local and the cloud (".Net") providers are
+// configured. The Python path behaves the same way; if exact cloud selection
+// is required, pass the model's tenant-model UUID instead.
+var resolvePaddleOCRModelForDispatch = defaultResolvePaddleOCRModelForDispatch
+
+func defaultResolvePaddleOCRModelForDispatch(ctx context.Context, db *gorm.DB, tenantID, modelID string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ if strings.TrimSpace(modelID) != "" && !strings.Contains(modelID, "@") {
+ driver, modelName, apiConfig, _, err := resolveModelConfigByID(ctx, db, tenantID, entity.ModelTypeOCR, modelID)
+ return driver, modelName, apiConfig, err
+ }
+ driver, modelName, apiConfig, _, err := resolveTenantOCRModelByProvider(ctx, db, tenantID, "PaddleOCR")
+ return driver, modelName, apiConfig, err
+}
+
+// dispatchPaddleOCRPdf submits a PDF to the tenant's PaddleOCR OCR model and
+// returns parsed sections. The resolved driver runs the protocol it knows:
+// the cloud "PaddleOCR.Net" driver submits a job and polls the
+// v2/ocr/jobs endpoint, the local "PaddleOCR" driver POSTs synchronously to
+// layout-parsing — both mirror the Python paddleocr_parser paths.
+func dispatchPaddleOCRPdf(
+ ctx context.Context,
+ db *gorm.DB,
+ filename string,
+ binary []byte,
+ tenantID string,
+ setup schema.ParserSetup,
+ modelID string,
+) (parserDispatchResult, error) {
+ driver, modelName, apiConfig, err := resolvePaddleOCRModelForDispatch(ctx, db, tenantID, modelID)
+ if err != nil {
+ return parserDispatchResult{}, fmt.Errorf("parser: PaddleOCR model: %w", err)
+ }
+ if !isPaddleOCRDriver(driver) {
+ return parserDispatchResult{}, fmt.Errorf(
+ "parser: PaddleOCR requires a PaddleOCR OCR model; found %q. Please add a PaddleOCR OCR model to your tenant", driver.Name())
+ }
+
+ baseURL := ""
+ if apiConfig.BaseURL != nil {
+ baseURL = *apiConfig.BaseURL
+ }
+ if baseURL == "" {
+ baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRBaseUrl))
+ }
+ if baseURL == "" {
+ baseURL = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAPIURL))
+ }
+ if baseURL == "" {
+ return parserDispatchResult{}, fmt.Errorf(
+ "parser: PaddleOCR requires a base url from the tenant PaddleOCR OCR model or PADDLEOCR_BASE_URL")
+ }
+
+ apiKey := ""
+ if apiConfig.ApiKey != nil {
+ apiKey = *apiConfig.ApiKey
+ }
+ algorithm := strings.TrimSpace(getStringOr(setup, "paddleocr_algorithm", ""))
+ if algorithm == "" {
+ algorithm = strings.TrimSpace(common.GetEnv(common.EnvPaddleOCRAlgorithm))
+ }
+ if algorithm == "" {
+ algorithm = "PaddleOCR-VL"
+ }
+ ocrAPIConfig := &modelModule.APIConfig{BaseURL: &baseURL}
+ if apiKey != "" {
+ ocrAPIConfig.ApiKey = &apiKey
+ }
+
+ resp, err := driver.OCRFile(ctx, &modelName, binary, &filename, ocrAPIConfig, &modelModule.OCRConfig{
+ Algorithm: algorithm,
+ }, nil)
+ if err != nil {
+ return parserDispatchResult{}, fmt.Errorf("parser: PaddleOCR OCRFile: %w", err)
+ }
+ if resp == nil || resp.Text == nil {
+ return parserDispatchResult{}, fmt.Errorf("parser: PaddleOCR returned empty text")
+ }
+ if strings.TrimSpace(*resp.Text) == "" {
+ return parserDispatchResult{}, fmt.Errorf("parser: PaddleOCR returned empty text")
+ }
+
+ // PaddleOCR backends always return rendered markdown text
+ // (OCRFile.Text), regardless of the requested output_format. The
+ // payload MUST be labelled markdown so the downstream chunker
+ // consumes the text; a non-markdown setup value only means this
+ // backend cannot produce the requested layout format.
+ if format := strings.TrimSpace(getStringOr(setup, "output_format", "markdown")); !strings.EqualFold(format, "markdown") {
+ common.Warn("paddleocr parser: output_format %q requested but backend only returns markdown; treating result as markdown",
+ zap.String("output_format", format))
+ }
+ return parserDispatchResult{
+ OutputFormat: "markdown",
+ Markdown: *resp.Text,
+ }, nil
+}
+
// resolveMinerUBaseURL extracts the resolved base URL from a model driver.
func resolveMinerUBaseURL(driver modelModule.ModelDriver, apiConfig *modelModule.APIConfig) (string, error) {
type baseURLGetter interface {
@@ -563,6 +725,40 @@ func isMinerUDriver(driver modelModule.ModelDriver) bool {
return false
}
+// isPaddleOCRDriver reports whether the model driver is a PaddleOCR variant
+// (remote paddle_ocr.net or local paddleocr).
+func isPaddleOCRDriver(driver modelModule.ModelDriver) bool {
+ switch strings.ToLower(driver.Name()) {
+ case "paddleocr", "paddle_ocr.net":
+ return true
+ }
+ return false
+}
+
+// isPaddleOCRLayoutModelID reports whether layout — a bare tenant model UUID
+// with no "model@instance@provider" composite hint — resolves to an active
+// OCR model driven by a PaddleOCR provider (local "PaddleOCR" or cloud
+// "PaddleOCR.Net"). The web UI stores the tenant model UUID directly in
+// layout_recognizer, so the raw value carries no provider spelling; this
+// mirrors Python's get_composite_model_name_by_id resolution of the same
+// UUID before the PaddleOCR path is chosen.
+var isPaddleOCRLayoutModelID = defaultIsPaddleOCRLayoutModelID
+
+func defaultIsPaddleOCRLayoutModelID(ctx context.Context, db *gorm.DB, tenantID, layout string) bool {
+ layout = strings.TrimSpace(layout)
+ if db == nil {
+ return false
+ }
+ if layout == "" || strings.Contains(layout, "@") {
+ return false
+ }
+ driver, _, _, err := defaultResolvePaddleOCRModelForDispatch(ctx, db, tenantID, layout)
+ if err != nil {
+ return false
+ }
+ return isPaddleOCRDriver(driver)
+}
+
// maybeDispatchPDFVisionEnhancement mirrors Python's
// enhance_media_sections_with_vision for PDF
// After the normal PDF parser produces JSON items, this function
diff --git a/internal/ingestion/component/pdf_vision_dispatch_test.go b/internal/ingestion/component/pdf_vision_dispatch_test.go
index 3cbf5e22d0..78a0b2b9e0 100644
--- a/internal/ingestion/component/pdf_vision_dispatch_test.go
+++ b/internal/ingestion/component/pdf_vision_dispatch_test.go
@@ -19,14 +19,95 @@ import (
"context"
"testing"
+ "ragflow/internal/common"
"ragflow/internal/dao"
"ragflow/internal/entity"
modelModule "ragflow/internal/entity/models"
+ "ragflow/internal/ingestion/component/schema"
"ragflow/internal/utility"
"gorm.io/gorm"
)
+// paddleOCRFakeDriver embeds the ModelDriver interface and only implements
+// the OCRFile method needed by dispatchPaddleOCRPdf.
+type paddleOCRFakeDriver struct {
+ modelModule.ModelDriver
+ text string
+}
+
+func (d *paddleOCRFakeDriver) Name() string { return "PaddleOCR" }
+
+func (d *paddleOCRFakeDriver) OCRFile(_ context.Context, _ *string, _ []byte, _ *string, _ *modelModule.APIConfig, _ *modelModule.OCRConfig, _ *common.ModelUsage) (*modelModule.OCRFileResponse, error) {
+ return &modelModule.OCRFileResponse{Text: &d.text}, nil
+}
+
+// TestDispatchPaddleOCRPdfLabelsPayloadAsMarkdown guards against the
+// format-mismatch bug: PaddleOCR backends always return markdown text via
+// OCRFile.Text, so the dispatch result MUST be labelled OutputFormat
+// "markdown" regardless of what setup["output_format"] says (the pdf default
+// is "json"). If the setup value leaked into OutputFormat, buildParserOutputs
+// would emit a nil "json" payload and the downstream TokenChunker would
+// consume an empty JSONResult -> "completed with 0 chunks".
+func TestDispatchPaddleOCRPdfLabelsPayloadAsMarkdown(t *testing.T) {
+ orig := resolvePaddleOCRModelForDispatch
+ t.Cleanup(func() { resolvePaddleOCRModelForDispatch = orig })
+
+ md := "## 《道德经》全文及翻译\n\n道可道,非常道。"
+ resolvePaddleOCRModelForDispatch = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ baseURL := "http://localhost:9380"
+ return &paddleOCRFakeDriver{text: md}, "ocr-model", &modelModule.APIConfig{BaseURL: &baseURL}, nil
+ }
+
+ // The real run had output_format=json in the pdf setup; the payload must
+ // still be labelled markdown because that is what the backend produced.
+ res, err := dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", schema.ParserSetup{"output_format": "json"}, "some-uuid")
+ if err != nil {
+ t.Fatalf("dispatchPaddleOCRPdf: %v", err)
+ }
+ if res.OutputFormat != "markdown" {
+ t.Errorf("OutputFormat = %q, want markdown", res.OutputFormat)
+ }
+ if res.Markdown != md {
+ t.Errorf("Markdown = %q, want %q", res.Markdown, md)
+ }
+
+ // Default setup (no output_format key) must behave identically.
+ res, err = dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", nil, "some-uuid")
+ if err != nil {
+ t.Fatalf("dispatchPaddleOCRPdf (default setup): %v", err)
+ }
+ if res.OutputFormat != "markdown" {
+ t.Errorf("OutputFormat = %q, want markdown", res.OutputFormat)
+ }
+ if res.Markdown != md {
+ t.Errorf("Markdown = %q, want %q", res.Markdown, md)
+ }
+}
+
+// TestDispatchPaddleOCRPdfEmptyTextFails guards against the silent-empty
+// result: OCRFileResponse.Text is a *string that stays non-nil even when the
+// backend produced zero text, so the old nil-only guard let an empty payload
+// through and the pipeline emitted a "completed with 0 chunks" document.
+// Empty text must surface as an explicit error instead.
+func TestDispatchPaddleOCRPdfEmptyTextFails(t *testing.T) {
+ orig := resolvePaddleOCRModelForDispatch
+ t.Cleanup(func() { resolvePaddleOCRModelForDispatch = orig })
+
+ resolvePaddleOCRModelForDispatch = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ baseURL := "http://localhost:9380"
+ return &paddleOCRFakeDriver{text: ""}, "ocr-model", &modelModule.APIConfig{BaseURL: &baseURL}, nil
+ }
+
+ res, err := dispatchPaddleOCRPdf(t.Context(), dao.DB, "test.pdf", []byte("%PDF-1.4"), "tenant", nil, "some-uuid")
+ if err == nil {
+ t.Fatalf("dispatchPaddleOCRPdf with empty text: expected error, got result %+v", res)
+ }
+ if res.OutputFormat != "" || res.Markdown != "" {
+ t.Errorf("expected zero-value result on error, got %+v", res)
+ }
+}
+
func TestMaybeDispatchPDFVisionEnhancementForwardsDatasetLanguage(t *testing.T) {
origResolver := resolveTenantModelByType
origInvoker := visionChatInvoker