[BUG] `load_text` and `load_dict` read artifacts in the locale encoding instead of UTF-8
[!WARNING] Before submitting a PR, please make sure that:
- A maintainer has triaged this issue and applied the
readylabel- This issue has no assignee
- No duplicate PR exists
PRs not meeting these requirements may be automatically closed.
Issues Policy acknowledgement
- I have read and agree to submit bug reports in accordance with the issues policy
Where did you encounter this bug?
Local machine
MLflow version
Reproduced on master at 7b9f165 (3.16.2.dev0). The two affected lines are identical in the 3.16.1 release.
System information
- OS: Windows 10 (10.0.19045)
- Python: 3.11.6
locale.getpreferredencoding():cp950,PYTHONUTF8unset- Tracking: local SQLite (
sqlite:///mlflow.db), local artifact store
Describe the problem
mlflow.log_text writes the artifact as UTF-8 (open(tmp_path, "w", encoding="utf-8") in MlflowClient.log_text), but mlflow.artifacts.load_text and load_dict read it back with open(local_artifact) (mlflow/artifacts/__init__.py:188 and :222), which uses the locale encoding. Wherever that is not UTF-8, the round trip in load_text's own docstring example breaks as soon as the text is not ASCII:
- Text whose bytes happen to decode in the locale codec comes back silently wrong:
café résuméreturnscaf矇 r矇sum矇on cp950, and the same bytes read as cp1252 givecafé résumé. - Text that does not decode raises
MlflowException: Unable to form a str object from file content, with error codeBAD_REQUEST, although the request was fine and it is the reader that picked the wrong codec. load_dictraisesUnicodeDecodeErrorfromjson.load.
With python -X utf8 the same script returns all three values correctly.
Suggested fix: open(local_artifact, encoding="utf-8") on both lines, matching the writer (and JSON is UTF-8 by spec). #14248 fixed the same pattern for trace data. One trade-off: a file in the locale encoding uploaded with log_artifact currently reads back correctly on the same machine (a cp950 台北天氣 does here), and would raise after the change. If that matters, a fallback to the locale encoding when UTF-8 fails would keep it working.
Code to reproduce issue
import json, os, tempfile
os.chdir(tempfile.mkdtemp())
import mlflow
mlflow.set_tracking_uri("sqlite:///mlflow.db")
with mlflow.start_run() as run:
mlflow.log_text("café résumé", "a.txt")
mlflow.log_text("台北今天天氣如何?", "b.txt")
mlflow.log_text(json.dumps({"城市": "台北"}, ensure_ascii=False), "c.json")
base = run.info.artifact_uri
for call in (
lambda: mlflow.artifacts.load_text(f"{base}/a.txt"),
lambda: mlflow.artifacts.load_text(f"{base}/b.txt"),
lambda: mlflow.artifacts.load_dict(f"{base}/c.json"),
):
try:
print(repr(call()))
except Exception as e:
print(f"{type(e).__name__}: {e}")Output on cp950:
'caf矇 r矇sum矇'
MlflowException: Unable to form a str object from file content
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe5 in position 2: illegal multibyte sequenceOutput with python -X utf8:
'café résumé'
'台北今天天氣如何?'
{'城市': '台北'}Stack trace
From calling load_text and load_dict directly, without the try/except in the script above:
Traceback (most recent call last):
File "...\mlflow\artifacts\__init__.py", line 190, in load_text
return str(local_artifact_fd.read())
^^^^^^^^^^^^^^^^^^^^^^^^
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe5 in position 0: illegal multibyte sequence
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "raw_tb.py", line 14, in <module>
mlflow.artifacts.load_text(f"{base}/b.txt")
File "...\mlflow\artifacts\__init__.py", line 192, in load_text
raise MlflowException("Unable to form a str object from file content", BAD_REQUEST)
mlflow.exceptions.MlflowException: Unable to form a str object from file contentTraceback (most recent call last):
File "raw_tb.py", line 16, in <module>
mlflow.artifacts.load_dict(f"{base}/c.json")
File "...\mlflow\artifacts\__init__.py", line 224, in load_dict
return json.load(local_artifact_fd)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "...\Lib\json\__init__.py", line 293, in load
return loads(fp.read(),
^^^^^^^^^
UnicodeDecodeError: 'cp950' codec can't decode byte 0xe5 in position 2: illegal multibyte sequenceOther info / logs
Investigated and written by Claude Code, which also ran the scripts above on my machine against master.
Willingness to contribute
Yes. I can contribute a fix for this bug independently.
What component(s) does this bug affect?
-
area/tracking: Tracking Service, tracking client APIs, autologging
Source: mlflow/mlflow