langchain · difficulty ◆
dict() is Deprecated: Switch to asdict()
A one-line migration that keeps mypy happy and your code forward-compatible.
Stop calling .dict() before it disappears. asdict() is the one-word fix that keeps your serialization code clean and mypy quiet.
$ grep -rn "\.dict()" --include="*.py" .What it does
LangChain v1.4.2 deprecates the .dict() method across all LangChain objects (Runnable, Message, Tool, etc.) in favor of .asdict(). The old .dict() conflicted with Python’s builtin dict type annotation, causing confusion and type-checking issues in static analysis tools like mypy.
Why it matters
If you use .dict() to serialize LangChain objects (Messages, Runnable configs, tool outputs), your code will break once the deprecation period ends. Migrating to .asdict() is a one-line change with identical output, but you must update before the removal lands in a future release.
Example
$ Migrate a message serialization call{'id': 'msg-123', 'content': 'Hello world', 'additional_kwargs': {},
'response_metadata': {}, 'type': 'ai', 'usage': None}msg.asdict() returns the same shape that msg.dict() used to.
$ Inspect a Runnable configdict_keys(['id', 'lc', 'name', 'kwargs'])chain.asdict() works across Runnable objects too.
Common flags
- .asdict()
- The new method replacing deprecated .dict() everywhere
- --deprecation
- Old .dict() still works for now but warns and will be removed
History
Origin
The deprecation was introduced in langchain-core==1.4.2 (PR #31685), surfaced in the June 9 tutorial.
Why now
The conflict with the builtin dict annotation broke type inference and confused tooling, so the team standardized on asdict() across the whole codebase.
Fun facts
Pros & cons
pros
- + Removes a name clash with the dict builtin
- + Identical output, trivial migration
- + Better static-analysis ergonomics
cons
- − Old code keeps working until removal
- − Large codebases need a sweep
Takeaways
- 1Replace .dict() with .asdict() for identical output.
- 2A grep + sed sweep plus your test suite is a complete migration.
- 3Do it before the deprecation is removed to avoid breakage.