Skip to content

[Bug] UnicodeDecodeError on Windows due to missing encoding="utf-8" in open() call #2660

Closed

Description

@umar-anzar

While importing unsloth on windows, I get

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d...

>>> import unsloth



🦥 Unsloth: Will patch your computer to enable 2x faster free finetuning.
🦥 Unsloth Zoo will now patch everything to make training faster!
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth\__init__.py", line 247, in <module>
    from .models import *
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth\models\__init__.py", line 15, in <module>
    from .llama     import FastLlamaModel
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth\models\llama.py", line 2757, in <module>
    PatchFastRL(FastLanguageModel = FastLlamaModel)
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth\models\rl.py", line 810, in PatchFastRL
    patch_trl_rl_trainers()
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth\models\rl.py", line 803, in patch_trl_rl_trainers
    _patch_trl_rl_trainers(trainer)
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth\models\rl.py", line 597, in _patch_trl_rl_trainers
    created_module = create_new_function(
  File "D:\diskA\git-repo\Hoopoes\unsloth-train\.venv\lib\site-packages\unsloth_zoo\compiler.py", line 346, in create_new_function
    with open(function_location, "r") as f: file_source = f.read()
  File "C:\Users\umar-anzar\AppData\Roaming\uv\python\cpython-3.10.17-windows-x86_64-none\lib\encodings\cp1252.py", line 23, in decode
    return codecs.charmap_decode(input,self.errors,decoding_table)[0]
UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position 54390: character maps to <undefined>

Root Cause:

In unsloth_zoo/compiler.py, the open() function is used without specifying encoding="utf-8".

Temporary Workaround:

This was resolved by setting the following environment variable via the Windows System Settings (not via Python):

PYTHONUTF8=1

Kindly handle UTF-8 encoding in file reads so we don't have to rely on setting PYTHONUTF8=1 as a workaround.

Activity

  1. shikage commented on Jun 18, 2025

    @shikage

    I can confirm I am getting the same error on a Windows 11 system.

  2. erikzimmermann commented on Jun 21, 2025

    @erikzimmermann

    Getting the same error on Windows 11. Adding the env variable fixed it for me.

  3. abdullahsultan commented on Jul 15, 2025

    @abdullahsultan

    Also getting this annoying error Win11, that temp solution worked for me. Thanks

  4. danielhanchen commented on Jun 18, 2026

    @danielhanchen
    Member

    This should be fixed now - sorry on the delay - please update with uv pip install --upgrade unsloth unsloth_zoo --force-reinstall --no-cache-dir - if it's not fixed yet, it'll be in the next pip / PyPI release - if it's still not working after that, please open another issue - appreciate it again!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions