Skip to content

fix: read and write program source and prompt templates as UTF-8 - #618

Merged
codelion merged 1 commit into
algorithmicsuperintelligence:mainfrom
xiao98:fix-controller-utf8-programs
Nov 25, 2026
Merged

codelion merged 1 commit into
algorithmicsuperintelligence:mainfrom
xiao98:fix-controller-utf8-programs

Conversation

@xiao98

@xiao98 xiao98 commented Nov 25, 2026 •

Copy link
Copy Markdown
Contributor

Problem

The evaluator writes each candidate program to disk as UTF-8 (program_code.encode("utf-8")), but the controller and TemplateManager open the same kind of text with the locale's default encoding:

  • controller.py: reading the initial program and island_initial_programs, writing best/best_program.* and checkpoint_N/best_program.*
  • prompt/templates.py: reading the .txt templates and fragments.json

Under a non-UTF-8 locale this breaks in two ways.

1. OpenEvolve cannot start. The default fragments.json contains → (U+2192) in fitness_improved / fitness_declined. On Windows with a GBK code page (the default for Chinese-language systems), constructing OpenEvolve fails:

File "openevolve/prompt/templates.py", line 214, in _load_from_directory
    loaded_fragments = json.load(f)
UnicodeDecodeError: 'gbk' codec can't decode byte 0x92 in position 57: illegal multibyte sequence

With cp1252 the bytes decode without an error, so the arrow reaches the LLM prompt as mojibake instead.

2. Programs with non-ASCII text are misread or cannot be saved. An initial program with a non-ASCII comment or string fails to load (or loads as mojibake that is then evolved):

File "openevolve/controller.py", line 335, in _load_initial_program
    return f.read()
UnicodeDecodeError: 'gbk' codec can't decode byte 0xad in position 4: illegal multibyte sequence

and a best program containing a character the locale cannot encode raises UnicodeEncodeError when the checkpoint or the final best program is written, after the evolution has already run.

Fix

Pass encoding="utf-8" to these six open() calls, matching what the evaluator already does for candidate programs.

Other text files opened without an encoding (e.g. api.py, coevolution.py, config YAML) are left for separate changes to keep this one small.

Tests

tests/test_controller_utf8_programs.py follows test_cli_utf8.py: it runs a real OpenEvolve in a subprocess under a non-UTF-8 locale (LC_ALL=C, PYTHONUTF8=0; skipped if the platform still reports UTF-8) and checks that the initial program and an island seed load unchanged, and that both best_program files are valid UTF-8 with the original text. It fails on main with the errors above.

On a Windows machine with a GBK code page, this change also takes the existing suite from 111 errors to 48; the remaining ones I looked at come from other files opened without an encoding (e.g. the evaluator script written by api.py) and from tests that assume POSIX paths.

The evaluator writes candidate programs as UTF-8, but the controller read
the initial program and island seeds, and wrote best_program files, in
the locale's encoding, and TemplateManager read the prompt templates the
same way. The default fragments.json contains U+2192, so under a locale
that cannot decode it (e.g. GBK on Windows) OpenEvolve failed in its
constructor, and a best program with non-ASCII text could not be saved
under an ASCII or cp1252 locale.

Use UTF-8 for these files regardless of locale.
@codelion
codelion merged commit 003f2f5 into algorithmicsuperintelligence:main Nov 25, 2026
5 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.

2 participants