Skip to content

Commit 8966e4b

Browse files
fix: add encoding='utf-8' to open() calls for non-English environments
- Add encoding='utf-8' to chat_completion_client_recorder.py read/write - Add encoding='utf-8' to page_logger.py write operations - Prevents UnicodeDecodeError on systems with non-UTF-8 default locale Fixes #5566
1 parent 027ecf0 commit 8966e4b

2 files changed

Lines changed: 5 additions & 5 deletions

File tree

python/packages/autogen-ext/src/autogen_ext/experimental/task_centric_memory/utils/chat_completion_client_recorder.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ def __init__(
7373
# Load the previously recorded messages and responses from disk.
7474
self.logger.info("Replay mode enabled.\nRetrieving session from: " + self.session_file_path)
7575
try:
76-
with open(self.session_file_path, "r") as f:
76+
with open(self.session_file_path, "r", encoding="utf-8") as f:
7777
self.records = json.load(f)
7878
except Exception as e:
7979
error_str = f"\nFailed to load recorded session: '{self.session_file_path}': {e}"
@@ -211,7 +211,7 @@ def finalize(self) -> None:
211211
# Create the directory if it doesn't exist.
212212
os.makedirs(os.path.dirname(self.session_file_path), exist_ok=True)
213213
# Write the records to disk.
214-
with open(self.session_file_path, "w") as f:
214+
with open(self.session_file_path, "w", encoding="utf-8") as f:
215215
json.dump(self.records, f, indent=2)
216216
self.logger.info("\nRecorded session was saved to: " + self.session_file_path)
217217
except Exception as e:

python/packages/autogen-ext/src/autogen_ext/experimental/task_centric_memory/utils/page_logger.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ def finalize(self) -> None:
117117
# Write the hash and other details to a file.
118118
hash_str, num_files, num_subdirs = hash_directory(self.log_dir)
119119
hash_path = os.path.join(self.log_dir, "hash.txt")
120-
with open(hash_path, "w") as f:
120+
with open(hash_path, "w", encoding="utf-8") as f:
121121
f.write(hash_str)
122122
f.write("\n")
123123
f.write("{} files\n".format(num_files))
@@ -386,7 +386,7 @@ def flush(self, finished: bool = False) -> None:
386386
return
387387
# Create a call tree of the log.
388388
call_tree_path = os.path.join(self.log_dir, self.name + ".html")
389-
with open(call_tree_path, "w") as f:
389+
with open(call_tree_path, "w", encoding="utf-8") as f:
390390
f.write(_html_opening("0 Call Tree", finished=finished))
391391
f.write(f"<h3>{self.name}</h3>")
392392
f.write("\n")
@@ -498,7 +498,7 @@ def flush(self) -> None:
498498
Writes the HTML page to disk.
499499
"""
500500
page_path = os.path.join(self.page_logger.log_dir, self.index_str + ".html")
501-
with open(page_path, "w") as f:
501+
with open(page_path, "w", encoding="utf-8") as f:
502502
f.write(_html_opening(self.file_title, finished=self.finished))
503503
f.write(f"<h3>{self.file_title}</h3>\n")
504504
for line in self.lines:

0 commit comments

Comments
 (0)