*   [](/v0.2/)
*   [How-to guides](/v0.2/docs/how_to/)
*   How to use LangChain with different Pydantic versions

On this page

How to use LangChain with different Pydantic versions
=====================================================

*   Pydantic v2 was released in June, 2023 ([https://docs.pydantic.dev/2.0/blog/pydantic-v2-final/](https://docs.pydantic.dev/2.0/blog/pydantic-v2-final/))
*   v2 contains has a number of breaking changes ([https://docs.pydantic.dev/2.0/migration/](https://docs.pydantic.dev/2.0/migration/))
*   Pydantic v2 and v1 are under the same package name, so both versions cannot be installed at the same time

LangChain Pydantic migration plan[​](#langchain-pydantic-migration-plan "Direct link to LangChain Pydantic migration plan")
---------------------------------------------------------------------------------------------------------------------------

As of `langchain>=0.0.267`, LangChain will allow users to install either Pydantic V1 or V2.

*   Internally LangChain will continue to [use V1](https://docs.pydantic.dev/latest/migration/#continue-using-pydantic-v1-features).
*   During this time, users can pin their pydantic version to v1 to avoid breaking changes, or start a partial migration using pydantic v2 throughout their code, but avoiding mixing v1 and v2 code for LangChain (see below).

User can either pin to pydantic v1, and upgrade their code in one go once LangChain has migrated to v2 internally, or they can start a partial migration to v2, but must avoid mixing v1 and v2 code for LangChain.

Below are two examples of showing how to avoid mixing pydantic v1 and v2 code in the case of inheritance and in the case of passing objects to LangChain.

**Example 1: Extending via inheritance**

**YES**

    from pydantic.v1 import root_validator, validatorfrom langchain_core.tools import BaseToolclass 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    CustomTool(    name='custom_tool',    description="hello",    x=1,)

**API Reference:**[BaseTool](https://api.python.langchain.com/en/latest/tools/langchain_core.tools.BaseTool.html)

Mixing Pydantic v2 primitives with Pydantic v1 primitives can raise cryptic errors

**NO**

    from pydantic import Field, field_validator # pydantic v2from langchain_core.tools import BaseToolclass 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,)

**API Reference:**[BaseTool](https://api.python.langchain.com/en/latest/tools/langchain_core.tools.BaseTool.html)

**Example 2: Passing objects to LangChain**

**YES**

    from langchain_core.tools import Toolfrom pydantic.v1 import BaseModel, Field # <-- Uses v1 namespaceclass CalculatorInput(BaseModel):    question: str = Field()Tool.from_function( # <-- tool uses v1 namespace    func=lambda question: 'hello',    name="Calculator",    description="useful for when you need to answer questions about math",    args_schema=CalculatorInput)

**API Reference:**[Tool](https://api.python.langchain.com/en/latest/tools/langchain_core.tools.Tool.html)

**NO**

    from langchain_core.tools import Toolfrom pydantic import BaseModel, Field # <-- Uses v2 namespaceclass CalculatorInput(BaseModel):    question: str = Field()Tool.from_function( # <-- tool uses v1 namespace    func=lambda question: 'hello',    name="Calculator",    description="useful for when you need to answer questions about math",    args_schema=CalculatorInput)

**API Reference:**[Tool](https://api.python.langchain.com/en/latest/tools/langchain_core.tools.Tool.html)

[Edit this page](https://github.com/langchain-ai/langchain/edit/master/docs/docs/how_to/pydantic_compatibility.md)

* * *

#### Was this page helpful?

  

#### You can also leave detailed feedback [on GitHub](https://github.com/langchain-ai/langchain/issues/new?assignees=&labels=03+-+Documentation&projects=&template=documentation.yml&title=DOC%3A+%3CPlease+write+a+comprehensive+title+after+the+%27DOC%3A+%27+prefix%3E).

[

Previous

How to use the Parent Document Retriever

](/v0.2/docs/how_to/parent_document_retriever/)[

Next

How to add chat history

](/v0.2/docs/how_to/qa_chat_history_how_to/)

*   [LangChain Pydantic migration plan](#langchain-pydantic-migration-plan)