fix(environments): pin PythonEnvironment._write_to_file encoding to UTF-8#2827
Merged
Conversation
…TF-8 PythonEnvironment._write_to_file is the shared helper every concrete PythonEnvironment subclass uses to land an agent-generated script on disk before invoking subprocess. The bare 'open(script_path, "w")' inherits 'locale.getpreferredencoding(False)' — cp1252 on default Windows installs — so the first non-cp1252 character (CJK string literal, emoji, smart quote from copy-pasted documentation, PEP 3131 identifier) raises 'UnicodeEncodeError' mid-write. The script is then partially written, the subprocess runs an invalid file, and the agent sees a syntax error or truncated output instead of the intended behavior. Pin encoding="utf-8" on the open call. Adds source-level regression coverage in test/environments/test_python_environment_utf8.py that runs on every CI lane (no PythonEnvironment subclass extras required) so the kwarg cannot silently regress.
marklysze
approved these changes
May 17, 2026
Collaborator
marklysze
left a comment
There was a problem hiding this comment.
Correct — PythonEnvironment._write_to_file now pins UTF-8. Agent-generated scripts almost always include non-ASCII content (docstrings, string literals, identifiers under PEP 3131), so this fix prevents silent mid-write failures on Windows. LGTM.
marklysze
approved these changes
May 17, 2026
Collaborator
marklysze
left a comment
There was a problem hiding this comment.
Same encoding fix in PythonEnvironment._write_to_file. This is the path that writes agent-generated scripts to disk — agent output reliably contains non-ASCII (comments, string literals, CJK identifiers). Test correctly exercises the cp1252-locale code path. Approved.
This was referenced May 17, 2026
`check-license-headers` pre-commit hook flagged the new test package init as missing the standard SPDX header; add the 2023-2026 ag2 header matching the sibling test file to clear the pre-commit-check / pr-check failures.
Remove regression comment regarding UTF-8 encoding handling in PythonEnvironment._write_to_file.
Codecov Report❌ Patch coverage is
... and 118 files with indirect coverage changes 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
PythonEnvironment._write_to_file(autogen/environments/python_environment.py:90) is the shared helper every concretePythonEnvironmentsubclass uses to land an agent-generated script on disk before invokingsubprocess. The bareopen(script_path, "w")inheritslocale.getpreferredencoding(False)— cp1252 on default Windows installs — so the first non-cp1252 character (CJK string literal, emoji, smart quote from copy-pasted documentation, PEP 3131 identifier) raisesUnicodeEncodeErrormid-write. The script is then partially written, the subprocess runs an invalid file, and the agent sees a syntax error or truncated output instead of the intended behavior. Same class of bug as #1731 / PRs #2818 / #2819 / #2825 / #2826.This change pins
encoding="utf-8".Tests
Added
test/environments/test_python_environment_utf8.py— source-level regression check that asserts the kwarg appears on the call site. Runs on every CI lane.Checklist
AI Disclosure