import json from pathlib import Path from typing import Any, Iterable def _write_jsonl_atomic(rows: Iterable[dict[str, Any]], path: Path) -> int: path.parent.mkdir(parents=True, exist_ok=True) temporary = path.with_suffix(path.suffix + ".tmp") count = 0 with temporary.open("w", encoding="utf-8") as stream: for row in rows: stream.write(json.dumps(row, ensure_ascii=False) + "\n") count += 1 temporary.replace(path) return count