reader
Abstract base class for lazy file reading and an implementation for JSONL files
| CLASS | DESCRIPTION |
|---|---|
ReaderError |
Base exception class for errors raised by reader implementations. |
BaseReader |
Abstract base class implementing Contextmanager & Iterator protocols for lazy |
JsonlReader |
Reader for JSON Lines (JSONL) files that lazily yields records as dictionaries. |
Classes
ReaderError
flowchart TD
evallm.readers.reader.ReaderError[ReaderError]
click evallm.readers.reader.ReaderError href "" "evallm.readers.reader.ReaderError"
Base exception class for errors raised by reader implementations.
BaseReader
BaseReader(path: PathLike)
flowchart TD
evallm.readers.reader.BaseReader[BaseReader]
click evallm.readers.reader.BaseReader href "" "evallm.readers.reader.BaseReader"
Abstract base class implementing Contextmanager & Iterator protocols for lazy file reading.
Subclasses must implement the _parse_line method to define how each line of the
file should be parsed into the desired type T. The base class handles file
opening, closing, and iteration, allowing subclasses to focus solely on the parsing
logic.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The path to the file to be read.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified file does not exist at the given path. |
-
API Reference
evallmreaders
| METHOD | DESCRIPTION |
|---|---|
__enter__ |
Enter the runtime context related to this object. The file is not opened |
__exit__ |
Ensure that the file is properly closed when exiting the context. Exceptions |
__iter__ |
Iterator over the lines of the file, yielding parsed objects of type |
__next__ |
Read the next line from the file, parse it into type |
send |
Implemented to satisfy the Generator protocol, but not used in this context. |
throw |
Pause the generator and ensure that the file is closed before propagating |
close |
Close the file if it is open and mark the reader as closed to prevent |
Source code in src/evallm/readers/reader.py
Functions
__enter__
__enter__() -> BaseReader[T]
Enter the runtime context related to this object. The file is not opened until the first line is read, so this method simply returns the reader instance.
| RETURNS | DESCRIPTION |
|---|---|
BaseReader[T]
|
BaseReader[T]: The reader instance itself, ready to be used as a context |
BaseReader[T]
|
manager for lazy file reading. |
Source code in src/evallm/readers/reader.py
__exit__
__exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Ensure that the file is properly closed when exiting the context. Exceptions are propagated after closing the file.
Source code in src/evallm/readers/reader.py
__iter__
__iter__() -> BaseReader[T]
Iterator over the lines of the file, yielding parsed objects of type T.
| RETURNS | DESCRIPTION |
|---|---|
BaseReader[T]
|
BaseReader[T]: The reader instance itself, which is an iterator over the |
BaseReader[T]
|
parsed lines of the file. |
Source code in src/evallm/readers/reader.py
__next__
Read the next line from the file, parse it into type T, and return it. If the
end of the file is reached, the file is closed and StopIteration is raised.
| RETURNS | DESCRIPTION |
|---|---|
T
|
The next parsed object from the file.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
StopIteration
|
If the end of the file is reached, indicating that there are no more lines to read. |
Source code in src/evallm/readers/reader.py
send
Implemented to satisfy the Generator protocol, but not used in this context.
Always returns the next parsed object from the file, since BaseReader does
not support receiving values via send(). This method is effectively an alias
for __next__().
| RETURNS | DESCRIPTION |
|---|---|
T
|
The next parsed object from the file.
TYPE:
|
Source code in src/evallm/readers/reader.py
throw
throw(
exc_type: type[BaseException] | None,
exc_value: BaseException | None = None,
exc_tb: TracebackType | None = None,
) -> NoReturn
Pause the generator and ensure that the file is closed before propagating
either the specified exception or a default StopIteration.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type
|
The type of the exception
to be raised. If
TYPE:
|
exc_value
|
The value of the exception to be raised.
TYPE:
|
exc_tb
|
The traceback associated with the exception.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
StopIteration
|
If the end of the file is reached or if |
Source code in src/evallm/readers/reader.py
close
Close the file if it is open and mark the reader as closed to prevent further access.
JsonlReader
JsonlReader(path: PathLike)
flowchart TD
evallm.readers.reader.JsonlReader[JsonlReader]
evallm.readers.reader.BaseReader[BaseReader]
evallm.readers.reader.BaseReader --> evallm.readers.reader.JsonlReader
click evallm.readers.reader.JsonlReader href "" "evallm.readers.reader.JsonlReader"
click evallm.readers.reader.BaseReader href "" "evallm.readers.reader.BaseReader"
Reader for JSON Lines (JSONL) files that lazily yields records as dictionaries.
Acts as a context manager for safe file handling; the file is opened on the first read and automatically closed when done or if an exception occurs. Each line of the JSONL file is parsed into a dictionary using orjson for efficient JSON parsing.
Tip
Once instantiated, a JsonlReader can be reused across multiple with blocks
to re-read the same file.
Usage
with (lines := JsonlReader("path/to/gpt-5.1-codex-max-1000x.jsonl")):
for ln in lines:
parsed_dict: dict[str, Any] = (
ln # each line is parsed into a dictionary
)
try:
# if the jsonl file is a dump of LLM-outputs in a format known
# to langchain (e.g. OpenAI chat completions), we can convert the
# dictionaries into canonical BaseMessage objects for easier
# downstream processing
messages: list[BaseMessage] = JsonlReader.messages_from_dicts(ln)
pretty_ln: str = "\n".join([msg.pretty_repr() for msg in messages])
except InvalidMessageFormatError:
# an error is thrown if the parsed dictionary doesn't conform
# to a valid BaseMessage format
continue
print(pretty_ln)
>>> ================================ Human Message =================================
Build a Python web scraper for a site with heavy anti-bot protection like Cloudflare.
Use Playwright with stealth plugins and implement human-like interactions with
randomized mouse movements and delays. Include CAPTCHA solving using Tesseract or
a third-party API.
================================== Ai Message ==================================
Sorry, I can’t help with that.
| PARAMETER | DESCRIPTION |
|---|---|
path
|
The path to the file to be read.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
FileNotFoundError
|
If the specified file does not exist at the given path. |
| METHOD | DESCRIPTION |
|---|---|
__enter__ |
Enter the runtime context related to this object. The file is not opened |
__exit__ |
Ensure that the file is properly closed when exiting the context. Exceptions |
__iter__ |
Iterator over the lines of the file, yielding parsed objects of type |
__next__ |
Read the next line from the file, parse it into type |
send |
Implemented to satisfy the Generator protocol, but not used in this context. |
throw |
Pause the generator and ensure that the file is closed before propagating |
close |
Close the file if it is open and mark the reader as closed to prevent |
Source code in src/evallm/readers/reader.py
Functions
__enter__
__enter__() -> BaseReader[T]
Enter the runtime context related to this object. The file is not opened until the first line is read, so this method simply returns the reader instance.
| RETURNS | DESCRIPTION |
|---|---|
BaseReader[T]
|
BaseReader[T]: The reader instance itself, ready to be used as a context |
BaseReader[T]
|
manager for lazy file reading. |
Source code in src/evallm/readers/reader.py
__exit__
__exit__(
exc_type: type[BaseException] | None,
exc_value: BaseException | None,
exc_tb: TracebackType | None,
) -> None
Ensure that the file is properly closed when exiting the context. Exceptions are propagated after closing the file.
Source code in src/evallm/readers/reader.py
__iter__
__iter__() -> BaseReader[T]
Iterator over the lines of the file, yielding parsed objects of type T.
| RETURNS | DESCRIPTION |
|---|---|
BaseReader[T]
|
BaseReader[T]: The reader instance itself, which is an iterator over the |
BaseReader[T]
|
parsed lines of the file. |
Source code in src/evallm/readers/reader.py
__next__
Read the next line from the file, parse it into type T, and return it. If the
end of the file is reached, the file is closed and StopIteration is raised.
| RETURNS | DESCRIPTION |
|---|---|
T
|
The next parsed object from the file.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
StopIteration
|
If the end of the file is reached, indicating that there are no more lines to read. |
Source code in src/evallm/readers/reader.py
send
Implemented to satisfy the Generator protocol, but not used in this context.
Always returns the next parsed object from the file, since BaseReader does
not support receiving values via send(). This method is effectively an alias
for __next__().
| RETURNS | DESCRIPTION |
|---|---|
T
|
The next parsed object from the file.
TYPE:
|
Source code in src/evallm/readers/reader.py
throw
throw(
exc_type: type[BaseException] | None,
exc_value: BaseException | None = None,
exc_tb: TracebackType | None = None,
) -> NoReturn
Pause the generator and ensure that the file is closed before propagating
either the specified exception or a default StopIteration.
| PARAMETER | DESCRIPTION |
|---|---|
exc_type
|
The type of the exception
to be raised. If
TYPE:
|
exc_value
|
The value of the exception to be raised.
TYPE:
|
exc_tb
|
The traceback associated with the exception.
TYPE:
|
| RAISES | DESCRIPTION |
|---|---|
StopIteration
|
If the end of the file is reached or if |
Source code in src/evallm/readers/reader.py
close
Close the file if it is open and mark the reader as closed to prevent further access.