Skip to content

fix: open metadata and benchmark task files as UTF-8 - #1252

Merged
stevemessick merged 4 commits into
Kaggle:mainfrom
tb91117:utf8-metadata-files
Nov 25, 2026
Merged

stevemessick merged 4 commits into
Kaggle:mainfrom
tb91117:utf8-metadata-files

Conversation

@tb91117

@tb91117 tb91117 commented Nov 20, 2026 •

Copy link
Copy Markdown
Contributor

I hit this when a × in one of my dataset descriptions showed up as × on the dataset page after kaggle datasets metadata --update on Windows.

open() without an encoding uses the locale's encoding. On Windows that is the ANSI code page: cp1252 on most Western machines, cp932 on Japanese ones, gbk on Chinese ones. The CLI opens dataset-metadata.json and the other metadata files that way, so on Windows:

  • datasets metadata --update, datasets create and datasets version upload UTF-8 text garbled, with no error. 144,606 trees × 28 columns becomes 144,606 trees × 28 columns.
  • Text the code page can't decode, such as Japanese, fails the command with UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d.

The same happens with model, model instance and competition metadata, and with the task file in benchmarks tasks push. kernels push no longer has this problem (it was #186): it already opens kernel-metadata.json and the code file with encoding="utf-8".

Change

Sets the encoding on 22 open() calls in kaggle_api_extended.py, and changes it on one more (kernels push):

  • Metadata reads use utf-8-sig, which reads UTF-8 with or without a BOM, so files saved by Notepad or PowerShell 5's Set-Content -Encoding UTF8 work too: the metadata file in competitions create, datasets metadata --update, datasets create, datasets version, models create, models update, models instances create, models instances update, kernels pull (2 places) and kernels push.
  • The task file in benchmarks tasks push is read as utf-8.
  • Writes use utf-8: the metadata files written by the init commands, datasets metadata, kernels pull -m, models get and models instances get, and the example and reference files written by benchmarks init. json.dump escapes non-ASCII, so the metadata files it writes don't change. The reference file has an em dash, which was written in cp1252, and on Japanese Windows (cp932) it can't be written at all.

The only open() calls left without an encoding are for the config file, the download resume marker and downloaded files.

One behavior change: a metadata file saved in a legacy code page with non-ASCII characters is no longer read in that code page. It now fails with <file> is not UTF-8 encoded, please re-save it as UTF-8, since JSON exchanged between systems must be UTF-8 (RFC 8259).

Tests

  • New tests/unit/test_metadata_encoding.py with 44 tests. An autouse fixture makes open() in kaggle_api_extended default to cp1252, as on a Western Windows machine, so the tests reproduce the bug on any OS. The readers are tested with text that cp1252 garbles (×) and text it can't decode (Japanese), each metadata file is written with and without a BOM, a cp1252 file is checked to get the new error, and the reference file is checked to be UTF-8.
  • On main, 42 of the 44 fail: the request gets the garbled text, the command raises UnicodeDecodeError or "Unexpected UTF-8 BOM", or the reference file isn't UTF-8. The 2 that pass are kernels push without a BOM, which Encoding issues when pushing a kernel #186 already fixed.
  • Three assertions in test_kernels_pull.py now expect the new encodings in the open() calls.
  • pytest tests/unit: 1515 passed. The one failure, test_symlinked_state_dir_is_rejected, also fails on main on my Windows machine and is unrelated.
  • black --check and mypy src/kaggle tests pass.

open() without an encoding uses the locale's encoding, which on Windows
is the ANSI code page (cp1252, cp932, gbk, ...). The CLI read
dataset-metadata.json and the other metadata files that way, so UTF-8
text was uploaded garbled ("×" became "×") or the command failed with
UnicodeDecodeError. kernels push already reads kernel-metadata.json and
the code file as UTF-8. Do the same for the dataset, model, model
instance and competition metadata files, kernels pull, the benchmarks
task file, and the files these commands write.
@sridipbasu sridipbasu added the bug Something isn't working label Nov 20, 2026
Comment thread src/kaggle/api/kaggle_api_extended.py Outdated
owner_slug, dataset_slug, effective_path = self.dataset_metadata_prep(dataset, path)
meta_file = self.get_dataset_metadata_file(effective_path)
with open(meta_file, "r") as f:
with open(meta_file, "r", encoding="utf-8") as f:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Small Nit: files saved as UTF-8 with a BOM (older Notepad, or PowerShell 5 Set-Content -Encoding UTF8) still fail here with "Unexpected UTF-8 BOM". They failed before too, so it's not a regression, but utf-8-sig would read files both with and without a BOM. The same change would apply to the other metadata reads, while the writes can stay utf-8. Totally fine to skip if you'd rather keep it consistent with kernels push.

CC: @stevemessick

Suggested change
with open(meta_file, "r", encoding="utf-8") as f:
with open(meta_file, "r", encoding="utf-8-sig") as f:

Comment thread src/kaggle/api/kaggle_api_extended.py Outdated

# read json
with open(meta_file) as f:
with open(meta_file, encoding="utf-8") as f:

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another small one: a metadata file saved in cp1252 with non-ASCII text now ends in a raw UnicodeDecodeError traceback, which doesn't really tell the user what to do. Maybe catch it and raise something like ValueError(f"{meta_file} is not UTF-8 encoded, please re-save it as UTF-8")? Not a blocker, could also be a follow-up.

Metadata files are now read as utf-8-sig, so a file saved with a BOM
(Notepad, or PowerShell 5's Set-Content -Encoding UTF8) no longer fails
with "Unexpected UTF-8 BOM". kernels push reads kernel-metadata.json the
same way. A metadata file saved in another encoding now raises
"<file> is not UTF-8 encoded, please re-save it as UTF-8" instead of
the codec error. Writes stay plain UTF-8.

Co-authored-by: Sridip Basu <122687772+sridipbasu@users.noreply.github.com>
@tb91117

tb91117 commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @sridipbasu, both are done in fbde4d9:

  • Every metadata read now uses utf-8-sig, including kernels push, so files with or without a BOM work. Writes stay utf-8. I added you as co-author, since the BOM change was your suggestion.
  • A metadata file in another encoding now raises ValueError with your wording: <file> is not UTF-8 encoded, please re-save it as UTF-8.

Each metadata test now runs with and without a BOM, and a new test covers the cp1252 error. The new cases fail on the previous commit. I also tried a file written by PowerShell 5's Set-Content -Encoding UTF8 (starts with EF BB BF) and one saved as cp1252: the first reads fine and the second gets the new message.

@stevemessick

Copy link
Copy Markdown
Contributor

/gcbrun

@stevemessick

Copy link
Copy Markdown
Contributor

Thanks @tb91117 — great catch and very clean fix, especially the cp1252 fixture in tests/unit/test_metadata_encoding.py!

Two small suggestions if you'd like to fold them in before we merge:

  1. Use utf-8-sig and the same friendly error in benchmarks_tasks_push_cli (src/kaggle/api/kaggle_api_extended.py):
    Currently benchmarks_tasks_push_cli opens the .py task file with encoding="utf-8":

    • If task.py is saved with a UTF-8 BOM (e.g., Notepad or PowerShell 5 Set-Content -Encoding UTF8 — valid Python per PEP 263), open(..., encoding="utf-8") leaves a leading \ufeff in content. ast.parse() inside _get_task_names_from_file then hits SyntaxError: invalid non-printable character U+FEFF, gets caught as [], and raises a misleading No @task decorators found in '<file>' error (and jupytext would otherwise carry \ufeff into cell 1 of the notebook).
    • If task.py is saved in a legacy code page like cp1252, f.read() raises a raw UnicodeDecodeError that doesn't name the file or suggest re-saving as UTF-8.

    Reading it the same way as _load_metadata_json handles both cleanly (and test_benchmarks_push_reads_task_file_as_utf8 could then also run across ["utf-8", "utf-8-sig"]):

         try:
             with open(file, encoding="utf-8-sig") as f:
                 content = f.read()
         except UnicodeDecodeError as e:
             raise ValueError(f"{file} is not UTF-8 encoded, please re-save it as UTF-8") from e
  2. CHANGELOG.md entry:
    Could you add a bullet under ### Next in CHANGELOG.md summarizing this fix?

Approving as-is and holding off on merging so you have a chance to push an update if you'd like!

kaggle benchmarks tasks push now reads the task file the same way as the
metadata files: a UTF-8 BOM is accepted (it used to reach ast.parse as
U+FEFF and end in a misleading "No @task decorators found"), and a file
in another encoding raises "<file> is not UTF-8 encoded, please re-save
it as UTF-8" instead of a raw UnicodeDecodeError. The task-file test now
runs with and without a BOM, and a cp1252 task file has its own test.
@tb91117

tb91117 commented Nov 24, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @stevemessick, both are in 982343a:

  1. benchmarks_tasks_push_cli now opens the task file with utf-8-sig and raises the same "is not UTF-8 encoded, please re-save it as UTF-8" error. test_benchmarks_push_reads_task_file_as_utf8 runs with and without a BOM, and a new test covers a cp1252 task file.
  2. Added a bullet under ### Next in CHANGELOG.md.

@stevemessick

Copy link
Copy Markdown
Contributor

/gcbrun

@stevemessick stevemessick left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

@stevemessick
stevemessick merged commit 8dd3016 into Kaggle:main Nov 25, 2026
12 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

bug Something isn't working

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants