langchain icon indicating copy to clipboard operation
langchain copied to clipboard

Support for Pydantic v2

Open preritdas opened this issue 2 years ago β€’ 5 comments

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.

preritdas avatar Jun 27 '23 20:06 preritdas

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.

dosubot[bot] avatar Jun 27 '23 20:06 dosubot[bot]

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

hidaris avatar Jun 30 '23 16:06 hidaris

Related https://github.com/pydantic/bump-pydantic

hidaris avatar Jun 30 '23 16:06 hidaris

+1 We are creating new projects with pydantic v2 and would like to use langchain as well

felixs8696 avatar Jul 04 '23 19:07 felixs8696

Latest version of FastAPI uses pydantic 2.0

It would be great to use latest versions of langchain and pydantic

ternaus avatar Jul 06 '23 21:07 ternaus

+1

timxieICN avatar Jul 14 '23 14:07 timxieICN

+1

matankley avatar Jul 17 '23 11:07 matankley

+1

RichStone avatar Jul 18 '23 14:07 RichStone

Second! Pydantic 2 is really great and lots of langchain's python developers are using FastAPI that bumps to Pydantic 2 recently.

peter65374 avatar Jul 19 '23 09:07 peter65374

Guys, python-llama-cpp requires pydantic v2, when I install it langchain does not work anymore. Please, upgrade to prepare Pydantic 2

antonkulaga avatar Jul 20 '23 14:07 antonkulaga

In the meantime, before a release that supports Pydantic v2 is released, is there a walkaround?

aitoehigie avatar Jul 22 '23 00:07 aitoehigie

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.

Gowee avatar Jul 22 '23 14:07 Gowee

+1

Burgerniu avatar Jul 26 '23 07:07 Burgerniu

+1

pstanisl avatar Jul 26 '23 07:07 pstanisl

+1 here too.

This is impossible to achieve because langchain depends on langsmith which is closed source (???) and depends on pydantic v1 as well.

mac-ema avatar Jul 26 '23 22:07 mac-ema

I am facing this issue in my project please provide the support for pydantic 2 , i am stucked in dependency confict hell otherwise

mohdhammad786 avatar Jul 31 '23 06:07 mohdhammad786

+1 here.

tasansal avatar Aug 01 '23 14:08 tasansal

Bump. Would be good to have, when installing the updated llama-cpp-python it requires pydantic2 to use their server.

paryska99 avatar Aug 03 '23 19:08 paryska99

Bump

fysp11 avatar Aug 04 '23 10:08 fysp11

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: @.***>

aitoehigie avatar Aug 04 '23 10:08 aitoehigie

bump :)

bondzula avatar Aug 09 '23 11:08 bondzula

Guys! I've created a solution https://github.com/bleschunov/langchain-with-pydantic-v2

bleschunov avatar Aug 10 '23 23:08 bleschunov

aiogram 3 also switched to Pydantic v2

k1ng avatar Aug 13 '23 11:08 k1ng

+1

sjunepark avatar Aug 13 '23 12:08 sjunepark

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

afonio avatar Aug 14 '23 03:08 afonio

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

bleschunov avatar Aug 14 '23 07:08 bleschunov

Related #9123

preritdas avatar Aug 14 '23 19:08 preritdas

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

arpagon avatar Aug 15 '23 16:08 arpagon

Closing this issue as there's a whole migration planned by the maintainers. #9337

preritdas avatar Aug 17 '23 00:08 preritdas

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

baskaryan avatar Aug 17 '23 21:08 baskaryan