Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: show how to use langchain-cli for migration #26535

Merged
merged 4 commits into from
Sep 16, 2024
Merged
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
145 changes: 100 additions & 45 deletions docs/docs/versions/v0_3/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -23,66 +23,71 @@ The following features have been added during the development of 0.2.x:

## How to update your code

If you're using `langchain` / `langchain-community` / `langchain-core` 0.0 or 0.1, we recommend that you first [upgrade to 0.2](https://python.langchain.com/v0.2/docs/versions/v0_2/). The `langchain-cli` will help you to migrate many imports automatically.
If you're using `langchain` / `langchain-community` / `langchain-core` 0.0 or 0.1, we recommend that you first [upgrade to 0.2](https://python.langchain.com/v0.2/docs/versions/v0_2/).

If you're using `langgraph`, upgrade to `langgraph>=0.2.20,<0.3`. This will work with either 0.2 or 0.3 versions of all the base packages.

Here is a complete list of all packages that have been released and what we recommend upgrading your version constraints to.
Any package that now requires `langchain-core` 0.3 had a minor version bump.
Any package that is now compatible with both `langchain-core` 0.2 and 0.3 had a patch version bump.

You can use the `langchain-cli` to update deprecated imports automatically.
The CLI will handle updating deprecated imports that were introduced in LangChain 0.0.x and LangChain 0.1, as
well as updating the `langchain_core.pydantic_v1` and `langchain.pydantic_v1` imports.


### Base packages

| Package | Latest | Recommended constraint |
| -------------------------------------- | ------- | -------------------------- |
| langchain | 0.3.0 | >=0.3,<0.4 |
| langchain-community | 0.3.0 | >=0.3,<0.4 |
| langchain-text-splitters | 0.3.0 | >=0.3,<0.4 |
| langchain-core | 0.3.0 | >=0.3,<0.4 |
| langchain-experimental | 0.3.0 | >=0.3,<0.4 |
| Package | Latest | Recommended constraint |
|--------------------------|--------|------------------------|
| langchain | 0.3.0 | >=0.3,<0.4 |
| langchain-community | 0.3.0 | >=0.3,<0.4 |
| langchain-text-splitters | 0.3.0 | >=0.3,<0.4 |
| langchain-core | 0.3.0 | >=0.3,<0.4 |
| langchain-experimental | 0.3.0 | >=0.3,<0.4 |

### Downstream packages

| Package | Latest | Recommended constraint |
| -------------------------------------- | ------- | -------------------------- |
| langgraph | 0.2.20 | >=0.2.20,<0.3 |
| langserve | 0.3.0 | >=0.3,<0.4 |
| Package | Latest | Recommended constraint |
|-----------|--------|------------------------|
| langgraph | 0.2.20 | >=0.2.20,<0.3 |
| langserve | 0.3.0 | >=0.3,<0.4 |

### Integration packages

| Package | Latest | Recommended constraint |
| -------------------------------------- | ------- | -------------------------- |
| langchain-ai21 | 0.2.0 | >=0.2,<0.3 |
| langchain-aws | 0.2.0 | >=0.2,<0.3 |
| langchain-anthropic | 0.2.0 | >=0.2,<0.3 |
| langchain-astradb | 0.4.1 | >=0.4.1,<0.5 |
| langchain-azure-dynamic-sessions | 0.2.0 | >=0.2,<0.3 |
| langchain-box | 0.2.0 | >=0.2,<0.3 |
| langchain-chroma | 0.1.4 | >=0.1.4,<0.2 |
| langchain-cohere | 0.2.0 | >=0.2,<0.3 |
| langchain-elasticsearch | 0.3.0 | >=0.3,<0.4 |
| langchain-exa | 0.2.0 | >=0.2,<0.3 |
| langchain-fireworks | 0.2.0 | >=0.2,<0.3 |
| langchain-groq | 0.2.0 | >=0.2,<0.3 |
| langchain-google-community | 2.0.0 | >=2,<3 |
| langchain-google-genai | 2.0.0 | >=2,<3 |
| langchain-google-vertexai | 2.0.0 | >=2,<3 |
| langchain-huggingface | 0.1.0 | >=0.1,<0.2 |
| langchain-milvus | 0.1.6 | >=0.1.6,<0.2 |
| langchain-mistralai | 0.2.0 | >=0.2,<0.3 |
| langchain-mongodb | 0.2.0 | >=0.2,<0.3 |
| langchain-nomic | 0.1.3 | >=0.1.3,<0.2 |
| langchain-ollama | 0.2.0 | >=0.2,<0.3 |
| langchain-openai | 0.2.0 | >=0.2,<0.3 |
| langchain-pinecone | 0.2.0 | >=0.2,<0.3 |
| langchain-postgres | 0.0.13 | >=0.0.13,<0.1 |
| langchain-prompty | 0.1.0 | >=0.1,<0.2 |
| langchain-redis | 0.1.0 | >=0.1,<0.2 |
| langchain-qdrant | 0.2.0 | >=0.2,<0.3 |
| langchain-together | 0.2.0 | >=0.2,<0.3 |
| langchain-unstructured | 0.1.4 | >=0.1.4,<0.2 |
| langchain-voyageai | 0.2.0 | >=0.2,<0.3 |
| langchain-weaviate | 0.1.0 | >=0.1,<0.2 |
| Package | Latest | Recommended constraint |
|----------------------------------|--------|------------------------|
| langchain-ai21 | 0.2.0 | >=0.2,<0.3 |
| langchain-aws | 0.2.0 | >=0.2,<0.3 |
| langchain-anthropic | 0.2.0 | >=0.2,<0.3 |
| langchain-astradb | 0.4.1 | >=0.4.1,<0.5 |
| langchain-azure-dynamic-sessions | 0.2.0 | >=0.2,<0.3 |
| langchain-box | 0.2.0 | >=0.2,<0.3 |
| langchain-chroma | 0.1.4 | >=0.1.4,<0.2 |
| langchain-cohere | 0.2.0 | >=0.2,<0.3 |
| langchain-elasticsearch | 0.3.0 | >=0.3,<0.4 |
| langchain-exa | 0.2.0 | >=0.2,<0.3 |
| langchain-fireworks | 0.2.0 | >=0.2,<0.3 |
| langchain-groq | 0.2.0 | >=0.2,<0.3 |
| langchain-google-community | 2.0.0 | >=2,<3 |
| langchain-google-genai | 2.0.0 | >=2,<3 |
| langchain-google-vertexai | 2.0.0 | >=2,<3 |
| langchain-huggingface | 0.1.0 | >=0.1,<0.2 |
| langchain-milvus | 0.1.6 | >=0.1.6,<0.2 |
| langchain-mistralai | 0.2.0 | >=0.2,<0.3 |
| langchain-mongodb | 0.2.0 | >=0.2,<0.3 |
| langchain-nomic | 0.1.3 | >=0.1.3,<0.2 |
| langchain-ollama | 0.2.0 | >=0.2,<0.3 |
| langchain-openai | 0.2.0 | >=0.2,<0.3 |
| langchain-pinecone | 0.2.0 | >=0.2,<0.3 |
| langchain-postgres | 0.0.13 | >=0.0.13,<0.1 |
| langchain-prompty | 0.1.0 | >=0.1,<0.2 |
| langchain-redis | 0.1.0 | >=0.1,<0.2 |
| langchain-qdrant | 0.2.0 | >=0.2,<0.3 |
| langchain-together | 0.2.0 | >=0.2,<0.3 |
| langchain-unstructured | 0.1.4 | >=0.1.4,<0.2 |
| langchain-voyageai | 0.2.0 | >=0.2,<0.3 |
| langchain-weaviate | 0.1.0 | >=0.1,<0.2 |

Once you've updated to recent versions of the packages, you may need to address the following issues stemming from the internal switch from Pydantic v1 to Pydantic v2:

Expand Down Expand Up @@ -185,6 +190,8 @@ CustomTool(
When sub-classing from LangChain models, users may need to add relevant imports
to the file and rebuild the model.

You can read more about `model_rebuild` [here](https://docs.pydantic.dev/latest/concepts/models/#rebuilding-model-schema).

```python
from langchain_core.output_parsers import BaseOutputParser

Expand All @@ -205,3 +212,51 @@ class FooParser(BaseOutputParser):

FooParser.model_rebuild()
```


## Migrate using langchain-cli
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should this be in the "How to update your code" section?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think let's reference, but not move since it's just one part of how to update yoru code and doesn't handle most of the work associated with the pydantic migration

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

maybe worth making that explicit here then? that this doesn't handle pydantic migration


The `langchain-cli` can help migrate your code to new imports automatically. As of 0.0.31, the `langchain-cli` relies on [gritql](https://about.grit.io/) for applying code mods.

## Installation

```bash
pip install -U langchain-cli
langchain-cli --version # <-- Make sure the version is at least 0.0.31
```

## 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`).

The `langchain-cli` will handle the `langchain_core.pydantic_v1` deprecation introduced in LangChain 0.3 as well
as older deprecations (e.g.,`from langchain.chat_models import ChatOpenAI` which should be `from langchain_openai import ChatOpenAI`),

You will need to run the migration script **twice** as it only applies one import replacement per run.
eyurtsev marked this conversation as resolved.
Show resolved Hide resolved

For example, say that your code is still using the old import `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`

```bash
# Run a first time
# Will replace from langchain.chat_models import ChatOpenAI
langchain-cli migrate --help [path to code] # Help
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

```bash
# See help menu
langchain-cli migrate --help
# Preview Changes without applying
langchain-cli migrate --diff [path to code]
# Approve changes interactively
langchain-cli migrate --interactive [path to code]
```