Skip to content

Remove support for Pydantic < 2 #789

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

Merged
merged 32 commits into from
Jun 17, 2024
Merged
Show file tree
Hide file tree
Changes from 14 commits
Commits
Show all changes
32 commits
Select commit Hold shift + click to select a range
b4ced1b
remove support for pydantic < 2
aaravnavani May 25, 2024
f00afa4
lint
aaravnavani May 27, 2024
909a1e3
lint
aaravnavani May 27, 2024
d9964ff
import
aaravnavani May 28, 2024
f7a0073
run tests
aaravnavani May 28, 2024
ef6c834
pyproject
aaravnavani May 28, 2024
700c501
poetry lock
aaravnavani May 28, 2024
96eb60e
poetry lockg
aaravnavani May 28, 2024
d142562
fix ci
aaravnavani May 28, 2024
700c285
reset pyproject and poetry lock
aaravnavani May 28, 2024
419aa58
revert updates
aaravnavani May 28, 2024
3047e25
update pyproject
aaravnavani May 28, 2024
4564f31
remove pytest check
aaravnavani Jun 4, 2024
f9b1f8a
lint
aaravnavani Jun 4, 2024
f5f0292
resolve comments
aaravnavani Jun 5, 2024
fc0e696
resolve comments
aaravnavani Jun 5, 2024
e82f46f
poetry lock conflict
aaravnavani Jun 5, 2024
1df1d2a
resolve conflict
aaravnavani Jun 5, 2024
c59c3e2
run poetry lock no update
aaravnavani Jun 5, 2024
d160a79
poetry lock
aaravnavani Jun 5, 2024
1d6d005
pytest
aaravnavani Jun 5, 2024
2ab9a23
resolve pydantic version
aaravnavani Jun 5, 2024
cb727a9
lint
aaravnavani Jun 5, 2024
5f22531
remove changes on test python rail
aaravnavani Jun 5, 2024
04b66b6
fix pydantic utils
aaravnavani Jun 5, 2024
bd2d552
Merge branch '0.5.0-dev' into remove_support_old_pydantic
CalebCourier Jun 17, 2024
98b7807
delete pydantic v1 tests
CalebCourier Jun 17, 2024
c107351
run PR checks against version branches
CalebCourier Jun 17, 2024
469911a
update lock file
CalebCourier Jun 17, 2024
9d3ed1b
type fixes and cleanup
CalebCourier Jun 17, 2024
f2e1c97
cleanup test
CalebCourier Jun 17, 2024
dd3657b
fix test
CalebCourier Jun 17, 2024
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ jobs:
# TODO: fix errors so that we can run both `make dev` and `make full`
# dependencies: ['dev', 'full']
dependencies: ["full"]
pydantic-version: ["1.10.9", "2.4.2"]
pydantic-version: ["2.4.2"]
openai-version: ["1.30.1"]
steps:
- uses: actions/checkout@v4
Expand Down
8 changes: 0 additions & 8 deletions guardrails/guard.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@
from langchain_core.messages import BaseMessage
from langchain_core.runnables import Runnable, RunnableConfig
from pydantic import BaseModel
from pydantic.version import VERSION as PYDANTIC_VERSION
from typing_extensions import deprecated # type: ignore

from guardrails.api_client import GuardrailsApiClient
Expand Down Expand Up @@ -420,13 +419,6 @@ def from_pydantic(
description: Optional[str] = None,
):
"""Create a Guard instance from a Pydantic model and prompt."""
if PYDANTIC_VERSION.startswith("1"):
warnings.warn(
"""Support for Pydantic v1.x is deprecated and will be removed in
Guardrails 0.5.x. Please upgrade to the latest Pydantic v2.x to
continue receiving future updates and support.""",
FutureWarning,
)
# We have to set the tracer in the ContextStore before the Rail,
# and therefore the Validators, are initialized
cls._set_tracer(cls, tracer) # type: ignore
Expand Down
8 changes: 1 addition & 7 deletions guardrails/utils/dataclass.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,4 @@

PYDANTIC_VERSION = pydantic.version.VERSION

if PYDANTIC_VERSION.startswith("1"):

def dataclass(cls): # type: ignore
return cls

else:
from dataclasses import dataclass # type: ignore # noqa
from dataclasses import dataclass # type: ignore # noqa
25 changes: 7 additions & 18 deletions guardrails/utils/pydantic_utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,14 @@
import pydantic.version
from .v2 import (
ArbitraryModel,
add_pydantic_validators_as_guardrails_validators,
add_validator,
convert_pydantic_model_to_datatype,
convert_pydantic_model_to_openai_fn,
)

PYDANTIC_VERSION = pydantic.version.VERSION

if PYDANTIC_VERSION.startswith("1"):
from .v1 import (
ArbitraryModel,
add_pydantic_validators_as_guardrails_validators,
add_validator,
convert_pydantic_model_to_datatype,
convert_pydantic_model_to_openai_fn,
)
else:
from .v2 import (
ArbitraryModel,
add_pydantic_validators_as_guardrails_validators,
add_validator,
convert_pydantic_model_to_datatype,
convert_pydantic_model_to_openai_fn,
)


__all__ = [
"add_validator",
"add_pydantic_validators_as_guardrails_validators",
Expand Down
41 changes: 38 additions & 3 deletions poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ python = "^3.8.1"
lxml = "^4.9.3"
openai = "^1.30.1"
rich = "^13.6.0"
pydantic = ">=1.10.9, <3.0"
pydantic = ">=2.0.0, <3.0"
typer = {extras = ["all"], version = "^0.9.0"}
griffe = "^0.36.9"
tenacity = ">=8.1.0"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@

from pydantic import BaseModel, Field

from guardrails.utils.pydantic_utils import PYDANTIC_VERSION
from guardrails.utils.reask_utils import FieldReAsk
from guardrails.validator_base import OnFailAction
from guardrails.validators import (
Expand Down Expand Up @@ -61,33 +60,22 @@ class Person(BaseModel):
"""

name: str
if PYDANTIC_VERSION.startswith("1"):
age: int = Field(
..., validators=[AgeMustBeBetween0And150(on_fail=OnFailAction.REASK)]
)
zip_code: str = Field(
...,
validators=[

age: int = Field(
...,
json_schema_extra={
"validators": [AgeMustBeBetween0And150(on_fail=OnFailAction.REASK)]
},
)
zip_code: str = Field(
...,
json_schema_extra={
"validators": [
ZipCodeMustBeNumeric(on_fail=OnFailAction.REASK),
ZipCodeInCalifornia(on_fail=OnFailAction.REASK),
],
)
else:
age: int = Field(
...,
json_schema_extra={
"validators": [AgeMustBeBetween0And150(on_fail=OnFailAction.REASK)]
},
)
zip_code: str = Field(
...,
json_schema_extra={
"validators": [
ZipCodeMustBeNumeric(on_fail=OnFailAction.REASK),
ZipCodeInCalifornia(on_fail=OnFailAction.REASK),
],
},
)
},
)


class ListOfPeople(BaseModel):
Expand Down
Loading
Loading