rohitg00/ai-engineering-from-scratch · error · RuntimeError

writer is not open

Error message

writer is not open

What it means

State guard in the HDF5 writer: _flush_buffer was invoked after the dataset was closed (close() sets _dataset to None), so the residual buffer has nowhere to be written.

Solutions

  1. Perform all add_document/add_boundary calls inside the with block before the writer closes
  2. Check writer._dataset or an is_open flag before appending after close
Defensive patterns

Strategy: type-guard

When it happens

Trigger: Thrown at phases/19-capstone-projects/43-hdf5-tokenized-corpus/code/main.py:170 when the library encounters an invalid state.

Common situations: See trigger scenarios.


AI-assisted analysis of rohitg00/ai-engineering-from-scratch@39ea8a1c6d (2026-08-26). Data as JSON: /api/errors/5cff0546ec6d8007. Report an issue: GitHub.

Appendix: source

Thrown at phases/19-capstone-projects/43-hdf5-tokenized-corpus/code/main.py:170

                self._dataset = None

    def add_document(self, token_ids: Iterable[int]) -> None:
        self._document_count += 1
        for token in token_ids:
            self._buffer.append(int(token))
            if len(self._buffer) >= self.chunk_size:
                self._flush_buffer(final=False)

    def add_boundary(self) -> None:
        """Inject the separator token between documents."""

        self._buffer.append(BOUNDARY_TOKEN_ID)
        if len(self._buffer) >= self.chunk_size:
            self._flush_buffer(final=False)

    def _flush_buffer(self, final: bool) -> None:
        if self._dataset is None:
            raise RuntimeError("writer is not open")
        if not self._buffer:
            return
        size = len(self._buffer) if final else self.chunk_size
        chunk = np.asarray(self._buffer[:size], dtype=TOKEN_DTYPE)
        new_total = self._token_count + size
        self._dataset.resize((new_total,))
        self._dataset[self._token_count : new_total] = chunk
        self._dataset.flush()
        self._hasher.update(chunk.tobytes())
        self._token_count = new_total
        self._buffer = self._buffer[size:]
        if not final and len(self._buffer) >= self.chunk_size:
            self._flush_buffer(final=False)

    @property
    def token_count(self) -> int:
        return self._token_count

View on GitHub (pinned to 39ea8a1c6d)