Closed deven298 closed 2 weeks ago
🤖
Hey @deven298! Always good to see familiar names popping up around here. I see you've got an issue, let's take a look.
Sure, I can guide you on how to upgrade to Pydantic v2 in the LangChain repository.
Firstly, you need to update the Pydantic package in your environment. You can do this by running the following command in your terminal:
pip install --upgrade pydantic
After upgrading the Pydantic package, you need to replace the deprecated dict()
method with the model_dump()
method in your code. Here is an example of how you can do this:
# Old code
response = response.dict()
# New code
response = response.model_dump()
You can find more information about the model_dump()
method in the Pydantic v2 Migration Guide.
Please note that the model_dump()
method returns a dictionary, just like the dict()
method. However, the model_dump()
method also includes default values for fields that were not set when the model instance was created. This is a change from the dict()
method, which only included fields that were explicitly set.
I hope this helps! If you have any other questions, feel free to ask.
This 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.
Starting to work on async
To piggyback here, there's more than the deprecation warning at hand. Consider this minimal example (runs as-is, after pip install langchain_core and pydantic v2):
#/bin/bash python3
"""
The issue is, essentially, that the langchain toolchain does not support pytdantic v2 objects.
Here, I use the JsonOutputParser because that's my main exposure to the v1/v2 painpoint.
"""
from pydantic import BaseModel, Field, field_validator # <- v2 namespace
from langchain_core.output_parsers import JsonOutputParser
class A(BaseModel):
a: int
JsonOutputParser(pydantic_object=A)
It produces this error:
Traceback (most recent call last):
File "minimum_6841_test.py", line 12, in <module>
JsonOutputParser(pydantic_object=A)
File "venv/lib/python3.11/site-packages/langchain_core/load/serializable.py", line 107, in __init__
super().__init__(**kwargs)
File "venv/lib/python3.11/site-packages/pydantic/v1/main.py", line 341, in __init__
raise validation_error
pydantic.v1.error_wrappers.ValidationError: 1 validation error for JsonOutputParser
pydantic_object
subclass of BaseModel expected (type=type_error.subclass; expected_class=BaseModel)
So, in short, the JsonOutputParser can't handle pydantic v2 objects.
would this help https://github.com/pydantic/bump-pydantic ?
Hello, when is pydantic v2 upgrade planned? It would be handy to use without the fears of version mismatch. Is there anything we could contribute to expedite it? Thank you
Checked other resources
Example Code
All the chat model responses inheriting
BaseModel
are converted intodict
usingresponse.dict()
. This throws the warning in the consoleDescription
Need to update the package to migrate to the new pydantic version - https://docs.pydantic.dev/2.0/migration/#changes-to-pydanticbasemodel
System Info
langchain==0.0.336
Related Components