LangChain v0.2
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, as well as a list of deprecations and breaking changes.
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.@langchain/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
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 doesn't 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 | import { UpstashVectorStore } from "langchain/vectorstores/upstash" | import { UpstashVectorStore } from "@langchain/community/vectorstores/upstash" |
@langchain/community | @langchain/openai | import { ChatOpenAI } from "@langchain/community/chat_models/openai" | import { ChatOpenAI } from "@langchain/openai" |
langchain | @langchain/core | import { Document } from "langchain/schema/document" | import { Document } from "@langchain/core/documents" |
langchain | @langchain/textsplitters | import { RecursiveCharacterTextSplitter } from "langchain/text_splitter" | import { RecursiveCharacterTextSplitter } from "@langchain/textsplitters" |
Deprecation timelineβ
We have two main types of deprecations:
- Code that was moved from
langchain
into another package (e.g,@langchain/community
)
If you try to import it from langchain
, it will fail since the entrypoint has been removed.
- Code that has better alternatives available and will eventually be removed, so there's only a single way to do things. (e.g.,
predictMessages
method in ChatModels has been deprecated in favor ofinvoke
).
Many of these were marked for removal in 0.2. We have bumped the removal to 0.3.
Installationβ
The 0.2.X migration script is only available in version 0.0.14-rc.1
or later.
- npm
- Yarn
- pnpm
npm i @langchain/scripts@0.0.14-rc.1
yarn add @langchain/scripts@0.0.14-rc.1
pnpm add @langchain/scripts@0.0.14-rc.1
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
).
For example, say your code still uses import ChatOpenAI from "@langchain/community/chat_models/openai";
:
Invoking the migration script will replace this import with import ChatOpenAI from "@langchain/openai";
.
import { updateEntrypointsFrom0_x_xTo0_2_x } from "@langchain/scripts/migrations";
const pathToMyProject = "..."; // This path is used in the following glob pattern: `${projectPath}/**/*.{ts,tsx,js,jsx}`.
updateEntrypointsFrom0_x_xTo0_2_x({
projectPath: pathToMyProject,
shouldLog: true,
});
Other optionsβ
updateEntrypointsFrom0_x_xTo0_2_x({
projectPath: pathToMyProject,
tsConfigPath: "tsconfig.json", // Path to the tsConfig file. This will be used to load all the project files into the script.
testRun: true, // If true, the script will not save any changes, but will log the changes that would be made.
files: ["..."], // A list of .ts file paths to check. If this is provided, the script will only check these files.
});