core[minor]: support pydantic v2 models in PydanticOutputParser (#18811)

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 <jan23@gmail.com>
This commit is contained in:
Jan Nissen
2024-03-27 15:37:52 -04:00
committed by GitHub
parent d0accc3275
commit 2e0ddd6fb8
3 changed files with 122 additions and 16 deletions

View File

@@ -14,7 +14,10 @@ fi
repository_path="$1"
# Search for lines matching the pattern within the specified repository
result=$(git -C "$repository_path" grep -E '^import pydantic|^from pydantic')
result=$(
git -C "$repository_path" grep -E '^[[:space:]]*import pydantic|^[[:space:]]*from pydantic' \
-- ':!langchain_core/pydantic_*' ':!langchain_core/utils' | grep -v 'pydantic: ignore'
)
# Check if any matching lines were found
if [ -n "$result" ]; then
@@ -23,5 +26,6 @@ if [ -n "$result" ]; then
echo "Please replace the code with an import from langchain_core.pydantic_v1."
echo "For example, replace 'from pydantic import BaseModel'"
echo "with 'from langchain_core.pydantic_v1 import BaseModel'"
echo "If this was intentional, you can add # pydantic: ignore after the import to ignore this error."
exit 1
fi