Failed result serialization destroys a previously saved JSON result

Đang mở Phù hợp với người mới
#867 0 bình luận 0 reaction 0 người được giao Xem trên GitHub

@chessing234 đang làm issue này rồi.

Từ ngày 15/11/2026.

  • #868 của @chessing234 — đang mở

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
78/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Công nghệ
python
Lĩnh vực
tooling

Hướng nghiên cứu

Bắt đầu trong src/evaluate/saving.py tại hàm save, nơi dòng 43 chạy json.dump(data, f) sau khi đích đã được mở để ghi. Chạy reproducer trong issue để xác nhận tệp bị cắt cụt, sau đó chuyển việc tuần tự hóa sang lời gọi json.dumps trước khi tệp được mở để một TypeError để nguyên các byte hiện có và không tạo tệp mới. Hoàn thành nghĩa là reproducer in ra Previous result preserved: True, một đích mới vẫn không tồn tại sau khi lỗi, và các bài kiểm thử lưu hiện có vẫn chạy qua trên cả hai stack phụ thuộc; lưu ý rằng người báo cáo cho biết một bản sửa và các ca kiểm thử đã tồn tại, nên hãy kiểm tra công việc đang tiến hành trước.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

evaluate.save() opens an existing result in write mode before it knows whether the new payload can be JSON-serialized. A serialization error can therefore destroy an earlier valid result and leave an incomplete JSON file.

Reproducer (no Hub downloads):

from pathlib import Path
from tempfile import TemporaryDirectory
import traceback
import numpy as np
import evaluate

with TemporaryDirectory() as directory:
    destination = Path(directory) / "results.json"
    evaluate.save(destination, accuracy=0.75)
    previous = destination.read_bytes()
    try:
        evaluate.save(destination, accuracy=1.0, details={"scores": np.array([1, 2])})
    except TypeError:
        traceback.print_exc()
    print("Previous result preserved:", destination.read_bytes() == previous)
    print(destination.read_text(encoding="utf-8"))

Actual output after the exception:

Previous result preserved: False
{"accuracy": 1.0, "details": {"scores":

Expected: still raise TypeError for unsupported JSON values, but preserve the previous file unchanged. For a new destination, a failed serialization should not leave a partial result file. This is not a request to add NumPy encoding support or promise atomic writes against operating-system/process failures.

Full traceback from the standalone reproduction (installation prefixes shortened):

Traceback (most recent call last):
  File "repro.py", line 51, in save_failure
    evaluate.save(destination, accuracy=1.0, details={"scores": np.array([1, 2])})
  File "src/evaluate/saving.py", line 43, in save
    json.dump(data, f)
  File "python3.11/json/__init__.py", line 179, in dump
    for chunk in iterable:
  File "python3.11/json/encoder.py", line 432, in _iterencode
    yield from _iterencode_dict(o, _current_indent_level)
  File "python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "python3.11/json/encoder.py", line 406, in _iterencode_dict
    yield from chunks
  File "python3.11/json/encoder.py", line 439, in _iterencode
    o = _default(o)
        ^^^^^^^^^^^
  File "python3.11/json/encoder.py", line 180, in default
    raise TypeError(f'Object of type {o.__class__.__name__} '
TypeError: Object of type ndarray is not JSON serializable

Reproduced on unchanged main a5db2ad54aaeadf15c7aa1f056bc6f7bbeeb7e5f, macOS 26.6.2 / Darwin 25.6.0, Python 3.11.13, with datasets 2.21.0 / NumPy 1.26.4 and datasets 5.0.1 / NumPy 2.4.6. PyTorch 2.8.0 is installed but is not involved; TensorFlow is not installed.

Searches of open/closed reports for save, overwrite, corruption, serializable, atomic, truncate, and _setup_path found no matching fix. Related #808 handles missing Git and #809 handles dotted directories. The original #7 discusses format choices and JSONL concurrency, not this single-call serialization failure.

A focused fix serializes the payload before opening the destination. Four failure cases cover existing/new files and scalar/nested NumPy values; another case keeps intentional replacement of a valid explicit destination working. These and the existing saving tests pass on both dependency stacks (8 per stack), and repository quality checks pass. All four preservation cases fail on unchanged main.

Ngôn ngữ chính
Python
Star
2.5k
Fork
380
Chỉ số merge pull request
Không có pull request nào được merge trong 30 ngày

Chuẩn bị môi trường

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của huggingface/evaluate

Tất cả issue của huggingface/evaluate

Issue tương tự

Thêm issue về Python

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.