fix(evals): open data file with utf-8-sig to support BOM-encoded jsonl inputs#4202
fix(evals): open data file with utf-8-sig to support BOM-encoded jsonl inputs#4202Mukller wants to merge 2 commits into
Conversation
Mukller
left a comment
There was a problem hiding this comment.
Self-Review
Root Cause
pd.read_json(data, lines=True) opens the file using the default system encoding.
On all common systems that default is UTF-8, which does not strip a Byte Order Mark.
A BOM (\xef\xbb\xbf) at the start of the file makes the JSON parser see the BOM
as the first character of the first token, which is not valid JSON → ValueError.
utf-8-sig is a Python built-in codec that reads UTF-8 BOM as a signal to skip the
BOM, while reading plain UTF-8 identically. It is the standard fix for BOM-related
encoding issues.
Correctness
| Input encoding | Before | After |
|---|---|---|
| UTF-8 (no BOM) | OK | OK (unchanged) |
| UTF-8 with BOM | ValueError | OK (BOM stripped) |
| Other encodings | ValueError | ValueError (same) |
The fix is strictly more permissive: existing UTF-8 files are unaffected, and BOM-prefixed
files now load correctly.
Scope
Two lines changed in _validate_and_load_data. The error handling, the return value, and
all callers are unchanged.
Fixes
Fixes #3670.
_validate_and_load_datapasses the rawdatapath string topd.read_json(data, lines=True).pd.read_jsonuses the system default encoding when opening files, which is UTF-8 on mostsystems but does not strip a UTF-8 BOM (
\xef\xbb\xbf). A JSONL file written withutf-8-sigencoding (BOM-prefixed UTF-8) causespd.read_jsonto emitValueError: Expected object or valuebecause the BOM sits in front of the first{.Fix
Open the data file explicitly with
encoding="utf-8-sig"and pass the resulting file objectto
pd.read_json. Python'sutf-8-sigcodec:utf-8-sigclients).The error message and the surrounding
try/exceptare unchanged; the only difference ishow the file is opened.
Reproduction
Closes #3670.