Skip to content

fix(environments): pin PythonEnvironment._write_to_file encoding to UTF-8#2827

Merged
vvlrff merged 6 commits into
ag2ai:mainfrom
genisis0x:fix/python-env-write-utf8
May 22, 2026
Merged

fix(environments): pin PythonEnvironment._write_to_file encoding to UTF-8#2827
vvlrff merged 6 commits into
ag2ai:mainfrom
genisis0x:fix/python-env-write-utf8

Conversation

@genisis0x
Copy link
Copy Markdown
Contributor

Description

PythonEnvironment._write_to_file (autogen/environments/python_environment.py:90) 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. 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.

$ pytest test/environments/test_python_environment_utf8.py -v
test_python_environment_write_to_file_pins_utf8 PASSED

Checklist

AI Disclosure

  • No AI tools were used
  • AI tools were used (details below)

…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.
Copy link
Copy Markdown
Collaborator

@marklysze marklysze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Collaborator

@marklysze marklysze left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

qingyun-wu and others added 4 commits May 16, 2026 22:05
`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.
@vvlrff vvlrff enabled auto-merge May 21, 2026 20:27
Remove regression comment regarding UTF-8 encoding handling in PythonEnvironment._write_to_file.
@vvlrff vvlrff disabled auto-merge May 21, 2026 20:28
@codecov
Copy link
Copy Markdown

codecov Bot commented May 21, 2026

Codecov Report

❌ Patch coverage is 0% with 1 line in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
autogen/environments/python_environment.py 0.00% 1 Missing ⚠️
Files with missing lines Coverage Δ
autogen/environments/python_environment.py 51.16% <0.00%> (ø)

... and 118 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@vvlrff vvlrff added this pull request to the merge queue May 22, 2026
Merged via the queue into ag2ai:main with commit 1400a5c May 22, 2026
19 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.

4 participants