mirror of
https://github.com/langchain-ai/langserve.git
synced 2026-07-16 09:34:30 -04:00
Value not declarable with JSON Schema (Pydantic V2) #200
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @JP-Ellis on GitHub (Jun 19, 2024).
Summary
I am unable to get LangServe to work nicely with a Pydantic V2 model.
Background
I understand that LangChain and LangServe are undergoing an upgrade from Pydantic V1 to V2. I was under the impression that LangServe supports V2 models, with the one caveat that OpenAPI docs aren't presently generated.
Example
Here is a simple reproducible example of the error, along with all installed dependencies and the error logs. The TL;DR is:
langchain-core==0.2.9langserve==0.2.2pydantic==2.7.4Virtual Environment
example.pyError logs
@eyurtsev commented on GitHub (Jun 20, 2024):
LangChain does not currently support pydantic 2 models. If you're using pydantic 2, you need to use the pydantic.v1 namespace
Do this in your code;
@JP-Ellis commented on GitHub (Jun 26, 2024):
Is there a tracking issue for when LangServe will support Pydantic 2? Or is there a way to use the
pydantic.v1namespace in Pydantic V2 installations?I'm asking because I have found that the most recent release of LangChain no longer support Pydantic V1 at all due to this commit which introduced the following change to the
langchain-coredependency:@eyurtsev commented on GitHub (Jun 26, 2024):
Use pydantic.v1 right now with pydantic 2. We first need to migrate langchain to support pydantic 2 proper (rather than from the v1 namespace).
@JP-Ellis commented on GitHub (Jun 26, 2024):
Unfortunately, using the
pydantic.v1namespace does not work for me at the moment, due to some mix up between V1 and V2 functions deep in dependencies which I would rather avoid monkey-patching. Happy to raise another issue to track that specifically if you want.@eyurtsev commented on GitHub (Jun 27, 2024):
Is it coming from langchain dependencies or another dependency?
@JP-Ellis commented on GitHub (Jul 3, 2024):
In my case, it comes from the underlying
FastAPIdependency. I am getting the same error as reported in:Let me see if I can generate a minimal example within LangChain's context.
EDIT: On investigating the issue, I have confirmed it is identical to the example documented above. This does raise another question though: if FastAPI doesn't support Pydantic V1 models, how is LangServe able to avoid this very issue?
Having said all this, I am happy for you to close this issue (or keep it open for visibility, until Pydantic V2 is fully supported?)
@codekiln commented on GitHub (Aug 4, 2024):
Just now saw this issue, which is very similar to the one I just filed - https://github.com/langchain-ai/langserve/issues/725. In my case, the only reason I upgraded was that I need to update
langchain-core, which was holding back many of my otherlang*libraries. In order to do that, I needed to update pydantic to2.x. I tried converting all models tofrom langchain.pydantic_v1 import ..., but then I discovered FastAPI was incompatible with having 2.x installed and using 1.x models; specifically, I was gettingfastapi basemodel.validate() takes 2 positional arguments but 3 were givenin my middleware on every request when I tried that.@eyurtsev commented on GitHub (Aug 7, 2024):
Hi this is a documented limitation currently. Please hold off until LangChain 0.3.x. If you want to use pydantic proper 2. You can use pydantic.v1 namespace but FastAPI does not support it.
https://github.com/langchain-ai/langserve?tab=readme-ov-file#pydantic
https://github.com/langchain-ai/langserve?tab=readme-ov-file#limitations
https://python.langchain.com/v0.2/docs/how_to/pydantic_compatibility/#4-langserve-cannot-generate-openapi-docs-if-running-pydantic-2
@Abd-elr4hman commented on GitHub (Aug 19, 2024):
how can one help with moving langchain to pydantic v2 ? can you point me towards a good starting point to get started ?
@eyurtsev