From 3642f5917c2072ab6e990eede7c6ef1b2b82b8ce Mon Sep 17 00:00:00 2001 From: Yijia-Xiao Date: Sun, 15 Mar 2026 16:43:37 +0000 Subject: [PATCH] fix: add explicit UTF-8 encoding to all file open() calls Prevents UnicodeEncodeError on Windows where the default encoding (cp1252/gbk) cannot handle Unicode characters in LLM output. Closes #77, closes #114, closes #126, closes #215, closes #332 --- cli/main.py | 8 ++++---- tradingagents/graph/trading_graph.py | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/cli/main.py b/cli/main.py index fb97d189..adda48fc 100644 --- a/cli/main.py +++ b/cli/main.py @@ -462,7 +462,7 @@ def update_display(layout, spinner_text=None, stats_handler=None, start_time=Non def get_user_selections(): """Get all user selections before starting the analysis display.""" # Display ASCII art welcome message - with open("./cli/static/welcome.txt", "r") as f: + with open("./cli/static/welcome.txt", "r", encoding="utf-8") as f: welcome_ascii = f.read() # Create welcome box content @@ -948,7 +948,7 @@ def run_analysis(): func(*args, **kwargs) timestamp, message_type, content = obj.messages[-1] content = content.replace("\n", " ") # Replace newlines with spaces - with open(log_file, "a") as f: + with open(log_file, "a", encoding="utf-8") as f: f.write(f"{timestamp} [{message_type}] {content}\n") return wrapper @@ -959,7 +959,7 @@ def run_analysis(): func(*args, **kwargs) timestamp, tool_name, args = obj.tool_calls[-1] args_str = ", ".join(f"{k}={v}" for k, v in args.items()) - with open(log_file, "a") as f: + with open(log_file, "a", encoding="utf-8") as f: f.write(f"{timestamp} [Tool Call] {tool_name}({args_str})\n") return wrapper @@ -972,7 +972,7 @@ def run_analysis(): content = obj.report_sections[section_name] if content: file_name = f"{section_name}.md" - with open(report_dir / file_name, "w") as f: + with open(report_dir / file_name, "w", encoding="utf-8") as f: f.write(content) return wrapper diff --git a/tradingagents/graph/trading_graph.py b/tradingagents/graph/trading_graph.py index 7944e5f5..c7ef0f98 100644 --- a/tradingagents/graph/trading_graph.py +++ b/tradingagents/graph/trading_graph.py @@ -260,6 +260,7 @@ class TradingAgentsGraph: with open( f"eval_results/{self.ticker}/TradingAgentsStrategy_logs/full_states_log_{trade_date}.json", "w", + encoding="utf-8", ) as f: json.dump(self.log_states_dict, f, indent=4)