Skip to content

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

fix(parser): use selected tenant SoMark and OpenDataLoader instances (#21112)

Xinliang Zhengab85152d0fdd
diff --git a/docs/guides/agent/ingestion_pipeline/configure_parser_component.md b/docs/guides/agent/ingestion_pipeline/configure_parser_component.md
index 7205fbf106..eb5f332d14 100644
--- a/docs/guides/agent/ingestion_pipeline/configure_parser_component.md
+++ b/docs/guides/agent/ingestion_pipeline/configure_parser_component.md
@@ -57,6 +57,42 @@ while retaining table recognition:
}
```
+### Tenant SoMark and OpenDataLoader models
+
+The Go Parser can use an enrolled **SoMark** or **OpenDataLoader** OCR model
+by tenant-model ID or by `model@instance@provider` reference, for example:
+
+```json
+{
+ "pdf": {
+ "parse_method": "SoMark-model@production@SoMark",
+ "output_format": "json"
+ }
+}
+```
+
+`layout_recognizer` also accepts a reference. Selection follows the existing PDF
+model rule: a model reference in `parse_method` takes precedence; otherwise the
+reference in `layout_recognizer` is used.
+
+The selected model and instance must be active, OCR-capable and owned by the
+current tenant/provider. The Parser resolves the instance endpoint and key on
+each execution, so credential changes apply without resaving the pipeline.
+SoMark supports ordinary instance key/base-URL storage; OpenDataLoader also
+supports its provider-card JSON configuration, including nested `api_key` maps.
+
+For an explicit reference, a missing endpoint, inaccessible/disabled model or
+invalid instance configuration is an error. The Parser never substitutes another
+instance, a global environment credential or a generic VLM. An empty optional
+key remains empty. Transport SSRF protections still apply.
+
+SoMark's non-secret per-model format/feature settings supply defaults; explicit
+pipeline options override them. An explicit `false` is retained. Resolved
+credentials stay on the per-run parser object, not in saved pipeline settings.
+
+Bare engine names (`somark`, `opendataloader`) continue to use their existing
+parser-specific setup/environment configuration.
+
### Other file types
For image files:
diff --git a/internal/ingestion/component/pdf_parser_model.go b/internal/ingestion/component/pdf_parser_model.go
new file mode 100644
index 0000000000..502473450b
--- /dev/null
+++ b/internal/ingestion/component/pdf_parser_model.go
@@ -0,0 +1,214 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package component
+
+import (
+ "context"
+ "encoding/json"
+ "fmt"
+ "maps"
+ "strings"
+
+ "ragflow/internal/dao"
+ "ragflow/internal/entity"
+ modelModule "ragflow/internal/entity/models"
+ "ragflow/internal/ingestion/component/schema"
+ "ragflow/internal/parser/parser"
+
+ "gorm.io/gorm"
+)
+
+// tenantPDFParserEngine identifies parser backends, not generic ModelDrivers.
+// Their tenant records are resolved directly through the existing DAOs because
+// SoMark and OpenDataLoader do not have a chat/OCR driver in conf/models.
+func tenantPDFParserEngine(provider string) string {
+ switch strings.ToLower(strings.TrimSpace(provider)) {
+ case "somark":
+ return "somark"
+ case "opendataloader":
+ return "opendataloader"
+ }
+ return ""
+}
+
+// maybeDispatchTenantPDFParser resolves an explicit model at execution time.
+// Bare engine names retain setup/environment-based parsing. A selected tenant
+// reference never falls back to another instance, environment settings or VLM.
+func maybeDispatchTenantPDFParser(ctx context.Context, db *gorm.DB, filename string, binary []byte, tenantID string, setup schema.ParserSetup) (parser.ParseResult, bool, error) {
+ selector, selected := resolvePDFVisionModelID(setup)
+ if !selected {
+ return parser.ParseResult{}, false, nil
+ }
+ composite := strings.Contains(selector, "@")
+ engine := ""
+ modelName, instanceName, providerName := "", "", ""
+ if composite {
+ var err error
+ modelName, instanceName, providerName, err = parseCompositeModelName(selector)
+ engine = tenantPDFParserEngine(providerName)
+ if engine == "" {
+ return parser.ParseResult{}, false, nil
+ }
+ if err != nil || strings.TrimSpace(modelName) == "" || strings.TrimSpace(instanceName) == "" {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: invalid %s model reference", engine)
+ }
+ if engine == "somark" {
+ providerName = "SoMark"
+ } else {
+ providerName = "OpenDataLoader"
+ }
+ }
+ if db == nil {
+ if engine == "" {
+ return parser.ParseResult{}, false, nil
+ }
+ return parser.ParseResult{}, true, fmt.Errorf("parser: %s model resolution requires a database", engine)
+ }
+
+ modelDAO := dao.NewTenantModelDAO()
+ providerDAO := dao.NewTenantModelProviderDAO()
+ instanceDAO := dao.NewTenantModelInstanceDAO()
+ var model *entity.TenantModel
+ var provider *entity.TenantModelProvider
+ var instance *entity.TenantModelInstance
+ var err error
+ if composite {
+ provider, err = providerDAO.GetByTenantIDAndProviderName(ctx, db, tenantID, providerName)
+ if err == nil {
+ instance, err = instanceDAO.GetByProviderIDAndInstanceName(ctx, db, provider.ID, instanceName)
+ }
+ if err == nil {
+ model, err = modelDAO.GetByProviderIDAndInstanceIDAndModelTypeAndModelName(
+ ctx, db, provider.ID, instance.ID, int(entity.ModelTypeOCR), modelName)
+ }
+ } else {
+ model, err = modelDAO.GetByID(ctx, db, selector)
+ if err == nil {
+ provider, err = providerDAO.GetByID(ctx, db, model.ProviderID)
+ }
+ if err == nil {
+ engine = tenantPDFParserEngine(provider.ProviderName)
+ if engine == "" {
+ return parser.ParseResult{}, false, nil
+ }
+ instance, err = instanceDAO.GetByID(ctx, db, model.InstanceID)
+ }
+ }
+ if err != nil {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: tenant PDF model lookup: %w", err)
+ }
+ if tenantID == "" || provider.TenantID != tenantID || instance.ProviderID != provider.ID {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: selected PDF model does not belong to this tenant/provider")
+ }
+ if model.Status != "active" || instance.Status != "active" || !entity.ModelType(model.ModelType).Has(entity.ModelTypeOCR) {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: selected PDF model and instance must be active OCR configuration")
+ }
+
+ baseURL, apiKey, err := tenantPDFParserCredentials(engine, instance)
+ if err != nil {
+ return parser.ParseResult{}, true, err
+ }
+ options := maps.Clone(setup)
+ if engine == "somark" && strings.TrimSpace(model.Extra) != "" {
+ var extra map[string]any
+ if err := json.Unmarshal([]byte(model.Extra), &extra); err != nil {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: invalid selected PDF model options")
+ }
+ // Only non-secret parser options belong here. Explicit pipeline options
+ // win; credentials and endpoint always come from the selected instance.
+ for _, key := range []string{
+ "somark_image_format", "somark_formula_format", "somark_table_format", "somark_cs_format",
+ "somark_enable_text_cross_page", "somark_enable_table_cross_page",
+ "somark_enable_title_level_recognition", "somark_enable_inline_image",
+ "somark_enable_table_image", "somark_enable_image_understanding", "somark_keep_header_footer",
+ } {
+ if value, ok := extra[key]; ok {
+ if _, explicit := options[key]; !explicit {
+ _, valid := value.(bool)
+ if strings.HasSuffix(key, "_format") {
+ _, valid = value.(string)
+ }
+ if !valid {
+ return parser.ParseResult{}, true, fmt.Errorf("parser: invalid selected PDF model option %s", key)
+ }
+ options[key] = value
+ }
+ }
+ }
+ }
+ pdf := parser.NewPDFParser()
+ pdf.ConfigureFromSetup(options)
+ pdf.ParseMethod = engine
+ pdf.UseTenantModelConfig = true
+ if engine == "somark" {
+ pdf.SoMarkBaseURL, pdf.SoMarkAPIKey = baseURL, apiKey
+ } else {
+ pdf.OpenDataLoaderAPIServer, pdf.OpenDataLoaderAPIKey = baseURL, apiKey
+ }
+ result := pdf.ParseWithResult(ctx, filename, binary)
+ if result.Err != nil {
+ return result, true, result.Err
+ }
+ if result.File == nil {
+ result.File = make(map[string]any)
+ }
+ result.File["parse_method"] = selector
+ return result, true, nil
+}
+
+// tenantPDFParserCredentials accepts instance-card JSON and ordinary key/base
+// URL storage. Missing optional keys stay empty, never borrowing a global key.
+func tenantPDFParserCredentials(engine string, instance *entity.TenantModelInstance) (string, string, error) {
+ var extra struct {
+ BaseURL string `json:"base_url"`
+ }
+ if strings.TrimSpace(instance.Extra) != "" {
+ if err := json.Unmarshal([]byte(instance.Extra), &extra); err != nil {
+ return "", "", fmt.Errorf("parser: invalid selected PDF instance settings")
+ }
+ }
+ baseURL := strings.TrimSpace(extra.BaseURL)
+ key := instance.APIKey
+ if strings.HasPrefix(strings.TrimSpace(key), "{") {
+ var config map[string]any
+ if err := json.Unmarshal([]byte(key), &config); err != nil {
+ return "", "", fmt.Errorf("parser: invalid selected PDF instance credentials")
+ }
+ if nested, ok := config["api_key"].(map[string]any); ok {
+ config = nested
+ }
+ for _, field := range []string{
+ engine + "_apiserver", engine + "_base_url", strings.ToUpper(engine) + "_APISERVER",
+ strings.ToUpper(engine) + "_BASE_URL", "base_url", engine + "_api_key",
+ strings.ToUpper(engine) + "_API_KEY", "api_key",
+ } {
+ if value, ok := config[field]; ok && value != nil {
+ if _, ok := value.(string); !ok {
+ return "", "", fmt.Errorf("parser: invalid selected PDF instance credential field %s", field)
+ }
+ }
+ }
+ if baseURL == "" {
+ baseURL = modelModule.ProviderJSONConfigValue(key,
+ engine+"_apiserver", engine+"_base_url", strings.ToUpper(engine)+"_APISERVER",
+ strings.ToUpper(engine)+"_BASE_URL", "base_url")
+ }
+ key = modelModule.ProviderJSONConfigValue(key, engine+"_api_key", strings.ToUpper(engine)+"_API_KEY", "api_key")
+ }
+ if baseURL == "" {
+ return "", "", fmt.Errorf("parser: selected %s instance requires an endpoint", engine)
+ }
+ return baseURL, strings.TrimSpace(key), nil
+}
diff --git a/internal/ingestion/component/pdf_parser_model_test.go b/internal/ingestion/component/pdf_parser_model_test.go
new file mode 100644
index 0000000000..5a36076c48
--- /dev/null
+++ b/internal/ingestion/component/pdf_parser_model_test.go
@@ -0,0 +1,631 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package component
+
+import (
+ "context"
+ "encoding/json"
+ "errors"
+ "fmt"
+ "net/http"
+ "net/http/httptest"
+ "reflect"
+ "strings"
+ "sync/atomic"
+ "testing"
+
+ "ragflow/internal/common"
+ doctype "ragflow/internal/deepdoc/parser/type"
+ "ragflow/internal/entity"
+ modelModule "ragflow/internal/entity/models"
+ "ragflow/internal/ingestion/component/schema"
+ "ragflow/internal/utility"
+
+ "github.com/glebarez/sqlite"
+ "gorm.io/gorm"
+ "gorm.io/gorm/logger"
+)
+
+type pdfParserRequest struct {
+ Path string
+ Authorization string
+ Key string
+ Formats string
+ Features string
+}
+
+// tenantPDFParserFixture uses real DAO records and local parser HTTP protocols,
+// with a separate decoy instance that must never be selected.
+func tenantPDFParserFixture(t *testing.T, provider string) (*gorm.DB, string, chan pdfParserRequest) {
+ t.Helper()
+ withSSRFBypass(t)
+ requests := make(chan pdfParserRequest, 8)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if strings.Contains(r.Header.Get("Content-Type"), "multipart") {
+ if err := r.ParseMultipartForm(1 << 20); err != nil {
+ t.Errorf("multipart: %v", err)
+ http.Error(w, "invalid fixture request", http.StatusBadRequest)
+ return
+ }
+ defer r.MultipartForm.RemoveAll()
+ } else {
+ _ = r.ParseForm()
+ }
+ requests <- pdfParserRequest{
+ Path: r.URL.Path, Authorization: r.Header.Get("Authorization"),
+ Key: r.FormValue("api_key"), Formats: r.FormValue("element_formats"),
+ Features: r.FormValue("feature_config"),
+ }
+ w.Header().Set("Content-Type", "application/json")
+ switch r.URL.Path {
+ case "/parse/async":
+ _, _ = w.Write([]byte(`{"code":0,"data":{"task_id":"local-task"}}`))
+ case "/parse/async_check":
+ _, _ = w.Write([]byte(`{"code":0,"data":{"status":"SUCCESS","result":{"outputs":{"json":{"pages":[{"blocks":[{"type":"text","content":"Selected instance B."}]}]}}}}}`))
+ case "/file_parse":
+ _, _ = w.Write([]byte(`{"json_doc":{"type":"text","content":"Selected instance B."}}`))
+ default:
+ t.Errorf("unexpected parser route %s", r.URL.Path)
+ http.Error(w, "unexpected fixture route", http.StatusNotFound)
+ }
+ }))
+ t.Cleanup(server.Close)
+ decoy := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ t.Errorf("another instance was selected: %s", r.URL.Path)
+ http.Error(w, "wrong instance", http.StatusBadRequest)
+ }))
+ t.Cleanup(decoy.Close)
+
+ db, err := gorm.Open(sqlite.Open(":memory:"), &gorm.Config{Logger: logger.Default.LogMode(logger.Silent)})
+ if err != nil {
+ t.Fatal(err)
+ }
+ sqlDB, err := db.DB()
+ if err != nil {
+ t.Fatal(err)
+ }
+ sqlDB.SetMaxOpenConns(1)
+ t.Cleanup(func() { _ = sqlDB.Close() })
+ if err := db.AutoMigrate(&entity.TenantModelProvider{}, &entity.TenantModelInstance{}, &entity.TenantModel{}); err != nil {
+ t.Fatal(err)
+ }
+ if err := db.Create(&entity.TenantModelProvider{ID: "provider", ProviderName: provider, TenantID: "tenant-1"}).Error; err != nil {
+ t.Fatal(err)
+ }
+ for _, inst := range []struct{ id, url string }{{"a", decoy.URL}, {"b", server.URL}} {
+ key := "fixture-key-" + inst.id
+ extra := fmt.Sprintf(`{"base_url":%q}`, inst.url)
+ if provider == "OpenDataLoader" {
+ key = fmt.Sprintf(`{"api_key":{"opendataloader_apiserver":%q,"opendataloader_api_key":%q}}`, inst.url, key)
+ extra = "{}"
+ }
+ for _, row := range []any{
+ &entity.TenantModelInstance{ID: "instance-" + inst.id, ProviderID: "provider", InstanceName: inst.id, APIKey: key, Status: "active", Extra: extra},
+ &entity.TenantModel{ID: "model-" + inst.id, ProviderID: "provider", InstanceID: "instance-" + inst.id,
+ ModelName: provider + "-model", ModelType: int(entity.ModelTypeOCR), Status: "active",
+ Extra: `{"somark_image_format":"base64","somark_enable_inline_image":false}`},
+ } {
+ if err := db.Create(row).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ }
+ return db, provider + "-model@b@" + provider, requests
+}
+
+// TestTenantPDFParserModelSelection prevents UUID and composite references from
+// being ignored, substituted with another instance or sent to generic VLM chat.
+func TestTenantPDFParserModelSelection(t *testing.T) {
+ originalRenderer := pdfVisionPageRenderer
+ pdfVisionPageRenderer = func([]byte, [][]int) ([]pdfVisionPage, error) {
+ return nil, fmt.Errorf("unexpected generic VLM path")
+ }
+ t.Cleanup(func() { pdfVisionPageRenderer = originalRenderer })
+ for _, provider := range []string{"SoMark", "OpenDataLoader"} {
+ for _, field := range []string{"parse_method", "layout_recognizer"} {
+ for _, form := range []string{"id", "composite"} {
+ t.Run(provider+"/"+field+"/"+form, func(t *testing.T) {
+ db, composite, requests := tenantPDFParserFixture(t, provider)
+ ref := composite
+ if form == "id" {
+ ref = "model-b"
+ }
+ setup := schema.ParserSetup{"parse_method": "deepdoc", "output_format": "json", field: ref}
+ before, _ := json.Marshal(setup)
+ res, handled, err := maybeDispatchPDFVision(t.Context(), db, utility.FileTypePDF,
+ "sample.pdf", []byte("%PDF local protocol fixture"), map[string]any{"tenant_id": "tenant-1"},
+ map[string]schema.ParserSetup{"pdf": setup})
+ if err != nil || !handled || res.Err != nil {
+ t.Fatalf("selected parser was not used: handled=%v err=%v result=%v", handled, err, res.Err)
+ }
+ body, _ := json.Marshal(res.JSON)
+ if !strings.Contains(string(body), "Selected instance B.") {
+ t.Fatalf("selected parser output missing: %s", body)
+ }
+ after, _ := json.Marshal(setup)
+ if !reflect.DeepEqual(before, after) {
+ t.Fatal("runtime credentials/options mutated saved setup")
+ }
+ if len(requests) == 0 {
+ t.Fatal("selected parser never received an HTTP request")
+ }
+ for len(requests) > 0 {
+ request := <-requests
+ if provider == "SoMark" && request.Key != "fixture-key-b" {
+ t.Fatalf("wrong SoMark instance credentials")
+ }
+ if provider == "OpenDataLoader" && request.Authorization != "Bearer fixture-key-b" {
+ t.Fatalf("wrong OpenDataLoader instance credentials")
+ }
+ }
+ })
+ }
+ }
+ }
+}
+
+// TestTenantPDFParserRejectsInvalidSelection checks real stored records; a bad
+// explicit selection must fail without an HTTP request or a different model.
+func TestTenantPDFParserRejectsInvalidSelection(t *testing.T) {
+ for _, provider := range []string{"SoMark", "OpenDataLoader"} {
+ for _, tc := range []struct {
+ name string
+ mutate func(*gorm.DB) error
+ tenant string
+ }{
+ {"wrong-tenant", nil, "tenant-2"},
+ {"missing-tenant", nil, ""},
+ {"disabled-model", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModel{}).Where("id = ?", "model-b").Update("status", "inactive").Error
+ }, "tenant-1"},
+ {"wrong-type", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModel{}).Where("id = ?", "model-b").Update("model_type", int(entity.ModelTypeImage2Text)).Error
+ }, "tenant-1"},
+ {"disabled-instance", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Update("status", "inactive").Error
+ }, "tenant-1"},
+ {"missing-instance", func(db *gorm.DB) error {
+ return db.Where("id = ?", "instance-b").Delete(&entity.TenantModelInstance{}).Error
+ }, "tenant-1"},
+ {"wrong-provider", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Update("provider_id", "other-provider").Error
+ }, "tenant-1"},
+ {"missing-endpoint", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Updates(map[string]any{"extra": "{}", "api_key": ""}).Error
+ }, "tenant-1"},
+ {"malformed-instance", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Update("extra", "{").Error
+ }, "tenant-1"},
+ {"malformed-key", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Update("api_key", "{").Error
+ }, "tenant-1"},
+ {"wrong-key-type", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Update("api_key", `{"api_key":false}`).Error
+ }, "tenant-1"},
+ {"malformed-model-options", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModel{}).Where("id = ?", "model-b").Update("extra", "{").Error
+ }, "tenant-1"},
+ {"invalid-option-type", func(db *gorm.DB) error {
+ return db.Model(&entity.TenantModel{}).Where("id = ?", "model-b").Update("extra", `{"somark_enable_inline_image":"false"}`).Error
+ }, "tenant-1"},
+ } {
+ if provider == "OpenDataLoader" && (tc.name == "malformed-model-options" || tc.name == "invalid-option-type") {
+ continue // OpenDataLoader has no SoMark-specific model options.
+ }
+ for _, form := range []string{"id", "composite"} {
+ t.Run(provider+"/"+tc.name+"/"+form, func(t *testing.T) {
+ db, ref, requests := tenantPDFParserFixture(t, provider)
+ if tc.mutate != nil {
+ if err := tc.mutate(db); err != nil {
+ t.Fatal(err)
+ }
+ }
+ if form == "id" {
+ ref = "model-b"
+ }
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"),
+ tc.tenant, schema.ParserSetup{"parse_method": ref})
+ if !handled || err == nil {
+ t.Fatalf("bad selection did not fail: handled=%v err=%v", handled, err)
+ }
+ if len(requests) != 0 {
+ t.Fatal("invalid selection sent a parser request")
+ }
+ })
+ }
+ }
+ }
+}
+
+// TestTenantPDFParserMissingModelIDIsHandled keeps a well-formed but absent
+// explicit reference terminal, rather than treating it as an unselected parser.
+func TestTenantPDFParserMissingModelIDIsHandled(t *testing.T) {
+ const missingModelID = "7f4e7a5ce0fa4abda74cd99b6a2f89c1"
+ for _, provider := range []string{"SoMark", "OpenDataLoader"} {
+ for _, field := range []string{"parse_method", "layout_recognizer"} {
+ t.Run(provider+"/"+field, func(t *testing.T) {
+ db, _, requests := tenantPDFParserFixture(t, provider)
+ setup := schema.ParserSetup{"parse_method": "deepdoc", "output_format": "json", field: missingModelID}
+ before, err := json.Marshal(setup)
+ if err != nil {
+ t.Fatal(err)
+ }
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("%PDF fixture"),
+ "tenant-1", setup)
+ if !handled {
+ t.Error("missing explicit model was left for another parser")
+ }
+ if !errors.Is(err, gorm.ErrRecordNotFound) || !strings.Contains(err.Error(), "tenant PDF model") {
+ t.Errorf("missing explicit model must return a PDF model lookup error, got %v", err)
+ }
+ if len(requests) != 0 {
+ t.Error("missing explicit model sent a parser request")
+ }
+ after, err := json.Marshal(setup)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(before, after) {
+ t.Error("failed lookup mutated the saved setup")
+ }
+ })
+ }
+ }
+}
+
+// TestTenantPDFParserMissingModelIDInvokeDoesNotFallback exercises the real
+// component with reloaded canvas params. Missing selections must neither reach
+// another parser nor rewrite the persisted or in-memory setup.
+func TestTenantPDFParserMissingModelIDInvokeDoesNotFallback(t *testing.T) {
+ const missingModelID = "7f4e7a5ce0fa4abda74cd99b6a2f89c1"
+ for _, tc := range []struct {
+ field string
+ method string
+ }{
+ {"parse_method", "deepdoc"},
+ {"layout_recognizer", "deepdoc"},
+ {"layout_recognizer", "somark"},
+ {"layout_recognizer", "opendataloader"},
+ } {
+ t.Run(tc.field+"/"+tc.method, func(t *testing.T) {
+ db, _, requests := tenantPDFParserFixture(t, "SoMark")
+ var httpCalls, genericCalls atomic.Int64
+ fallback := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ httpCalls.Add(1)
+ w.Header().Set("Content-Type", "application/json")
+ w.WriteHeader(http.StatusServiceUnavailable)
+ _, _ = w.Write([]byte(`{"code":1,"message":"unexpected parser fallback"}`))
+ }))
+ t.Cleanup(fallback.Close)
+ t.Setenv(common.EnvSOMarkBaseUrl, fallback.URL+"/environment/somark")
+ t.Setenv(common.EnvSOMarkAPIKey, "environment-key-must-not-be-used")
+ t.Setenv(common.EnvOpenDataLoaderAPIServer, fallback.URL+"/environment/opendataloader")
+ t.Setenv(common.EnvOpenDataLoaderAPIKey, "environment-key-must-not-be-used")
+
+ originalRenderer, originalResolver := pdfVisionPageRenderer, pdfVisionModelResolver
+ originalPromptLoader, originalInvoker := pdfVisionPromptLoader, pdfVisionChatInvoker
+ originalAnalyzerFactory := doctype.NativeDocAnalyzerFactory
+ t.Cleanup(func() {
+ pdfVisionPageRenderer, pdfVisionModelResolver = originalRenderer, originalResolver
+ pdfVisionPromptLoader, pdfVisionChatInvoker = originalPromptLoader, originalInvoker
+ doctype.SetNativeDocAnalyzerFactory(originalAnalyzerFactory)
+ })
+ var renderCalls, resolveCalls, promptCalls, chatCalls int
+ // Let an erroneous VLM detour reach chat without native rendering
+ // or a real model; the sentinel error cannot satisfy the lookup check.
+ pdfVisionPageRenderer = func([]byte, [][]int) ([]pdfVisionPage, error) {
+ renderCalls++
+ return []pdfVisionPage{{PageNumber: 1, WidthPts: 100, HeightPts: 200, ImageURL: "data:image/png;base64,fixture"}}, nil
+ }
+ pdfVisionModelResolver = func(context.Context, *gorm.DB, string, string) (modelModule.ModelDriver, string, *modelModule.APIConfig, error) {
+ resolveCalls++
+ return nil, "unexpected-vlm", nil, nil
+ }
+ pdfVisionPromptLoader = func(string) (string, error) {
+ promptCalls++
+ return "Transcribe page {{ page }}.", nil
+ }
+ pdfVisionChatInvoker = func(context.Context, modelModule.ModelDriver, string, []modelModule.Message, *modelModule.APIConfig) (*modelModule.ChatResponse, error) {
+ chatCalls++
+ return nil, errors.New("unexpected generic VLM fallback")
+ }
+ doctype.SetNativeDocAnalyzerFactory(func() (doctype.DocAnalyzer, bool) {
+ genericCalls.Add(1)
+ return nil, false
+ })
+
+ setup := map[string]any{
+ "parse_method": tc.method, "output_format": "json", tc.field: missingModelID,
+ "somark_base_url": fallback.URL + "/setup/somark", "somark_api_key": "setup-key-must-not-be-used",
+ "opendataloader_apiserver": fallback.URL + "/setup/opendataloader",
+ "opendataloader_api_key": "setup-key-must-not-be-used",
+ }
+ if err := db.AutoMigrate(&entity.UserCanvas{}); err != nil {
+ t.Fatal(err)
+ }
+ canvas := entity.UserCanvas{
+ ID: "parser-canvas", UserID: "tenant-1", CanvasCategory: "dataflow_canvas",
+ DSL: entity.JSONMap{"components": map[string]any{
+ "Parser:fixture": map[string]any{"obj": map[string]any{
+ "component_name": "Parser", "params": map[string]any{"pdf": setup},
+ }},
+ }},
+ }
+ if err := db.Create(&canvas).Error; err != nil {
+ t.Fatal(err)
+ }
+ var reloaded entity.UserCanvas
+ if err := db.Where("id = ?", canvas.ID).Take(&reloaded).Error; err != nil {
+ t.Fatal(err)
+ }
+ saved, err := json.Marshal(reloaded.DSL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ node := reloaded.DSL["components"].(map[string]any)["Parser:fixture"].(map[string]any)
+ params := node["obj"].(map[string]any)["params"].(map[string]any)
+ component, err := NewParserComponent(params)
+ if err != nil {
+ t.Fatal(err)
+ }
+ parserComponent := component.(*ParserComponent)
+ runtimeSetup, err := json.Marshal(parserComponent.setups)
+ if err != nil {
+ t.Fatal(err)
+ }
+ out, err := component.Invoke(t.Context(), db, map[string]any{
+ "binary": []byte("%PDF synthetic fixture"), "file_type": "pdf",
+ "name": "sample.pdf", "tenant_id": "tenant-1",
+ })
+ if !errors.Is(err, gorm.ErrRecordNotFound) || !strings.Contains(err.Error(), "tenant PDF model") {
+ t.Errorf("Invoke must report the missing PDF model, got %v", err)
+ }
+ if out != nil {
+ t.Error("missing explicit model produced parser output")
+ }
+ if renderCalls != 0 || resolveCalls != 0 || promptCalls != 0 || chatCalls != 0 || genericCalls.Load() != 0 {
+ t.Errorf("missing explicit model entered fallback: render=%d resolve=%d prompt=%d chat=%d generic=%d",
+ renderCalls, resolveCalls, promptCalls, chatCalls, genericCalls.Load())
+ }
+ if httpCalls.Load() != 0 || len(requests) != 0 {
+ t.Errorf("missing explicit model sent HTTP requests: fallback=%d tenant=%d", httpCalls.Load(), len(requests))
+ }
+ after, err := json.Marshal(reloaded.DSL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(saved, after) {
+ t.Error("failed invocation mutated reloaded canvas params")
+ }
+ after, err = json.Marshal(parserComponent.setups)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(runtimeSetup, after) {
+ t.Error("failed invocation mutated the component setup")
+ }
+ var stored entity.UserCanvas
+ if err := db.Where("id = ?", canvas.ID).Take(&stored).Error; err != nil {
+ t.Fatal(err)
+ }
+ after, err = json.Marshal(stored.DSL)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !reflect.DeepEqual(saved, after) {
+ t.Error("failed invocation changed the persisted canvas")
+ }
+ })
+ }
+}
+
+// TestTenantPDFParserEnvironmentIsolation protects optional anonymous keys and
+// explicit false model options from unrelated global environment configuration.
+func TestTenantPDFParserEnvironmentIsolation(t *testing.T) {
+ t.Setenv(common.EnvSOMarkAPIKey, "global-secret-must-not-be-used")
+ t.Setenv(common.EnvSOMarkEnableInlineImage, "true")
+ t.Setenv(common.EnvOpenDataLoaderAPIKey, "global-secret-must-not-be-used")
+ for _, provider := range []string{"SoMark", "OpenDataLoader"} {
+ t.Run(provider, func(t *testing.T) {
+ db, ref, requests := tenantPDFParserFixture(t, provider)
+ var instance entity.TenantModelInstance
+ if err := db.Where("id = ?", "instance-b").Take(&instance).Error; err != nil {
+ t.Fatal(err)
+ }
+ update := map[string]any{"api_key": ""}
+ if provider == "OpenDataLoader" {
+ // Move this instance's URL to ordinary base_url storage, while
+ // intentionally leaving its optional credential empty.
+ var stored struct {
+ APIKey struct {
+ Endpoint string `json:"opendataloader_apiserver"`
+ } `json:"api_key"`
+ }
+ if err := json.Unmarshal([]byte(instance.APIKey), &stored); err != nil {
+ t.Fatal(err)
+ }
+ update["extra"] = fmt.Sprintf(`{"base_url":%q}`, stored.APIKey.Endpoint)
+ }
+ if err := db.Model(&instance).Updates(update).Error; err != nil {
+ t.Fatal(err)
+ }
+ res, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"),
+ "tenant-1", schema.ParserSetup{"parse_method": ref})
+ if !handled || err != nil || res.Err != nil {
+ t.Fatalf("anonymous selected instance: %v", err)
+ }
+ for len(requests) > 0 {
+ request := <-requests
+ if request.Key != "" || request.Authorization != "" {
+ t.Fatal("selected anonymous instance borrowed global credentials")
+ }
+ if request.Path == "/parse/async" && !strings.Contains(request.Features, `"enable_inline_image":false`) {
+ t.Fatalf("environment overrode explicit false: %s", request.Features)
+ }
+ }
+ })
+ }
+}
+
+// TestTenantPDFParserKeepsSSRFGuard proves explicit instance selection does not
+// bypass the repository's production transport policy.
+func TestTenantPDFParserKeepsSSRFGuard(t *testing.T) {
+ db, ref, requests := tenantPDFParserFixture(t, "SoMark")
+ common.AllowAnyHostForTest = false
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"),
+ "tenant-1", schema.ParserSetup{"parse_method": ref})
+ if !handled || err == nil || !strings.Contains(err.Error(), "non-public address") || len(requests) != 0 {
+ t.Fatalf("SSRF policy not preserved: handled=%v err=%v", handled, err)
+ }
+}
+
+// TestTenantPDFParserConfigRoundTripAndRotation exercises normal component
+// construction and output after a persisted reference is reloaded. Credentials
+// are looked up again on each invocation, not cached or written into params.
+func TestTenantPDFParserConfigRoundTripAndRotation(t *testing.T) {
+ for _, provider := range []string{"SoMark", "OpenDataLoader"} {
+ t.Run(provider, func(t *testing.T) {
+ db, ref, requests := tenantPDFParserFixture(t, provider)
+ params := map[string]any{"pdf": map[string]any{"parse_method": ref}}
+ saved, _ := json.Marshal(params)
+ var reloaded map[string]any
+ if err := json.Unmarshal(saved, &reloaded); err != nil {
+ t.Fatal(err)
+ }
+ component, err := NewParserComponent(reloaded)
+ if err != nil {
+ t.Fatal(err)
+ }
+ for _, key := range []string{"fixture-key-b", "rotated-fixture-key"} {
+ if key == "rotated-fixture-key" {
+ var instance entity.TenantModelInstance
+ if err := db.Where("id = ?", "instance-b").Take(&instance).Error; err != nil {
+ t.Fatal(err)
+ }
+ baseURL, _, err := tenantPDFParserCredentials(tenantPDFParserEngine(provider), &instance)
+ if err != nil {
+ t.Fatal(err)
+ }
+ if err := db.Model(&instance).Updates(map[string]any{
+ "api_key": key, "extra": fmt.Sprintf(`{"base_url":%q}`, baseURL),
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ }
+ out, err := component.Invoke(t.Context(), db, map[string]any{
+ "binary": []byte("%PDF local fixture"), "file_type": "pdf",
+ "name": "sample.pdf", "tenant_id": "tenant-1",
+ })
+ if err != nil {
+ t.Fatal(err)
+ }
+ body, _ := json.Marshal(out)
+ if !strings.Contains(string(body), "Selected instance B.") || strings.Contains(string(body), key) {
+ t.Fatalf("component output missing text or exposing credentials: %s", body)
+ }
+ if len(requests) == 0 {
+ t.Fatal("missing runtime parser request")
+ }
+ for len(requests) > 0 {
+ request := <-requests
+ if provider == "SoMark" && request.Key != key ||
+ provider == "OpenDataLoader" && request.Authorization != "Bearer "+key {
+ t.Fatal("runtime used stale or wrong credentials")
+ }
+ }
+ }
+ after, _ := json.Marshal(reloaded)
+ if !reflect.DeepEqual(saved, after) {
+ t.Fatal("component mutated persisted configuration")
+ }
+ })
+ }
+}
+
+// TestTenantPDFParserBareEnginesAndOtherModels leaves existing named engines
+// and other providers to their current dispatch paths.
+func TestTenantPDFParserBareEnginesAndOtherModels(t *testing.T) {
+ for _, selector := range []string{"", "somark", "OpenDataLoader", "deepdoc", "plain_text", "vision@b@OpenAI"} {
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), nil, "sample.pdf", []byte("pdf"),
+ "tenant-1", schema.ParserSetup{"parse_method": selector})
+ if handled || err != nil {
+ t.Fatalf("unrelated/bare selector %q intercepted: %v", selector, err)
+ }
+ }
+ for _, selector := range []string{"SoMark-model@b@SoMark", "OpenDataLoader-model@b@OpenDataLoader", "@b@SoMark"} {
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), nil, "sample.pdf", []byte("pdf"),
+ "tenant-1", schema.ParserSetup{"parse_method": selector})
+ if !handled || err == nil {
+ t.Fatalf("explicit selection %q fell back without database: %v", selector, err)
+ }
+ }
+}
+
+// TestTenantPDFParserSelectorPrecedence shares the existing model-selector rule:
+// a model in parse_method wins, otherwise layout_recognizer supplies the model.
+func TestTenantPDFParserSelectorPrecedence(t *testing.T) {
+ db, ref, requests := tenantPDFParserFixture(t, "SoMark")
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"),
+ "tenant-1", schema.ParserSetup{"parse_method": ref, "layout_recognizer": "model-a"})
+ if !handled || err != nil || len(requests) != 2 {
+ t.Fatalf("parse_method model did not take precedence: handled=%v err=%v", handled, err)
+ }
+}
+
+// TestTenantPDFParserExplicitOptions keeps non-secret overrides while ignoring
+// pipeline endpoint/key values in favor of the explicitly selected instance.
+func TestTenantPDFParserExplicitOptions(t *testing.T) {
+ db, ref, requests := tenantPDFParserFixture(t, "SoMark")
+ setup := schema.ParserSetup{
+ "parse_method": ref, "somark_image_format": "none", "somark_enable_inline_image": true,
+ "somark_base_url": "https://not-the-selected-instance.invalid", "somark_api_key": "not-the-selected-key",
+ }
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"), "tenant-1", setup)
+ if !handled || err != nil {
+ t.Fatal(err)
+ }
+ request := <-requests
+ if request.Key != "fixture-key-b" || !strings.Contains(request.Formats, `"image":"none"`) ||
+ !strings.Contains(request.Features, `"enable_inline_image":true`) {
+ t.Fatalf("instance credentials / pipeline option precedence mismatch: %#v", request)
+ }
+}
+
+// TestTenantPDFParserFailureDoesNotFallback returns the selected backend's
+// failure rather than silently trying another instance or a VLM.
+func TestTenantPDFParserFailureDoesNotFallback(t *testing.T) {
+ for _, provider := range []string{"SoMark", "OpenDataLoader"} {
+ t.Run(provider, func(t *testing.T) {
+ db, ref, otherRequests := tenantPDFParserFixture(t, provider)
+ calls := make(chan struct{}, 2)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ calls <- struct{}{}
+ w.WriteHeader(http.StatusServiceUnavailable)
+ _, _ = w.Write([]byte(`{"code":1,"message":"fixture backend unavailable"}`))
+ }))
+ defer server.Close()
+ if err := db.Model(&entity.TenantModelInstance{}).Where("id = ?", "instance-b").Updates(map[string]any{
+ "extra": fmt.Sprintf(`{"base_url":%q}`, server.URL), "api_key": "fixture-key-b",
+ }).Error; err != nil {
+ t.Fatal(err)
+ }
+ _, handled, err := maybeDispatchTenantPDFParser(t.Context(), db, "sample.pdf", []byte("pdf"), "tenant-1",
+ schema.ParserSetup{"parse_method": ref})
+ if !handled || err == nil || len(calls) != 1 || len(otherRequests) != 0 {
+ t.Fatalf("selected backend failure fell back: handled=%v err=%v", handled, err)
+ }
+ })
+ }
+}
diff --git a/internal/ingestion/component/pdf_vision_dispatch.go b/internal/ingestion/component/pdf_vision_dispatch.go
index 8125ecf213..1f146dd179 100644
--- a/internal/ingestion/component/pdf_vision_dispatch.go
+++ b/internal/ingestion/component/pdf_vision_dispatch.go
@@ -60,6 +60,9 @@ var (
pdfVisionChatInvoker = defaultPDFVisionChatInvoker
)
+// maybeDispatchPDFVision handles selected tenant PDF/OCR parsers before generic
+// VLM parsing. A handled result, including an error, must not fall back to the
+// ordinary PDF parser.
func maybeDispatchPDFVision(
ctx context.Context,
db *gorm.DB,
@@ -82,6 +85,10 @@ func maybeDispatchPDFVision(
tenantID := getStringOr(inputs, "tenant_id", "")
layoutLower := strings.ToLower(strings.TrimSpace(layout))
+ if result, handled, err := maybeDispatchTenantPDFParser(ctx, db, filename, binary, tenantID, setup); handled {
+ return result, true, err
+ }
+
monkeySelector := layout
if strings.TrimSpace(monkeySelector) == "" {
monkeySelector = method
diff --git a/internal/ingestion/pipeline/parser_tenant_reference_test.go b/internal/ingestion/pipeline/parser_tenant_reference_test.go
new file mode 100644
index 0000000000..2cc2fcae00
--- /dev/null
+++ b/internal/ingestion/pipeline/parser_tenant_reference_test.go
@@ -0,0 +1,53 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package pipeline
+
+import (
+ "encoding/json"
+ "testing"
+
+ "ragflow/internal/ingestion/component"
+)
+
+// TestTenantPDFParserReferenceCleaning retains a reference, not resolved
+// credentials, through the existing parser_config cleaning/save/load boundary.
+func TestTenantPDFParserReferenceCleaning(t *testing.T) {
+ for _, ref := range []string{"model-b", "SoMark-model@b@SoMark", "OpenDataLoader-model@b@OpenDataLoader"} {
+ for _, field := range []string{"parse_method", "layout_recognizer"} {
+ t.Run(ref+"/"+field, func(t *testing.T) {
+ cleaned := CleanComponentParams(generalDSL(t), map[string]any{
+ "Parser:HipSignsRhyme": map[string]any{
+ "pdf": map[string]any{"parse_method": "deepdoc", field: ref},
+ },
+ })
+ saved, err := json.Marshal(cleaned)
+ if err != nil {
+ t.Fatal(err)
+ }
+ var reloaded map[string]any
+ if err := json.Unmarshal(saved, &reloaded); err != nil {
+ t.Fatal(err)
+ }
+ params := reloaded["Parser:HipSignsRhyme"].(map[string]any)
+ if got := params["pdf"].(map[string]any)[field]; got != ref {
+ t.Fatalf("saved reference = %v, want %s", got, ref)
+ }
+ if _, err := component.NewParserComponent(params); err != nil {
+ t.Fatalf("reloaded component: %v", err)
+ }
+ })
+ }
+ }
+}
diff --git a/internal/parser/parser/pdf_parser_common.go b/internal/parser/parser/pdf_parser_common.go
index 3155d9b62f..be6bcc81f0 100644
--- a/internal/parser/parser/pdf_parser_common.go
+++ b/internal/parser/parser/pdf_parser_common.go
@@ -70,6 +70,10 @@ type PDFParser struct {
EnableMultiColumn bool
OutputFormat string
ParseMethod string
+ // UseTenantModelConfig disables global endpoint/credential/option fallback
+ // for explicitly resolved SoMark/OpenDataLoader instances. Runtime-only;
+ // ConfigureFromSetup does not accept this flag from persisted settings.
+ UseTenantModelConfig bool
// Pages restricts parsing to these 1-indexed inclusive page ranges.
// nil/empty means parse all pages. Populated by ConfigureFromSetup from
// the filetype setup map. DeepDOC reads it through its ParserConfig, and
diff --git a/internal/parser/parser/pdf_parser_opendataloader.go b/internal/parser/parser/pdf_parser_opendataloader.go
index 8caa1a1d87..7227752873 100644
--- a/internal/parser/parser/pdf_parser_opendataloader.go
+++ b/internal/parser/parser/pdf_parser_opendataloader.go
@@ -12,19 +12,21 @@ import (
"strings"
)
+// parsePDFWithOpenDataLoader normalizes the selected server's JSON or Markdown
+// output. Explicit tenant configuration never borrows a global endpoint or key.
func parsePDFWithOpenDataLoader(ctx context.Context, filename string, data []byte, parser *PDFParser) ParseResult {
if len(data) == 0 {
return emptyPDFResult(filename)
}
baseURL := strings.TrimSpace(parser.OpenDataLoaderAPIServer)
- if baseURL == "" {
+ if baseURL == "" && !parser.UseTenantModelConfig {
baseURL = strings.TrimSpace(common.GetEnv(common.EnvOpenDataLoaderAPIServer))
}
if baseURL == "" {
return ParseResult{Err: fmt.Errorf("parser: OpenDataLoader requires opendataloader_apiserver or OPENDATALOADER_APISERVER")}
}
apiKey := strings.TrimSpace(parser.OpenDataLoaderAPIKey)
- if apiKey == "" {
+ if apiKey == "" && !parser.UseTenantModelConfig {
apiKey = strings.TrimSpace(common.GetEnv(common.EnvOpenDataLoaderAPIKey))
}
diff --git a/internal/parser/parser/pdf_parser_somark.go b/internal/parser/parser/pdf_parser_somark.go
index 363badbd72..c78c0755f8 100644
--- a/internal/parser/parser/pdf_parser_somark.go
+++ b/internal/parser/parser/pdf_parser_somark.go
@@ -13,19 +13,21 @@ import (
"strings"
)
+// parsePDFWithSoMark submits a PDF and converts the task result into parser
+// output. Explicit tenant configuration never borrows a global endpoint or key.
func parsePDFWithSoMark(filename string, data []byte, parser *PDFParser) ParseResult {
if len(data) == 0 {
return emptyPDFResult(filename)
}
baseURL := strings.TrimSpace(parser.SoMarkBaseURL)
- if baseURL == "" {
+ if baseURL == "" && !parser.UseTenantModelConfig {
baseURL = strings.TrimSpace(common.GetEnv(common.EnvSOMarkBaseUrl))
}
if baseURL == "" {
return ParseResult{Err: fmt.Errorf("parser: SoMark requires somark_base_url or SOMARK_BASE_URL")}
}
apiKey := parser.SoMarkAPIKey
- if strings.TrimSpace(apiKey) == "" {
+ if strings.TrimSpace(apiKey) == "" && !parser.UseTenantModelConfig {
apiKey = strings.TrimSpace(common.GetEnv(common.EnvSOMarkAPIKey))
}
taskID, err := soMarkSubmit(strings.TrimRight(baseURL, "/"), filename, data, parser, apiKey)
@@ -43,6 +45,8 @@ func parsePDFWithSoMark(filename string, data []byte, parser *PDFParser) ParseRe
return pdfItemsToResult(filename, items, parser.OutputFormat, pageCount)
}
+// soMarkSubmit uploads a PDF with the supplied credentials and parser options,
+// applying environment defaults only when no tenant instance was selected.
func soMarkSubmit(baseURL, filename string, data []byte, parser *PDFParser, apiKey string) (string, error) {
var body bytes.Buffer
writer := multipart.NewWriter(&body)
@@ -55,19 +59,19 @@ func soMarkSubmit(baseURL, filename string, data []byte, parser *PDFParser, apiK
}
_ = writer.WriteField("output_formats", "json")
elementFormats, _ := json.Marshal(map[string]any{
- "image": envOrDefault(common.EnvSOMarkImageFormat, parser.SoMarkImageFormat, "url"),
- "formula": envOrDefault(common.EnvSOMarkFormulaFormat, parser.SoMarkFormulaFormat, "latex"),
- "table": envOrDefault(common.EnvSOMarkTableFormat, parser.SoMarkTableFormat, "html"),
- "cs": envOrDefault(common.EnvSOMarkCSFormat, parser.SoMarkCSFormat, "image"),
+ "image": parser.envOrDefault(common.EnvSOMarkImageFormat, parser.SoMarkImageFormat, "url"),
+ "formula": parser.envOrDefault(common.EnvSOMarkFormulaFormat, parser.SoMarkFormulaFormat, "latex"),
+ "table": parser.envOrDefault(common.EnvSOMarkTableFormat, parser.SoMarkTableFormat, "html"),
+ "cs": parser.envOrDefault(common.EnvSOMarkCSFormat, parser.SoMarkCSFormat, "image"),
})
featureConfig, _ := json.Marshal(map[string]any{
- "enable_text_cross_page": envOrBool(common.EnvSOMarkEnableTextCrossPage, parser.SoMarkEnableTextCrossPage),
- "enable_table_cross_page": envOrBool(common.EnvSOMarkEnableTableCrossPage, parser.SoMarkEnableTableCrossPage),
- "enable_title_level_recognition": envOrBool(common.EnvSOMarkEnableTitleLevelRecognition, parser.SoMarkEnableTitleLevelRecognition),
- "enable_inline_image": envOrBool(common.EnvSOMarkEnableInlineImage, parser.SoMarkEnableInlineImage),
- "enable_table_image": envOrBool(common.EnvSOMarkEnableTableImage, parser.SoMarkEnableTableImage),
- "enable_image_understanding": envOrBool(common.EnvSOMarkEnableImageUnderstanding, parser.SoMarkEnableImageUnderstanding),
- "keep_header_footer": envOrBool(common.EnvSOMarkKeepHeaderFooter, parser.SoMarkKeepHeaderFooter),
+ "enable_text_cross_page": parser.envOrBool(common.EnvSOMarkEnableTextCrossPage, parser.SoMarkEnableTextCrossPage),
+ "enable_table_cross_page": parser.envOrBool(common.EnvSOMarkEnableTableCrossPage, parser.SoMarkEnableTableCrossPage),
+ "enable_title_level_recognition": parser.envOrBool(common.EnvSOMarkEnableTitleLevelRecognition, parser.SoMarkEnableTitleLevelRecognition),
+ "enable_inline_image": parser.envOrBool(common.EnvSOMarkEnableInlineImage, parser.SoMarkEnableInlineImage),
+ "enable_table_image": parser.envOrBool(common.EnvSOMarkEnableTableImage, parser.SoMarkEnableTableImage),
+ "enable_image_understanding": parser.envOrBool(common.EnvSOMarkEnableImageUnderstanding, parser.SoMarkEnableImageUnderstanding),
+ "keep_header_footer": parser.envOrBool(common.EnvSOMarkKeepHeaderFooter, parser.SoMarkKeepHeaderFooter),
})
_ = writer.WriteField("element_formats", string(elementFormats))
_ = writer.WriteField("feature_config", string(featureConfig))
@@ -221,17 +225,26 @@ func soMarkBlockToItem(block map[string]any, keepHeaderFooter bool) map[string]a
}
}
-func envOrDefault(envKey, configured, fallback string) string {
+// envOrDefault prefers configured values, then the environment for named
+// engines only, then the built-in fallback.
+func (p *PDFParser) envOrDefault(envKey, configured, fallback string) string {
if configured != "" {
return configured
}
- if raw := strings.TrimSpace(common.GetEnv(envKey)); raw != "" {
- return raw
+ if !p.UseTenantModelConfig {
+ if raw := strings.TrimSpace(common.GetEnv(envKey)); raw != "" {
+ return raw
+ }
}
return fallback
}
-func envOrBool(envKey string, configured bool) bool {
+// envOrBool preserves tenant instance values, including explicit false, while
+// named engines retain their environment override.
+func (p *PDFParser) envOrBool(envKey string, configured bool) bool {
+ if p.UseTenantModelConfig {
+ return configured
+ }
if raw := strings.TrimSpace(common.GetEnv(envKey)); raw != "" {
switch strings.ToLower(raw) {
case "1", "true", "yes", "on":
diff --git a/internal/parser/parser/pdf_parser_tenant_config_test.go b/internal/parser/parser/pdf_parser_tenant_config_test.go
new file mode 100644
index 0000000000..17976d6a39
--- /dev/null
+++ b/internal/parser/parser/pdf_parser_tenant_config_test.go
@@ -0,0 +1,95 @@
+// Copyright 2026 The InfiniFlow Authors. All Rights Reserved.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+package parser
+
+import (
+ "net/http"
+ "net/http/httptest"
+ "strings"
+ "testing"
+
+ "ragflow/internal/common"
+)
+
+// TestPDFParserTenantConfigurationPolicy keeps named-engine environment behavior
+// while explicit tenant instances cannot borrow an unrelated global credential.
+func TestPDFParserTenantConfigurationPolicy(t *testing.T) {
+ withSSRFBypass(t)
+ t.Setenv(common.EnvSOMarkAPIKey, "global-fixture-key")
+ t.Setenv(common.EnvOpenDataLoaderAPIKey, "global-fixture-key")
+ t.Setenv(common.EnvSOMarkEnableInlineImage, "true")
+ for _, engine := range []string{"somark", "opendataloader"} {
+ for _, selected := range []bool{false, true} {
+ t.Run(engine+map[bool]string{false: "/named", true: "/selected"}[selected], func(t *testing.T) {
+ requests := make(chan string, 4)
+ server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
+ if strings.Contains(r.Header.Get("Content-Type"), "multipart") {
+ if err := r.ParseMultipartForm(1 << 20); err != nil {
+ t.Errorf("multipart: %v", err)
+ return
+ }
+ defer r.MultipartForm.RemoveAll()
+ } else {
+ _ = r.ParseForm()
+ }
+ key := r.FormValue("api_key")
+ if engine == "opendataloader" {
+ key = strings.TrimPrefix(r.Header.Get("Authorization"), "Bearer ")
+ }
+ requests <- key
+ switch r.URL.Path {
+ case "/parse/async":
+ want := `"enable_inline_image":true`
+ if selected {
+ want = `"enable_inline_image":false`
+ }
+ if !strings.Contains(r.FormValue("feature_config"), want) {
+ t.Errorf("feature environment policy not respected")
+ }
+ _, _ = w.Write([]byte(`{"code":0,"data":{"task_id":"local"}}`))
+ case "/parse/async_check":
+ _, _ = w.Write([]byte(`{"code":0,"data":{"status":"SUCCESS","result":{"outputs":{"json":{"pages":[{"blocks":[{"type":"text","content":"fixture"}]}]}}}}}`))
+ case "/file_parse":
+ _, _ = w.Write([]byte(`{"md_text":"fixture"}`))
+ default:
+ t.Errorf("unexpected path: %s", r.URL.Path)
+ }
+ }))
+ defer server.Close()
+ p := NewPDFParser()
+ p.ParseMethod = engine
+ p.SoMarkBaseURL, p.OpenDataLoaderAPIServer = server.URL, server.URL
+ p.SoMarkEnableInlineImage = false
+ p.ConfigureFromSetup(map[string]any{"use_tenant_model_config": true})
+ if p.UseTenantModelConfig {
+ t.Fatal("runtime-only policy accepted from saved settings")
+ }
+ p.UseTenantModelConfig = selected
+ if result := p.ParseWithResult(t.Context(), "sample.pdf", []byte("pdf")); result.Err != nil {
+ t.Fatal(result.Err)
+ }
+ if len(requests) == 0 {
+ t.Fatal("no parser requests")
+ }
+ for len(requests) > 0 {
+ key := <-requests
+ if selected && key != "" || !selected && key != "global-fixture-key" {
+ t.Fatal("incorrect environment credential policy")
+ }
+ }
+ })
+ }
+ }
+}