langchain - 💡(How to fix) Fix Agent.save() and Chain.save() write JSON/YAML without explicit encoding='utf-8' — fails on Windows / non-utf-8 locales

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…

Agent.save() / AgentExecutor.save() in libs/langchain/langchain_classic/agents/agent.py and Chain.save() in libs/langchain/langchain_classic/chains/base.py write JSON or YAML serializations of agent/chain dicts via Path.open("w") without an explicit encoding= argument. Python falls back to locale.getpreferredencoding(), which is cp1252 on Windows and utf-8 on Linux/macOS.

Agent/chain dicts routinely contain non-ASCII content — prompts, descriptions, system messages, tool docstrings. A chain saved on Linux and re-loaded on Windows (or vice versa) can therefore produce UnicodeDecodeError or silently mangle characters when the prompt contains accented characters, mathematical notation, emoji, or CJK.

PEP 597 also recommends always specifying encoding= for text-mode opens, and Python 3.10+ emits EncodingWarning for unspecified text-mode opens under PYTHONWARNDEFAULTENCODING=1.

Error Message

Error Message and Stack Trace (if applicable)

Root Cause

Agent.save() / AgentExecutor.save() in libs/langchain/langchain_classic/agents/agent.py and Chain.save() in libs/langchain/langchain_classic/chains/base.py write JSON or YAML serializations of agent/chain dicts via Path.open("w") without an explicit encoding= argument. Python falls back to locale.getpreferredencoding(), which is cp1252 on Windows and utf-8 on Linux/macOS.

Agent/chain dicts routinely contain non-ASCII content — prompts, descriptions, system messages, tool docstrings. A chain saved on Linux and re-loaded on Windows (or vice versa) can therefore produce UnicodeDecodeError or silently mangle characters when the prompt contains accented characters, mathematical notation, emoji, or CJK.

PEP 597 also recommends always specifying encoding= for text-mode opens, and Python 3.10+ emits EncodingWarning for unspecified text-mode opens under PYTHONWARNDEFAULTENCODING=1.

Fix Action

Fix / Workaround

  • This is a bug report, not a question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Code Example

from langchain_classic.agents import AgentExecutor
# agent_executor with non-ASCII prompts / descriptions
agent_executor.save("./my_agent.json")

# Later, on a different machine (or after rebooting on Windows):
from langchain_classic.agents import load_agent
agent_executor = load_agent("./my_agent.json")  # UnicodeDecodeError on Windows / cp1252
RAW_BUFFERClick to expand / collapse

Checked other resources

  • This is a bug report, not a question. For questions, please use the LangChain Forum (https://forum.langchain.com/).
  • I added a clear and descriptive title that summarizes this issue.
  • I used the GitHub search to find a similar question and didn't find it.
  • I am sure that this is a bug in LangChain rather than my code.
  • The bug is not resolved by updating to the latest stable version of LangChain (or the specific integration package).

Example Code

from langchain_classic.agents import AgentExecutor
# agent_executor with non-ASCII prompts / descriptions
agent_executor.save("./my_agent.json")

# Later, on a different machine (or after rebooting on Windows):
from langchain_classic.agents import load_agent
agent_executor = load_agent("./my_agent.json")  # UnicodeDecodeError on Windows / cp1252

Error Message and Stack Trace (if applicable)

UnicodeDecodeError: 'charmap' codec can't decode byte 0x9d in position N: character maps to <undefined> (reproduces on Windows when the saved JSON contains non-ASCII content)

Description

Agent.save() / AgentExecutor.save() in libs/langchain/langchain_classic/agents/agent.py and Chain.save() in libs/langchain/langchain_classic/chains/base.py write JSON or YAML serializations of agent/chain dicts via Path.open("w") without an explicit encoding= argument. Python falls back to locale.getpreferredencoding(), which is cp1252 on Windows and utf-8 on Linux/macOS.

Agent/chain dicts routinely contain non-ASCII content — prompts, descriptions, system messages, tool docstrings. A chain saved on Linux and re-loaded on Windows (or vice versa) can therefore produce UnicodeDecodeError or silently mangle characters when the prompt contains accented characters, mathematical notation, emoji, or CJK.

PEP 597 also recommends always specifying encoding= for text-mode opens, and Python 3.10+ emits EncodingWarning for unspecified text-mode opens under PYTHONWARNDEFAULTENCODING=1.

Proposed fix

Add encoding="utf-8" to 6 save_path.open("w") call sites:

  • libs/langchain/langchain_classic/agents/agent.py — 4 sites in Agent.save() and AgentExecutor.save() (JSON and YAML branches)
  • libs/langchain/langchain_classic/chains/base.py — 2 sites in Chain.save() (JSON and YAML branches)

No behaviour change on utf-8 systems; eliminates locale-dependent breakage everywhere else. Happy to send the PR.

System Info

System Information

  • Affects all platforms (Windows / Linux / macOS) but bug only triggers on non-utf-8 locales (primarily Windows cp1252).
  • Reproducible against current master.

Package Information

  • langchain (libs/langchain) — langchain_classic.agents.agent, langchain_classic.chains.base

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING