Skip to content

Writing files in Windows crashes on certain unicode characters in cp1252 files. Specify utf-8 encoding when opening files. #68

@chipfox

Description

@chipfox

The SaveLangchain function in PrintUtils.py writes MarkdownVersion to a file. The default encoding for file writing on Windows is cp1252, which doesn't support many Unicode characters and will sometimes cause a crash.

All lines that open files need to be specified as utf-8 like this:
with open(ThisLogPathJSON, "w", encoding="utf-8") as f:
with open(ThisLogPathMD, "w", encoding="utf-8") as f:
with open(f"{self.LogDirPrefix}/Story.md", "w", encoding="utf-8") as f:

with open(ThisLogPathJSON, "w") as f:
f.write(json.dumps(_LangChain, indent=4, sort_keys=True))
# Now, Save Markdown Version
with open(ThisLogPathMD, "w") as f:
MarkdownVersion:str = f"# Debug LangChain {LangChainDebugTitle}\n**Note: '```' tags have been removed in this version.**\n"
for Message in _LangChain:
MarkdownVersion += f"\n\n\n# Role: {Message['role']}\n"
MarkdownVersion += f"```{Message['content'].replace('```', '')}```"
f.write(MarkdownVersion)
self.Log(f"Wrote This Language Chain ({LangChainDebugTitle}) To Debug File {ThisLogPathMD}", 5)
# Saves the given story to disk
def SaveStory(self, _StoryContent:str):
with open(f"{self.LogDirPrefix}/Story.md", "w") as f:

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions