Repository navigation
Description
Describe the bug
I noticed that several MONAI functions read and write JSON, YAML, and Markdown files without explicitly specifying an encoding.
Python uses the system's default encoding in these cases. This is usually UTF-8 on Linux and macOS, but on Windows it can be a legacy encoding such as cp1252 or cp936.
Because of this, valid UTF-8 files containing non-ASCII characters can cause issues when used on Windows. For example, a configuration file containing text like Größe ≥ 3 mm or Chinese characters may raise a UnicodeDecodeError instead of loading correctly.
The same problem can occur when writing files. Characters such as ≥ may trigger a UnicodeEncodeError, while other characters may be written incorrectly.
I was able to reproduce this with Python's default file handling on Windows 11. The issue also appears on Linux when the default encoding is not UTF-8.
The affected functions I identified are:
monai/bundle/config_parser.py
ConfigParser.load_config_file
ConfigParser.export_config_file
monai/bundle/scripts.py
_check_monai_version
init_bundle
_add_model_card_metadata
monai/bundle/workflows.py
BundleWorkflow (properties_path)
monai/data/decathlon_datalist.py
load_decathlon_datalist
load_decathlon_properties
To Reproduce
The following example reproduces the issue on Windows when the default encoding is cp1252:
import json
from pathlib import Path
Path("config.json").write_bytes(
'{"note": "Lungenknoten ≥ 3 mm, 肺結節"}'.encode("utf-8")
)
with open("config.json") as f:
json.load(f)Observed output:
UnicodeDecodeError: 'charmap' codec can't decode byte 0x90 in position 38: character maps to <undefined>
The same issue occurs when writing non-ASCII content:
import json
with open("out.json", "w") as f:
json.dump(
{"note": "Lungenknoten ≥ 3 mm, 肺結節"},
f,
ensure_ascii=False
)Observed output:
UnicodeEncodeError: 'charmap' codec can't encode character '≥' in position 14: character maps to <undefined>
I also reproduced the loading issue on Linux with a non-UTF-8 default encoding:
UnicodeDecodeError: 'ascii' codec can't decode byte 0xe2 in position 23: ordinal not in range(128)
Expected behavior
MONAI should consistently read and write configuration files, datalists, metadata, and model cards using UTF-8, regardless of the operating system's default encoding.
This would make bundle files more portable across Windows, Linux, and macOS and prevent failures when files contain non-ASCII characters.
Environment
Windows check:
Windows 11 Pro (build 26300)
Python 3.14.4
locale.getpreferredencoding() = cp1252
Note: MONAI was not installed for the Windows reproduction. This was tested using plain Python file handling.
MONAI check:
Docker / Linux
Python 3.11.18
MONAI dev at 846ae34
MONAI version: 1.6.1rc0+66.g846ae34
Additional context
I looked into the affected code paths and found that explicitly specifying UTF-8 when opening these files should address the underlying issue.
I have also prepared a potential fix with tests covering the affected cases and would be happy to submit a PR for review.
Activity
CodeAvk commentedon Nov 15, 2026
I tested this on Windows 11 with Python 3.13 (default encoding: cp1252) on the current
devbranch.Apart from the
UnicodeDecodeError, there's another issue where UTF-8 config files containing characters like "Größe" are read incorrectly as "Größe", without throwing any errors. Writing also fails when usingensure_ascii=Falseorallow_unicode=True.I'd like to fix this by explicitly setting
encoding="utf-8"in the functions mentioned above, along with the other files written byinit_bundle. I'll also add tests that simulate a cp1252 default encoding, so the issue can be caught even in Linux CI.Would that be okay?
ericspod commentedon Nov 15, 2026
Hi @CodeAvk please go ahead with this PR, let me know when it's ready but having reviewed it briefly it looks good to me. I allowed tests to run but I'll probably have to do that again after you've changed things so you're best off testing locally until the PR is completely ready.