Skip to content
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

Upgrade to Pydantic V2 #12265

Closed
Luca-Blight opened this issue Oct 25, 2023 · 11 comments
Closed

Upgrade to Pydantic V2 #12265

Luca-Blight opened this issue Oct 25, 2023 · 11 comments
Labels
🤖:improvement Medium size change to existing code to handle new use-cases

Comments

@Luca-Blight
Copy link

Feature request

Langchain is currently using Pydantic V1, and I believe it would be good to upgrade this to V2

Motivation

V2 is far more performant than V1 because of the python-rust binding

Your contribution

I can create a PR here.

@dosubot dosubot bot added the 🤖:improvement Medium size change to existing code to handle new use-cases label Oct 25, 2023
@dosubot
Copy link

dosubot bot commented Oct 25, 2023

🤖

That's great to hear! Thank you for your willingness to contribute to LangChain. Upgrading to Pydantic V2 sounds like a valuable improvement for the framework. Please go ahead and create a pull request with your proposed changes. The maintainers will review it and decide if it should be merged. Keep up the good work!


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.

@mokeyish
Copy link
Contributor

Langchain use v1, while Fastapi use v2. they can't work together. 😔

Hopefully v2 will be supported soon.

@Luca-Blight
Copy link
Author

I'm actually working on that upgrade now. Hopefully I can settle it in the next week. You'll still have to wait until the next release : /

@dosubot
Copy link

dosubot bot commented Oct 26, 2023

🤖

@baskaryan, I could use your expertise on this one.


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.

@eyurtsev
Copy link
Collaborator

eyurtsev commented Oct 26, 2023

Please reference the discussion here: #9337

LangChain is currently cross-compatible with both versions of pydantic. When using pydantic v2, langchain relies on the v1 namespace of pydantic v2.

We cannot yet upgrade to v2 since that'll break LangChain to any users that are dependent on v1. v1 is more popular currently than v2 and is used by many of our users.

FWIW:
FastAPI includes v1/v2 comaptibility on its roadmap: fastapi/fastapi#10370, fastapi/fastapi#10360

LangChain should be usable with fastapi and pydantic v2 -- as long as LangChain schema isn't used directly in the fast api request/responses (can clone the schema with pydantic v2 use that for fast api validation, and then internally convert to schema with pydantic v1 when working with LangChain).

With that said, any work you can do to bring the code base to a state that will allow us to replace v1 models with v2 models will be appreciated.

There's some automation that's available for this: https://github.com/pydantic/bump-pydantic, but it doesn't catch all cases and in a code base like LangChain there'll be a lot of manual work likely (we've got hundreds of pydantic classes).

@mokeyish
Copy link
Contributor

Can we use a variable to control which version is used?

@eyurtsev
Copy link
Collaborator

@mokeyish I'm not sure at which level you mean

Users can control at the level fo the dependencies and install either pydantic v1 or pydantic v2. Everything will work as long as they don't only use v1 namespaced objects with langchain primitives; otherwise they can use pydantic v2 freely in their applications.

If you mean to use a variable at the application level to configure LangChain to use the proper v2 namepace, that would be great, but I'm not sure how feasible, if you can come up with a solution let us know. LangChain is CI already runs with both pydantic v1 and v2 for unit tests so you can test out to see how well your solution works.

As best as I can tell it's a bit of an all or nothing game with the pydantic migration -- I don't know that there's a safe way to upgrade some objects, but not others. :(

@nikokaoja
Copy link

nikokaoja commented Oct 26, 2023

@eyurtsev , I bump into this issue. I need to use pydantic v2 in my project, which hinders extension of for example in my case BaseChatModel. Any suggestion how to overcome my issue?

@eyurtsev
Copy link
Collaborator

@nikokaoja please follow the guidelines here: https://github.com/langchain-ai/langchain/blob/master/docs/docs/guides/pydantic_compatibility.md

@nikokaoja
Copy link

many thanks @eyurtsev ! it worked.

Copy link

dosubot bot commented Feb 8, 2024

Hi, @Luca-Blight,

I'm helping the LangChain team manage their backlog and am marking this issue as stale. From the discussion, it seems that you were willing to contribute by creating a pull request to upgrade Pydantic from V1 to V2 for the Langchain project. There was ongoing discussion about the compatibility of Langchain with both versions of Pydantic, the potential impact on users, and the feasibility of using a variable to control the version used. You mentioned working on the upgrade and aiming to settle it in the next week.

Could you please confirm if this issue is still relevant to the latest version of the LangChain repository? If it is, please let the LangChain team know by commenting on the issue. Otherwise, feel free to close the issue yourself, or it will be automatically closed in 7 days.

Thank you for your understanding and contribution to the LangChain project.

I'm dosu.

@dosubot dosubot bot added the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Feb 8, 2024
@eyurtsev eyurtsev removed the pydantic label Feb 14, 2024
@dosubot dosubot bot closed this as not planned Won't fix, can't repro, duplicate, stale Feb 15, 2024
@dosubot dosubot bot removed the stale Issue has not had recent activity or appears to be solved. Stale issues will be automatically closed label Feb 15, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🤖:improvement Medium size change to existing code to handle new use-cases
Projects
None yet
Development

No branches or pull requests

4 participants