langchain
langchain copied to clipboard
Support for Pydantic v2
Feature request
Currently, langchain 0.0.217 depends on pydantic<2 and >=1. Pydantic v2 is re-written in Rust and is between 5-50x faster than v1 depending on the use case. Given how much LangChain relies on Pydantic for both modeling and functional components, and given that FastAPI is now supporting (in beta) Pydantic v2, it'd be great to see LangChain handle a user-specified installation of Pydantic above v2.
The following is an example of what happens when a user specifies installing Pydantic above v2.
The conflict is caused by:
The user requested pydantic==2.0b2
fastapi 0.100.0b1 depends on pydantic!=1.8, !=1.8.1, <3.0.0 and >=1.7.4
inflect 6.0.4 depends on pydantic>=1.9.1
langchain 0.0.217 depends on pydantic<2 and >=1
Motivation
Pydantic v2 is re-written in Rust and is between 5-50x faster than v1 depending on the use case. Given how much LangChain relies on Pydantic for both modeling and functional components, and given that FastAPI is now supporting (in beta) Pydantic v2, it'd be great to see LangChain handle a user-specified installation of Pydantic above v2.
Your contribution
Yes! I'm currently opening just an issue to document my request, and because I'm fairly backlogged. But I have contributed to LangChain in the past and would love to write a pull request to facilitate this in full.
Answer generated by a π€
Answer
Thank you for your interest in contributing to LangChain! We greatly appreciate your support.
It's fantastic to hear that you're willing to write a pull request to facilitate the support for Pydantic v2 in LangChain. Your contribution will be valuable in enhancing the framework's functionality and performance.
Please feel free to proceed with creating the pull request whenever you're ready. The maintainers will review it and determine if it aligns with the project's goals and requirements.
If you have any questions or need any assistance during the process, don't hesitate to ask. We're here to help. Happy coding!
This response is meant to be useful, save you time, and share context. 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.
Yes, I just saw that pydantic v2 has been officially released. I am curious about the improvements and the extent of the upgrade in the context of langchain
Related https://github.com/pydantic/bump-pydantic
+1 We are creating new projects with pydantic v2 and would like to use langchain as well
Latest version of FastAPI uses pydantic 2.0
It would be great to use latest versions of langchain and pydantic
+1
+1
+1
Second! Pydantic 2 is really great and lots of langchain's python developers are using FastAPI that bumps to Pydantic 2 recently.
Guys, python-llama-cpp requires pydantic v2, when I install it langchain does not work anymore. Please, upgrade to prepare Pydantic 2
In the meantime, before a release that supports Pydantic v2 is released, is there a walkaround?
As suggested by Pydantic's README
Pydantic V2 also ships with the latest version of Pydantic V1 built in so that you can incrementally upgrade your code base and projects: from pydantic import v1 as pydantic_v1.
I suppose it is safe to upgrade to Pydantic v2 in no time by replacing all old usage of Pydantic with this so that to be compatible with other packages such as fastapi that has switched to Pydantic v2.
+1
+1
+1 here too.
This is impossible to achieve because langchain depends on langsmith which is closed source (???) and depends on pydantic v1 as well.
I am facing this issue in my project please provide the support for pydantic 2 , i am stucked in dependency confict hell otherwise
+1 here.
Bump. Would be good to have, when installing the updated llama-cpp-python it requires pydantic2 to use their server.
Bump
It seems this feature request isn't top priority ATM. So, if anyone will like to collaborate to work with me on it, please let me know.
On Fri, 4 Aug 2023, 11:27 Felipe MendonΓ§a, @.***> wrote:
Bump
β Reply to this email directly, view it on GitHub https://github.com/langchain-ai/langchain/issues/6841#issuecomment-1665386941, or unsubscribe https://github.com/notifications/unsubscribe-auth/AABPDWE2K4QIHSD5J4GN5Y3XTTFBZANCNFSM6AAAAAAZWDICUU . You are receiving this because you commented.Message ID: @.***>
bump :)
Guys! I've created a solution https://github.com/bleschunov/langchain-with-pydantic-v2
aiogram 3 also switched to Pydantic v2
+1
It seems that langchain is not compatible with supabase v1.0.4(depends on gotrue~=1.0.3).
ERROR: Cannot install ... and supabase because these package versions have conflicting dependencies.
The conflict is caused by:
fastapi 0.100.0 depends on pydantic!=1.8, !=1.8.1, !=2.0.0, !=2.0.1, <3.0.0 and >=1.7.4
langchain 0.0.263 depends on pydantic<2 and >=1
gotrue 1.0.3 depends on pydantic<3.0 and >=2.1
It seems that langchain is not compatible with supabase v1.0.4(depends on gotrue~=1.0.3).
ERROR: Cannot install ... and supabase because these package versions have conflicting dependencies. The conflict is caused by: fastapi 0.100.0 depends on pydantic!=1.8, !=1.8.1, !=2.0.0, !=2.0.1, <3.0.0 and >=1.7.4 langchain 0.0.263 depends on pydantic<2 and >=1 gotrue 1.0.3 depends on pydantic<3.0 and >=2.1
It works with my solution https://github.com/bleschunov/langchain-with-pydantic-v2
Related #9123
We must update to Pydantic 2.0 to maintain compatibility with several essential libraries and frameworks. For instance:
- Supabase
- llama-cpp-python
- llamaV2
- FastAPI > 0.100
- Spacy
All of these require Pydantic 2.0 or higher, and I suspect the list will only grow longer.
We will get these libraries' latest features and improvements if we update them.
It's essential to assess the potential impact on the Langchain project and make an informed decision about the update.
https://github.com/langchain-ai/langchain/pull/8330
Closing this issue as there's a whole migration planned by the maintainers. #9337
Hi all! As of langchain==0.0.267 the pydantic version is uncapped and we should largely be cross-compatible with pydantic v1 and v2. There are still some known pydantic v2 failure modes explained in #9337, and some potentially unknown failure modes.
We'd love to hear if the latest version unblocks your use cases or if you're still running into issues!
cc @arpagon, @preritdas, @felixs8696 @ternaus @paryska99