Migration
LangChain v0.2 was released in May 2024. This release includes a number of breaking changes and deprecations. This document contains a guide on upgrading to 0.2.x.
Migration
This documentation will help you upgrade your code to LangChain 0.2.x.
. To prepare for migration, we first recommend you take the following steps:
- Install the 0.2.x versions of langchain-core, langchain and upgrade to recent versions of other packages that you may be using. (e.g. langgraph, langchain-community, langchain-openai, etc.)
- Verify that your code runs properly with the new packages (e.g., unit tests pass).
- Install a recent version of
langchain-cli
, and use the tool to replace old imports used by your code with the new imports. (See instructions below.) - Manually resolve any remaining deprecation warnings.
- Re-run unit tests.
- If you are using
astream_events
, please review how to migrate to astream events v2.
Upgrade to new importsβ
We created a tool to help migrate your code. This tool is still in beta and may not cover all cases, but we hope that it will help you migrate your code more quickly.
The migration script has the following limitations:
- Itβs limited to helping users move from old imports to new imports. It does not help address other deprecations.
- It canβt handle imports that involve
as
. - New imports are always placed in global scope, even if the old import that was replaced was located inside some local scope (e..g, function body).
- It will likely miss some deprecated imports.
Here is an example of the import changes that the migration script can help apply automatically:
From Package | To Package | Deprecated Import | New Import |
---|---|---|---|
langchain | langchain-community | from langchain.vectorstores import InMemoryVectorStore | from langchain_community.vectorstores import InMemoryVectorStore |
langchain-community | langchain_openai | from langchain_community.chat_models import ChatOpenAI | from langchain_openai import ChatOpenAI |
langchain-community | langchain-core | from langchain_community.document_loaders import Blob | from langchain_core.document_loaders import Blob |
langchain | langchain-core | from langchain.schema.document import Document | from langchain_core.documents import Document |
langchain | langchain-text-splitters | from langchain.text_splitter import RecursiveCharacterTextSplitter | from langchain_text_splitters import RecursiveCharacterTextSplitter |
Installationβ
pip install langchain-cli
langchain-cli --version # <-- Make sure the version is at least 0.0.22
Usageβ
Given that the migration script is not perfect, you should make sure you have a backup of your code first (e.g., using version control like git
).
You will need to run the migration script twice as it only applies one import replacement per run.
For example, say your code still uses from langchain.chat_models import ChatOpenAI
:
After the first run, youβll get: from langchain_community.chat_models import ChatOpenAI
After the second run, you βll get: from langchain_openai import ChatOpenAI
# Run a first time
# Will replace from langchain.chat_models import ChatOpenAI
langchain-cli migrate --diff [path to code] # Preview
langchain-cli migrate [path to code] # Apply
# Run a second time to apply more import replacements
langchain-cli migrate --diff [path to code] # Preview
langchain-cli migrate [path to code] # Apply
Other optionsβ
# See help menu
langchain-cli migrate --help
# Preview Changes without applying
langchain-cli migrate --diff [path to code]
# Run on code including ipython notebooks
# Apply all import updates except for updates from langchain to langchain-core
langchain-cli migrate --disable langchain_to_core --include-ipynb [path to code]