-
Notifications
You must be signed in to change notification settings - Fork 16.3k
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
core[minor]: support pydantic v2 models in PydanticOutputParser #18811
Conversation
The latest updates on your projects. Learn more about Vercel for Git ↗︎ 1 Ignored Deployment
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Will require investigation
@@ -0,0 +1,55 @@ | |||
import json |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A bit worried about getting this into the code base. Definitely into core
as it will create duplicated code in core
when it upgrades to pydantic 2
Not sure, but this could potentially create issues when using in a chain or a runnable map together since it'll cause mixing of pydantic v1 and v2 code.
@jnis23 Apologies for blocking. I think we need to do a bit of investigation first to figure out the pathway for a pydantic v2 migration and how this would interact with it. |
@eyurtsev No problem at all. Agreed it feels bad to dupe that much code, but couldn't think of a better way without adding some additional abstraction. Lmk if there is anything I can do to help re: the investigation! |
@jnis23 we basically have two options:
If you want to investigate (2), I'd check that it still works with something like In this case, could we not do something like:
with PydanticBaseModel being Union[BaseModelv2, BaseModelv1] if pydantic version == 2, and
|
for
I'll try to check a potential pathway forward for this next week, and will share if it looks promising |
I'm currently using the v2 parser in a chain without issue so I can add a test for that. Are there supported use cases for outputs getting piped back into other runnables? I can definitely see that being a problem if the runnable depends on v1. If you know of any example off the top of your head, I can start there, otherwise I can dig. |
@eyurtsev Was able to get both versions working under the existing If this looks good, I can nuke the V2 class |
# * This change is easier to roll out and roll back. | ||
|
||
|
||
if get_pydantic_major_version() < 2: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There should be no side effects on import since the import is triggered by default rather than user action
@@ -0,0 +1,24 @@ | |||
import warnings |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm not convinced that the namespace is needed yet -- could you review the code snippets -- i think some form of either those versions or else local scope imports will work
if PYDANTIC_MAJOR_VERSION == 2 and issubclass( | ||
self.pydantic_object, V2BaseModel | ||
): | ||
try: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The validation error can be imported here locally -- the import is cached so there's no issue in terms of speed
if VERSION = 2:
if issubclass(self.pydantic_object, V1):
.....
except pydantic.v1.ValidationError:
else:
except pydantic.ValidationError:
|
||
from langchain_core.exceptions import OutputParserException | ||
from langchain_core.output_parsers import JsonOutputParser | ||
from langchain_core.outputs import Generation | ||
from langchain_core.pydantic_v1 import BaseModel, ValidationError | ||
from langchain_core.pydantic_v2 import BaseModel as V2BaseModel | ||
from langchain_core.pydantic_v2 import ValidationError as V2ValidationError | ||
from langchain_core.utils.pydantic import PYDANTIC_MAJOR_VERSION | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
pydantic_v2 module could probably be eliminated to do something like this instead:
import pydantic
if VERSION = 2:
PydanticBaseModel = Union[pydantic.BaseModel, pydantic.v1.BaseModel]
else:
PydanticBaseModel = pydantic.BaseModel
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@eyurtsev Definitely like this approach better, it's much cleaner. The namespace was to get around the check_pydantic
script that gets run as part of linting and in CI. If we're fine adding an exclusion for this file and the test, I can push up these changes
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah OK skipping this file or adding some # pydantic: ignore type filter
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
okay, added support for # pydantic: ignore. Lmk how the rest looks
@jnis23 looks good. you can remove the older version of the code. I added a few suggestions that I think can further reduce the code foot print of the change, let me know if those work |
@eyurtsev ready for you to re-review when you can |
Thank you @jnis23! |
…chain-ai#18811) As mentioned in langchain-ai#18322, the current PydanticOutputParser won't work for anyone trying to parse to pydantic v2 models. This PR adds a separate `PydanticV2OutputParser`, as well as a `langchain_core.pydantic_v2` namespace that will fail on import to any projects using pydantic<2. Happy to update the docs for output parsers if this is something we're interesting in adding. On a separate note, I also updated `check_pydantic.sh` to detect pydantic imports with leading whitespace and excluded the internal namespaces. That change can be separated into its own PR if needed. --------- Co-authored-by: Jan Nissen <[email protected]>
As mentioned in #18322, the current PydanticOutputParser won't work for anyone trying to parse to pydantic v2 models. This PR adds a separate `PydanticV2OutputParser`, as well as a `langchain_core.pydantic_v2` namespace that will fail on import to any projects using pydantic<2. Happy to update the docs for output parsers if this is something we're interesting in adding. On a separate note, I also updated `check_pydantic.sh` to detect pydantic imports with leading whitespace and excluded the internal namespaces. That change can be separated into its own PR if needed. --------- Co-authored-by: Jan Nissen <[email protected]>
As mentioned in #18322, the current PydanticOutputParser won't work for anyone trying to parse to pydantic v2 models. This PR adds a separate
PydanticV2OutputParser
, as well as alangchain_core.pydantic_v2
namespace that will fail on import to any projects using pydantic<2. Happy to update the docs for output parsers if this is something we're interesting in adding.On a separate note, I also updated
check_pydantic.sh
to detect pydantic imports with leading whitespace and excluded the internal namespaces. That change can be separated into its own PR if needed.