ragas icon indicating copy to clipboard operation
ragas copied to clipboard

[Bug]: Reusing the same train_test_split seed changes partitions and mutates input

Open Elon-Jiang opened this issue 3 weeks ago • 0 comments

[x] I reviewed the split method's documentation and existing split tests.

Describe the bug

Calling Dataset.train_test_split() twice on the same populated dataset with the same random_state produces different train/test partitions. The method calls random.shuffle(self._data), so each call shuffles the previous call's order. It also changes the original caller-supplied list and resets the application's global random state.

This makes repeated evaluation/optimization runs use different data despite an unchanged seed. The same inherited method affects Experiment.

Ragas version: main at 298b68274234c060deacab3cf5fb52aa3a20e885. Python version: 3.12.13, macOS arm64; Pydantic 2.14.3.

Code to reproduce

from ragas import Dataset

rows = [{"id": i} for i in range(10)]
dataset = Dataset("repro", "inmemory", data=rows)

train1, test1 = dataset.train_test_split(test_size=0.3, random_state=42)
train2, test2 = dataset.train_test_split(test_size=0.3, random_state=42)

print([row["id"] for row in train1])
print([row["id"] for row in train2])
print([row["id"] for row in rows])

Observed output on pristine main

[7, 3, 2, 8, 5, 6, 9]
[4, 8, 2, 0, 6, 9, 1]
[4, 8, 2, 0, 6, 9, 1, 5, 7, 3]

Expected behavior

Repeated calls with the same seed and unchanged input produce identical partitions. Splitting preserves the source's order and does not change the global RNG state. The first seeded split, split sizes, and the existing no-seed behavior remain compatible.

Additional context / contribution

I found this during source inspection and reproduced it with the real public Dataset and local backends, not during a production incident. I am working on a scoped fix that shuffles a temporary row list with a local RNG, with regressions covering Dataset/Experiment, dictionary/Pydantic rows, seeds 0/42, and the no-seed control. No model or external-service replacement is involved.

The current unrestricted dependency install encounters the separate VertexAI import problem tracked by PR #2957. The reproduction uses declared-compatible LangChain versions: langchain 0.3.30, langchain-core 0.3.86, langchain-community 0.3.31, langchain-openai 0.3.35. No dependency file changes are proposed.

Existing #2896 / closed PR #2897 concern loading an initially empty table from its backend; this report uses populated tables and addresses the separate in-place shuffle/global RNG defect. I checked all 266 current open PR file lists and all-state split/seed searches without finding this fix.

Codex assisted with investigation, implementation, tests, and this report under the contributor's authorization.

Elon-Jiang avatar Nov 07 '26 09:11 Elon-Jiang