Skip to content

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

fix(api): validate embedding check sample count (#19574)

Harsh Kashyapaf61e3fcd618
diff --git a/api/apps/services/dataset_api_service.py b/api/apps/services/dataset_api_service.py
index 7aff2a7f17..f4d3750253 100644
--- a/api/apps/services/dataset_api_service.py
+++ b/api/apps/services/dataset_api_service.py
@@ -1343,7 +1343,12 @@ def check_embedding(dataset_id: str, tenant_id: str, req: dict):
embd_model_config = resolve_model_config(kb.tenant_id, LLMType.EMBEDDING, embd_id)
emb_mdl = LLMBundle(kb.tenant_id, embd_model_config)
- n = int(req.get("check_num", 5))
+ raw_check_num = req.get("check_num", 5)
+ if type(raw_check_num) is not int:
+ return False, "`check_num` must be an integer."
+ n = raw_check_num
+ if n <= 0:
+ return False, "`check_num` must be greater than 0."
samples = sample_random_chunks_with_vectors(settings.docStoreConn, tenant_id=kb.tenant_id, kb_id=dataset_id, n=n)
logging.info("check_embedding: dataset=%s sampled=%d chunks", dataset_id, len(samples))
diff --git a/test/unit_test/api/apps/services/test_gaussdb_dataset_embedding.py b/test/unit_test/api/apps/services/test_gaussdb_dataset_embedding.py
index 73fef442f8..098a2527c4 100644
--- a/test/unit_test/api/apps/services/test_gaussdb_dataset_embedding.py
+++ b/test/unit_test/api/apps/services/test_gaussdb_dataset_embedding.py
@@ -15,6 +15,8 @@
#
import importlib.util
import sys
+
+import pytest
from pathlib import Path
from types import ModuleType, SimpleNamespace
from unittest.mock import MagicMock
@@ -230,3 +232,37 @@ def test_tc_wrt_207_check_embedding_compares_gaussdb_valid_vectors_once(monkeypa
assert result["summary"]["valid"] == 1
assert result["summary"]["avg_cos_sim"] == 1.0
assert result["results"][0]["vector_dim"] == 4
+
+
[email protected]("check_num", ["invalid", 1.5, True])
+def test_check_embedding_rejects_non_integer_check_num(monkeypatch, check_num):
+ module = _load_dataset_module(monkeypatch)
+
+ _install_module(
+ monkeypatch,
+ "api.db.services.llm_service",
+ LLMBundle=FailIfCalledBundle,
+ resolve_llm_setting=lambda *_args, **_kwargs: {},
+ )
+
+ ok, result = module.check_embedding("kb1", "tenant1", {"embd_id": "new-embd", "check_num": check_num})
+
+ assert ok is False
+ assert result == "`check_num` must be an integer."
+
+
[email protected]("check_num", [0, -1])
+def test_check_embedding_rejects_non_positive_check_num(monkeypatch, check_num):
+ module = _load_dataset_module(monkeypatch)
+
+ _install_module(
+ monkeypatch,
+ "api.db.services.llm_service",
+ LLMBundle=FailIfCalledBundle,
+ resolve_llm_setting=lambda *_args, **_kwargs: {},
+ )
+
+ ok, result = module.check_embedding("kb1", "tenant1", {"embd_id": "new-embd", "check_num": check_num})
+
+ assert ok is False
+ assert result == "`check_num` must be greater than 0."