Skip to main content

LangChain v0.3

Last updated: 09.13.24

What's changedโ€‹

  • All packages have been upgraded from Pydantic 1 to Pydantic 2 internally. Use of Pydantic 2 in user code is fully supported with all packages without the need for bridges like langchain_core.pydantic_v1 or pydantic.v1.
  • Pydantic 1 will no longer be supported as it reached its end-of-life in June 2024.
  • Python 3.8 will no longer be supported as its end-of-life is October 2024.

These are the only breaking changes.

Whatโ€™s newโ€‹

The following features have been added during the development of 0.2.x:

  • Moved more integrations from langchain-community to their own langchain-x packages. This is a non-breaking change, as the legacy implementations are left in langchain-community and marked as deprecated. This allows us to better manage the dependencies of, test, and version these integrations. You can see all the latest integration packages in the API reference.
  • Simplified tool definition and usage. Read more here.
  • Added utilities for interacting with chat models: universal model constructor, rate limiter, message utilities,
  • Added the ability to dispatch custom events.
  • Revamped integration docs and API reference. Read more here.
  • Marked as deprecated a number of legacy chains and added migration guides for all of them. These are slated for removal in langchain 1.0.0. See the deprecated chains and associated migration guides here.

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.

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โ€‹

PackageLatestRecommended constraint
langchain0.3.0>=0.3,<0.4
langchain-community0.3.0>=0.3,<0.4
langchain-text-splitters0.3.0>=0.3,<0.4
langchain-core0.3.0>=0.3,<0.4
langchain-experimental0.3.0>=0.3,<0.4

Downstream packagesโ€‹

PackageLatestRecommended constraint
langgraph0.2.20>=0.2.20,<0.3
langserve0.3.0>=0.3,<0.4

Integration packagesโ€‹

PackageLatestRecommended constraint
langchain-ai210.2.0>=0.2,<0.3
langchain-aws0.2.0>=0.2,<0.3
langchain-anthropic0.2.0>=0.2,<0.3
langchain-astradb0.4.1>=0.4.1,<0.5
langchain-azure-dynamic-sessions0.2.0>=0.2,<0.3
langchain-box0.2.0>=0.2,<0.3
langchain-chroma0.1.4>=0.1.4,<0.2
langchain-cohere0.3.0>=0.3,<0.4
langchain-elasticsearch0.3.0>=0.3,<0.4
langchain-exa0.2.0>=0.2,<0.3
langchain-fireworks0.2.0>=0.2,<0.3
langchain-groq0.2.0>=0.2,<0.3
langchain-google-community2.0.0>=2,<3
langchain-google-genai2.0.0>=2,<3
langchain-google-vertexai2.0.0>=2,<3
langchain-huggingface0.1.0>=0.1,<0.2
langchain-milvus0.1.6>=0.1.6,<0.2
langchain-mistralai0.2.0>=0.2,<0.3
langchain-mongodb0.2.0>=0.2,<0.3
langchain-nomic0.1.3>=0.1.3,<0.2
langchain-ollama0.2.0>=0.2,<0.3
langchain-openai0.2.0>=0.2,<0.3
langchain-pinecone0.2.0>=0.2,<0.3
langchain-postgres0.0.13>=0.0.13,<0.1
langchain-prompty0.1.0>=0.1,<0.2
langchain-redis0.1.0>=0.1,<0.2
langchain-qdrant0.1.4>=0.1.4,<0.2
langchain-together0.2.0>=0.2,<0.3
langchain-unstructured0.1.4>=0.1.4,<0.2
langchain-voyageai0.2.0>=0.2,<0.3
langchain-weaviate0.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:

  • If your code depends on Pydantic aside from LangChain, you will need to upgrade your pydantic version constraints to be pydantic>=2,<3. See Pydanticโ€™s migration guide for help migrating your non-LangChain code to Pydantic v2 if you use pydantic v1.
  • There are a number of side effects to LangChain components caused by the internal switch from Pydantic v1 to v2. We have listed some of the common cases below together with the recommended solutions.

Common issues when transitioning to Pydantic 2โ€‹

1. Do not use the langchain_core.pydantic_v1 namespaceโ€‹

Replace any usage of langchain_core.pydantic_v1 or langchain.pydantic_v1 with direct imports from pydantic.

For example,

from langchain_core.pydantic_v1 import BaseModel

to:

from pydantic import BaseModel

This may require you to make additional updates to your Pydantic code given that there are a number of breaking changes in Pydantic 2. See the Pydantic Migration for how to upgrade your code from Pydantic 1 to 2.

2. Passing Pydantic objects to LangChain APIsโ€‹

Users using the following APIs:

  • BaseChatModel.bind_tools
  • BaseChatModel.with_structured_output
  • Tool.from_function
  • StructuredTool.from_function

should ensure that they are passing Pydantic 2 objects to these APIs rather than Pydantic 1 objects (created via the pydantic.v1 namespace of pydantic 2).

caution

While v1 objets may be accepted by some of these APIs, users are advised to use Pydantic 2 objects to avoid future issues.

3. Sub-classing LangChain modelsโ€‹

Any sub-classing from existing LangChain models (e.g., BaseTool, BaseChatModel, LLM) should upgrade to use Pydantic 2 features.

For example, any user code that's relying on Pydantic 1 features (e.g., validator) should be updated to the Pydantic 2 equivalent (e.g., field_validator), and any references to pydantic.v1, langchain_core.pydantic_v1, langchain.pydantic_v1 should be replaced with imports from pydantic.

from pydantic.v1 import validator, Field # if pydantic 2 is installed
# from pydantic import validator, Field # if pydantic 1 is installed
# from langchain_core.pydantic_v1 import validator, Field
# from langchain.pydantic_v1 import validator, Field

class CustomTool(BaseTool): # BaseTool is v1 code
x: int = Field(default=1)

def _run(*args, **kwargs):
return "hello"

@validator('x') # v1 code
@classmethod
def validate_x(cls, x: int) -> int:
return 1

Should change to:

from pydantic import Field, field_validator # pydantic v2
from langchain_core.pydantic_v1 import BaseTool

class CustomTool(BaseTool): # BaseTool is v1 code
x: int = Field(default=1)

def _run(*args, **kwargs):
return "hello"

@field_validator('x') # v2 code
@classmethod
def validate_x(cls, x: int) -> int:
return 1


CustomTool(
name='custom_tool',
description="hello",
x=1,
)

4. model_rebuild()โ€‹

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.

from langchain_core.output_parsers import BaseOutputParser


class FooParser(BaseOutputParser):
...
API Reference:BaseOutputParser

New code:

from typing import Optional as Optional

from langchain_core.output_parsers import BaseOutputParser

class FooParser(BaseOutputParser):
...

FooParser.model_rebuild()
API Reference:BaseOutputParser

Migrate using langchain-cliโ€‹

The langchain-cli can help update deprecated LangChain imports in your code automatically.

Please note that the langchain-cli only handles deprecated LangChain imports and cannot help to upgrade your code from pydantic 1 to pydantic 2.

For help with the Pydantic 1 to 2 migration itself please refer to the Pydantic Migration Guidelines.

As of 0.0.31, the langchain-cli relies on gritql for applying code mods.

Installationโ€‹

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.

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

# 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โ€‹

# 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]

Was this page helpful?


You can also leave detailed feedback on GitHub.