Skip to content

Read and write bundle and datalist files as UTF-8 - #9282

Merged
ericspod merged 3 commits into
Project-MONAI:devfrom
CodeAvk:fix/9277-utf8-file-encoding
Nov 26, 2026
Merged

ericspod merged 3 commits into
Project-MONAI:devfrom
CodeAvk:fix/9277-utf8-file-encoding

Conversation

@CodeAvk

@CodeAvk CodeAvk commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

Fixes #9277.

Description

On Windows, Python often uses cp1252 as the default file encoding instead of UTF-8. Some parts of the code, including ConfigParser.load_config_file / export_config_file, the bundle scripts (_check_monai_version, init_bundle, _add_model_card_metadata), BundleWorkflow (properties_path), and the Decathlon datalist helpers, were opening files without specifying an encoding.

Because of this, UTF-8 config files containing special characters could either raise a UnicodeDecodeError or be read incorrectly. For example, "Größe" was displayed as "Größe". Writing files with ensure_ascii=False or allow_unicode=True could also raise a UnicodeEncodeError.

This PR fixes the issue by explicitly setting encoding="utf-8" wherever these files are opened.

I've also added tests that simulate cp1252 as the default encoding, so the issue can be caught on any OS, including Linux CI.

I reproduced the issue and verified the fix on Windows 11 with Python 3.13.

Types of changes

  • Non-breaking change (fix or new feature that would not break existing functionality).
  • New tests added to cover the changes.

Note: files that are not valid UTF-8 (for example saved as cp1252 or GBK) now raise UnicodeDecodeError instead of being read with the system's default encoding.

Fixes Project-MONAI#9277.

ConfigParser, the bundle scripts, BundleWorkflow and the Decathlon
datalist helpers opened their JSON, YAML and text files without an
encoding, so Python used the system default. On Windows that is often
cp1252, where a UTF-8 file with non-ASCII text either raises
UnicodeDecodeError or is decoded into wrong characters without an
error (e.g. "Größe" is read as "Größe"), and writing with
ensure_ascii=False or allow_unicode=True raises UnicodeEncodeError.

Open these files with encoding="utf-8". The new tests patch open() to
use cp1252 as the default, so they fail without the fix on any OS.

Signed-off-by: CodeAvk <avksmlavk@gmail.com>
@coderabbitai

coderabbitai Bot commented Nov 15, 2026 •

Copy link
Copy Markdown
Contributor

No actionable comments were generated in the recent review.

Walkthrough

Bundle configuration, metadata, model-card, workflow-property, and Decathlon datalist file operations now specify UTF-8. Regression tests simulate a cp1252 default and check non-ASCII content.

Estimated code review effort: 2 (Simple) | ~10 minutes

Severity of issue fixed: Medium

Merge Risk: Minimal · up to 667a5

This change makes bundle and datalist files read and write as UTF-8 regardless of the system default encoding, which fixes Windows decoding problems. Files saved in other encodings will now raise a decode error. No merge-blocking risk was found.

Pre-merge checks: Passed 4

Check name Status Explanation
Title check Passed The title names the main change: bundle and datalist files are now read and written as UTF-8. It matches the raw summary, which adds explicit encoding="utf-8" to the bundle config parser, scripts, wor…
Description check Passed The description follows the template. It gives the problem, the fix (explicit encoding="utf-8"), the added cp1252-simulation tests, and the checked 'Non-breaking change' and 'New tests added' boxes. I…
Linked Issues check Passed [#9277] Objective: read and write bundle configs, bundle scripts, BundleWorkflow properties, and Decathlon datalists as UTF-8, independent of the system default encoding. Verified at head by diff agai…
Out of Scope Changes check Passed Diffs confirm the config_parser.py and workflows.py changes are single-argument encoding additions to the named functions. The decathlon_datalist.py changes match the named loaders. The PR summary rep…
Full details: Title check

Explanation

The title names the main change: bundle and datalist files are now read and written as UTF-8. It matches the raw summary, which adds explicit encoding="utf-8" to the bundle config parser, scripts, workflows, and Decathlon datalist helpers.

Full details: Description check

Explanation

The description follows the template. It gives the problem, the fix (explicit encoding="utf-8"), the added cp1252-simulation tests, and the checked 'Non-breaking change' and 'New tests added' boxes. It also notes the new UnicodeDecodeError behavior. The unchecked items (quick/integration test runs, docstrings, docs) are not stated, which is minor.

Full details: Linked Issues check

Explanation

[#9277] Objective: read and write bundle configs, bundle scripts, BundleWorkflow properties, and Decathlon datalists as UTF-8, independent of the system default encoding. Verified at head by diff against base: ConfigParser.load_config_file uses open(..., encoding="utf-8") and export_config_file uses open(_filepath, "w", encoding="utf-8") (monai/bundle/config_parser.py). BundleWorkflow properties_path uses open(properties_path, encoding="utf-8") (monai/bundle/workflows.py). Earlier direct read confirmed load_decathlon_datalist, load_decathlon_properties, and create_cross_validation_datalist use encoding="utf-8" (monai/data/decathlon_datalist.py). The scripts.py diff (_check_monai_version, init_bundle, _add_model_card_metadata) could not be read because changed blobs are missing; that result rests on the PR summary, which states explicit UTF-8 in those functions. Test additions (cp1252 default simulation in tests/bundle/test_config_parser.py and tests/data/test_load_decathlon_datalist.py) are reported in the summary and were not read directly. Newline handling is not required by #9277.

Full details: Out of Scope Changes check

Explanation

Diffs confirm the config_parser.py and workflows.py changes are single-argument encoding additions to the named functions. The decathlon_datalist.py changes match the named loaders. The PR summary reports the scripts.py changes as encoding-only with no public declaration changes; that diff could not be read, so its scope rests on the summary. The summary reports test changes that support the encoding objective. No unrelated change is demonstrated.

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

Share

Recent review info

Run configuration

Commits

Reviewing files that changed from the base of the PR and between 1810703 and 667a5f0.

Files selected for processing: 5

  • monai/bundle/config_parser.py
  • monai/bundle/scripts.py
  • monai/bundle/workflows.py
  • tests/bundle/test_config_parser.py
  • tests/data/test_load_decathlon_datalist.py

Included review availability: This review used your included allowance. Your plan provides up to 8 included reviews per hour; 7 remain after this review.

@CodeAvk
CodeAvk marked this pull request as ready for review November 15, 2026 15:27
@CodeAvk

CodeAvk commented Nov 15, 2026

Copy link
Copy Markdown
Contributor Author

Thanks @ericspod! It's ready for review now. All checks pass, and I tested it locally on Windows 11.

@kesonglab kesonglab left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Thanks — this addresses a real problem on Windows (cp1252 as the default locale encoding). The direction and the set of touched call sites look right.

Some suggestions:

  1. Consider a repo-wide sweep rather than point fixes. This class of bug recurs; grep -rn 'open(' monai/ | grep -v encoding= or enabling ruff's PLW1514 (unspecified-encoding) would catch remaining sites and prevent regressions. Worth confirming nothing is left in monai/bundle/ and monai/data/.

  2. This is a behaviour change for non-UTF-8 inputs. Files that previously loaded under cp1252/GBK will now raise UnicodeDecodeError. That is the correct contract for JSON/YAML, but it is user-visible and probably deserves a note in the PR description.

  3. Newline handling on write. Without newline="" / newline="\n", Windows writes \r\n. If any bundle content is hashed or compared textually, that produces different bytes. newline="\n" may be worth setting on the write sites.

  4. Test coverage: a negative case (a file with non-UTF-8 bytes, now raising) would pin down the new contract.

LGTM modulo the above.

A cp1252-encoded config used to load when cp1252 was the default encoding. It now raises UnicodeDecodeError, which this test pins down.

Signed-off-by: CodeAvk <avksmlavk@gmail.com>
@CodeAvk

CodeAvk commented Nov 20, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review!

  1. I kept this PR to the bundle/datalist files from Bundle configs and datalists fail to handle non-UTF-8 default encodings on Windows #9277. In monai/bundle and monai/data, the remaining text-mode opens are csv_saver.py, an empty hash file and os.devnull. csv_saver.py could be a follow-up PR.
  2. Good point, I've added a note to the description: non-UTF-8 files now raise UnicodeDecodeError instead of being read with the locale encoding.
  3. This PR doesn't change newline handling (text mode wrote \r\n on Windows before as well), so I'd leave that for a separate discussion.
  4. Added a test that a cp1252-encoded config now raises UnicodeDecodeError.

@CodeAvk

CodeAvk commented Nov 26, 2026

Copy link
Copy Markdown
Contributor Author

Hi @ericspod, gentle ping on this one when you have time. I've answered kesonglab's review comments and all checks still pass. Once this is in, I'd like to open the follow-up for #9293 (UTF-8 for csv_saver.py and the handler utils). Thanks!

@ericspod ericspod self-assigned this Nov 26, 2026

@ericspod ericspod left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Thank @CodeAvk I think this is fine now. I'm going to run the tests but I have other things to merge first before coming back to this one, and will have to re-authorise the tests so it'll take some time.

@ericspod
ericspod enabled auto-merge (squash) November 26, 2026 14:34
@ericspod
ericspod merged commit 8c31f2e into Project-MONAI:dev Nov 26, 2026
32 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Bundle configs and datalists fail to handle non-UTF-8 default encodings on Windows

3 participants