Skip to content

[Bugfix] UnicodeEncodeError in backend_api.py on Windows (cp1252) #3275

@gugabendin

Description

@gugabendin

Bug description
I encountered a UnicodeEncodeError when uploading a file containing special characters (e.g., \u02dd) while running the GUI on Windows.

Windows defaults to cp1252 encoding, which causes a crash when writing non-standard characters to the .md or .txt files in the upload_files function.

Traceback

File "...\g4f\gui\server\backend_api.py", line 444, in upload_files
    f.write(f"{result}\n")
File "encodings\cp1252.py", line 19, in encode
UnicodeEncodeError: 'charmap' codec can't encode character '\u02dd'...

The Fix I fixed this by enforcing utf-8 encoding when opening files for writing in g4f/gui/server/backend_api.py.

In backend_api.py, inside the upload_files function:

Change 1 (approx line 432):
# Before
with open(os.path.join(bucket_dir, f"{filename}.md"), 'w') as f:

# After (Fix)
with open(os.path.join(bucket_dir, f"{filename}.md"), 'w', encoding="utf-8") as f:

Change 2 (approx line 461):
# Before
with open(os.path.join(bucket_dir, "files.txt"), 'w') as f:

# After (Fix)
with open(os.path.join(bucket_dir, "files.txt"), 'w', encoding="utf-8") as f:

This ensures compatibility with special characters on Windows systems.

Metadata

Metadata

Assignees

Labels

bugSomething isn't workingstale

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions