Build Agentic workflows, RAG pipelines, with rich AI model and tool support on one collaborative workspace. Deploy on cloud, VPC, or self-hosted, so teams move from prototype to production without rebuilding the stack.
Python agentagentic-aiagentic-frameworkagentic-workflowaiautomationclaudedeepseekgenaigptllmlow-codemcpnextjsno-codeopenaipythonskillsworkflow
fix: fix sample_size not work (#42131)
wangxiaolei66f1e6568e65
diff --git a/api/core/rag/extractor/helpers.py b/api/core/rag/extractor/helpers.pyindex 5b466b281c4..71c5325b080 100644--- a/api/core/rag/extractor/helpers.py+++ b/api/core/rag/extractor/helpers.py@@ -31,7 +31,9 @@ def detect_file_encodings(file_path: str, timeout: int = 5, sample_size: int = 1"""def read_and_detect(filename: str):- rst = charset_normalizer.from_path(filename)+ with open(filename, "rb") as file:+ sample = file.read(sample_size)+ rst = charset_normalizer.from_bytes(sample)best = rst.best()if best is None:return []diff --git a/api/tests/unit_tests/core/rag/extractor/test_helpers.py b/api/tests/unit_tests/core/rag/extractor/test_helpers.pyindex 1c6f97ec53c..43ef3283392 100644--- a/api/tests/unit_tests/core/rag/extractor/test_helpers.py+++ b/api/tests/unit_tests/core/rag/extractor/test_helpers.py@@ -1,5 +1,7 @@import tempfile+from pathlib import Pathfrom types import SimpleNamespace+from unittest.mock import Mock, patchimport pytest@@ -8,6 +10,40 @@ from core.rag.extractor.helpers import detect_file_encodingsclass TestHelpers:+ @pytest.mark.parametrize(("sample_size", "file_size"), [(None, 1_200_000), (4096, 140_000), (4096, 12)])+ def test_detect_file_encodings_bounds_read(self, tmp_path: Path, sample_size: int | None, file_size: int):+ file_path = tmp_path / "sample.txt"+ file_path.write_bytes(b"a" * file_size)+ real_open = open+ reads = []+ byte_counts = []++ def tracked_open(filename, *args, **kwargs):+ stream = real_open(filename, *args, **kwargs)+ if str(filename) == str(file_path):+ real_read = stream.read++ def tracked_read(*read_args, **read_kwargs):+ data = real_read(*read_args, **read_kwargs)+ byte_counts.append(len(data))+ return data++ stream.read = Mock(side_effect=tracked_read)+ reads.append(stream.read)+ return stream++ with patch("builtins.open", side_effect=tracked_open):+ if sample_size is None:+ encodings = detect_file_encodings(str(file_path))+ else:+ encodings = detect_file_encodings(str(file_path), sample_size=sample_size)++ expected_limit = 1024 * 1024 if sample_size is None else sample_size+ assert len(reads) == 1+ reads[0].assert_called_once_with(expected_limit)+ assert byte_counts == [min(file_size, expected_limit)]+ assert encodings[0].encoding == "ascii"+def test_detect_file_encodings(self) -> None:with tempfile.NamedTemporaryFile(mode="w+t", suffix=".txt") as temp:temp.write("Shared data")@@ -41,15 +77,22 @@ class TestHelpers:with pytest.raises(TimeoutError, match="Timeout reached while detecting encoding"):detect_file_encodings("file.txt", timeout=1)- def test_detect_file_encodings_raises_when_encoding_not_detected(self, monkeypatch: pytest.MonkeyPatch):+ @pytest.mark.parametrize("no_match", [False, True])+ def test_detect_file_encodings_raises_when_encoding_not_detected(+ self, monkeypatch: pytest.MonkeyPatch, tmp_path: Path, no_match: bool+ ):class FakeResult:encoding = Nonecoherence = 0.0language = None+ file_path = tmp_path / "sample.txt"+ file_path.write_bytes(b"sample")monkeypatch.setattr(- helpers.charset_normalizer, "from_path", lambda _: SimpleNamespace(best=lambda: FakeResult())+ helpers.charset_normalizer,+ "from_bytes",+ lambda _: SimpleNamespace(best=lambda: None if no_match else FakeResult()),)with pytest.raises(RuntimeError, match="Could not detect encoding"):- detect_file_encodings("file.txt")+ detect_file_encodings(str(file_path))diff --git a/api/tests/unit_tests/core/rag/extractor/test_text_extractor.py b/api/tests/unit_tests/core/rag/extractor/test_text_extractor.pyindex 908bf2a9088..92edf3f4b8f 100644--- a/api/tests/unit_tests/core/rag/extractor/test_text_extractor.py+++ b/api/tests/unit_tests/core/rag/extractor/test_text_extractor.py@@ -8,6 +8,18 @@ from core.rag.extractor.text_extractor import TextExtractorclass TestTextExtractor:+ def test_extract_autodetect_preserves_full_file_beyond_sample(self, tmp_path: Path):+ file_path = tmp_path / "large.txt"+ content = "Document text\n" * 50_000 + "End of document"+ file_path.write_text(content, encoding="utf-16")+ assert file_path.stat().st_size > 1024 * 1024++ docs = TextExtractor(str(file_path), encoding="utf-8", autodetect_encoding=True).extract()++ assert len(docs) == 1+ assert docs[0].page_content == content+ assert docs[0].metadata == {"source": str(file_path)}+def test_extract_success(self, tmp_path: Path):file_path = tmp_path / "data.txt"file_path.write_text("hello world", encoding="utf-8")