Skip to content

fix(chat): read and write export/interpolate files as UTF-8 - #438

Merged
cpsievert merged 1 commit into
posit-dev:mainfrom
taekop:fix/utf8-file-encoding
Sep 28, 2026
Merged

cpsievert merged 1 commit into
posit-dev:mainfrom
taekop:fix/utf8-file-encoding

Conversation

@taekop

@taekop taekop commented Sep 24, 2026

Copy link
Copy Markdown
Contributor

Closes #437.

On a non-UTF-8 locale, open() without an explicit encoding= falls back to the platform's preferred encoding. Chat.export() and interpolate_file() both did this, so export() raised UnicodeEncodeError on non-ASCII content and interpolate_file() silently returned a mojibake prompt instead of raising. Both now pass encoding="utf-8" explicitly.

Adds regression tests for interpolate_file() and Chat.export() that monkeypatch open() to fall back to latin-1 (simulating a non-UTF-8 locale default) when no encoding is given, and assert round-trip correctness with non-ASCII content.

Chat.export_eval() and BatchJob's state file read/write open files the same way and could get the same fix in a follow-up, if maintainers want it.

Both opened files without an explicit encoding, so a non-UTF-8 locale
made export() raise and interpolate_file() return mojibake.

Fixes posit-dev#437

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Copilot review overview

🟢 Approval recommended

No unresolved blocking issues were identified.

Review effort: Lite
Findings: None

What changed in this PR

Fixes locale-dependent encoding by explicitly using UTF-8 for chat exports and prompt interpolation.

Changes:

  • Uses UTF-8 for reading and writing affected files.
  • Adds non-ASCII regression tests.
  • Documents the fix in the changelog.
File Description
tests/​test_interpolate.py Tests UTF-8 prompt reading.
tests/​test_chat.py Tests UTF-8 transcript writing.
chatlas/​_interpolate.py Reads prompt files as UTF-8.
chatlas/​_chat.py Writes exports as UTF-8.
CHANGELOG.md Documents the fix.

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@cpsievert

Copy link
Copy Markdown
Collaborator

Thanks!

@cpsievert
cpsievert merged commit de246d3 into posit-dev:main Sep 28, 2026
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.

export() raises UnicodeEncodeError and interpolate_file() returns a corrupted prompt when the locale is not UTF-8

3 participants