Python: Read and write chat history files as UTF-8 - #14445
Open
Kumaresan-AI-Engineer wants to merge 1 commit into
Open
Kumaresan-AI-Engineer wants to merge 1 commit into
Kumaresan-AI-Engineer wants to merge 1 commit into
Conversation
ChatHistory.store_chat_history_to_file and load_chat_history_from_file opened the file in text mode without an encoding, so they used the locale encoding. On Windows that is cp1252, and storing a history with non-ASCII content raised UnicodeEncodeError; a history written as UTF-8 elsewhere also failed to load. Both now pass encoding="utf-8", matching how the rest of the package reads text files (KernelPlugin.from_directory and KernelFunctionFromPrompt.from_directory default to utf-8). Adds a regression test that round-trips a history with Chinese, accented and emoji content, which fails on the Windows CI legs before this change.
Kumaresan-AI-Engineer
had a problem deploying
to
github-app-auth
September 13, 2026 17:21 — with
GitHub Actions
Failure
Kumaresan-AI-Engineer
had a problem deploying
to
github-app-auth
September 13, 2026 17:21 — with
GitHub Actions
Failure
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
The UTF-8 changes are covered by regression testing, with no unresolved review comments.
Pull request overview
Updates chat history file I/O to use UTF-8 consistently across platforms.
Changes:
- Uses UTF-8 when reading and writing chat history files.
- Adds regression coverage for non-ASCII content.
File summaries
| File | Summary |
|---|---|
python/tests/unit/contents/test_chat_history.py |
Tests non-ASCII chat history round-tripping. |
python/semantic_kernel/contents/chat_history.py |
Applies UTF-8 encoding to file reads and writes. |
Review details
- Files reviewed: 2/2 changed files
- Comments generated: 0
- Review effort level: Lite
💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.
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.
Motivation and Context
ChatHistory.store_chat_history_to_file and load_chat_history_from_file were opening files in text mode without specifying an encoding. This meant Python used the system's default locale encoding, which is typically cp1252 on Windows.
As a result, saving chat history containing non-ASCII characters (such as Chinese characters, accented characters, or emojis) could raise a UnicodeEncodeError. Similarly, chat history files written as UTF-8 could fail to load correctly on Windows.
This change makes both methods explicitly use UTF-8, which is also consistent with how other parts of the package handle text files, such as KernelPlugin.from_directory and KernelFunctionFromPrompt.from_directory.
Description
Explicitly use encoding="utf-8" when saving chat history.
Explicitly use encoding="utf-8" when loading chat history.
Added a regression test that saves and loads chat history containing Chinese, accented, and emoji characters.
The regression test ensures that chat history can be safely round-tripped across platforms, including Windows.