langchain-ai / langchain

🦜🔗 Build context-aware reasoning applications
https://python.langchain.com
MIT License
93.04k stars 14.95k forks source link

upgrade to pydantic v2 #16306

Closed deven298 closed 2 weeks ago

deven298 commented 8 months ago

Checked other resources

Example Code

All the chat model responses inheriting BaseModel are converted into dict using response.dict(). This throws the warning in the console

PydanticDeprecatedSince20: The `dict` method is deprecated; use `model_dump` instead. Deprecated in Pydantic V2.0 to be removed in V3.0. See Pydantic V2 Migration Guide at https://errors.pydantic.dev/2.5/migration/
  response = response.dict()

Description

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

dosubot[bot] commented 8 months 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.

Sources


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.

KartikeyBartwal commented 8 months ago

Starting to work on async

ekalosak commented 8 months ago

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.

ereli-sevenai commented 6 months ago

would this help https://github.com/pydantic/bump-pydantic ?

nnWhisperer commented 3 months ago

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