-
Notifications
You must be signed in to change notification settings - Fork 15.8k
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
Unable to extend BaseChatModel with pydantic 2 #12358
Comments
🤖 Hello @nikokaoja, I'm here to assist you with your issue. I'm an AI bot designed to help with bug solving, answering questions, and guiding you to become a contributor. The If your To resolve this, ensure that you're using the same version of Pydantic as the LangChain base class you're extending. If Here's an example of how to correctly extend a base class using Pydantic v1: from pydantic.v1 import root_validator, validator
class MyChat(BaseChatModel): # BaseChatModel is v1 code
x: int = Field(default=1)
@validator('x') # v1 code
@classmethod
def validate_x(cls, x: int) -> int:
return 1 Avoid using Pydantic v2 code like this: from pydantic import Field, field_validator # pydantic v2
class MyChat(BaseChatModel): # BaseChatModel is v1 code
x: int = Field(default=1)
@field_validator('x') # v2 code
@classmethod
def validate_x(cls, x: int) -> int:
return 1 This is because mixing Pydantic v2 primitives with Pydantic v1 primitives can raise cryptic errors, such as the one you're experiencing. For more information, you can refer to the LangChain Pydantic compatibility guide and the LangChain Pydantic v1 main.py file. I hope this helps! If you have any more questions or need further clarification, feel free to ask. Best, SourcesThis response is meant to be useful and save you time. It is not meant to be a precise solution, but rather a starting point for your own research. Help me be more useful! Please leave a 👍 if this is helpful and 👎 if it is irrelevant. If you want to continue the conversation, start your reply with @dosu-bot. |
Using |
Fixed with a proper redirect to v1, as suggested: |
System Info
langchain
= 0.0.324pydantic
= 2.4.2python
= 3.11platform
= macosIn the project I have class
MyChat
which inheritsBaseChatModel
and further customize/extends it. Adding additional fields works fine with both pydantic v1 and v2, however adding a field and/or model validator fails with pydantic v2, raisingTypeError
:Full error:
Who can help?
No response
Information
Related Components
Reproduction
Expected behavior
There should be no error.
The text was updated successfully, but these errors were encountered: