As of langchain>=0.0.267
, LangChain will allow users to install either Pydantic V1 or V2.
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, validator
class CustomTool(BaseTool):
x: int = Field(default=1)
def _run(*args, **kwargs):
return "hello"
@validator('x')
@classmethod
def validate_x(cls, x: int) -> int:
return 1
CustomTool(
name='custom_tool',
description="hello",
x=1,
)
Mixing Pydantic v2 primitives with Pydantic v1 primitives can raise cryptic errors
NO
from pydantic import Field, field_validator
class CustomTool(BaseTool):
x: int = Field(default=1)
def _run(*args, **kwargs):
return "hello"
@field_validator('x')
@classmethod
def validate_x(cls, x: int) -> int:
return 1
CustomTool(
name='custom_tool',
description="hello",
x=1,
)
Example 2: Passing objects to LangChain
YES
from langchain_core.tools import Tool
from pydantic.v1 import BaseModel, Field
class CalculatorInput(BaseModel):
question: str = Field()
Tool.from_function(
func=lambda question: 'hello',
name="Calculator",
description="useful for when you need to answer questions about math",
args_schema=CalculatorInput
)
NO
from langchain_core.tools import Tool
from pydantic import BaseModel, Field
class CalculatorInput(BaseModel):
question: str = Field()
Tool.from_function(
func=lambda question: 'hello',
name="Calculator",
description="useful for when you need to answer questions about math",
args_schema=CalculatorInput
)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4