From dcc4a90081d2c5e0fe79e6c4aa3239579bf2e349 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:52:09 -0800 Subject: [PATCH 01/14] remove unsupported types --- guardrails/datatypes.py | 79 +---------------------------------------- 1 file changed, 1 insertion(+), 78 deletions(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index 2cdc18756..14865b36b 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -15,13 +15,7 @@ from guardrails.validator_base import Validator, ValidatorSpec from guardrails.validatorsattr import ValidatorsAttr -# TODO - deprecate these altogether -deprecated_string_types = {"sql", "email", "url", "pythoncode"} - - def update_deprecated_type_to_string(type): - if type in deprecated_string_types: - return "string" return type @@ -169,7 +163,7 @@ def decorator(cls: T) -> T: def deprecate_type(cls: type): warnings.warn( f"""The '{cls.__name__}' type is deprecated and will be removed in \ -versions 0.3.0 and beyond. Use the pydantic 'str' primitive instead.""", +versions 0.4.0 and beyond. Use the pydantic 'str' primitive instead.""", DeprecationWarning, ) return cls @@ -349,77 +343,6 @@ def from_xml(cls, element: ET._Element, strict: bool = False) -> "Time": return datatype - -@deprecate_type -@register_type("email") -class Email(ScalarType): - """Element tag: ``""" - - tag = "email" - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - deprecate_type(type(self)) - - def get_example(self): - return "hello@example.com" - - -@deprecate_type -@register_type("url") -class URL(ScalarType): - """Element tag: ``""" - - tag = "url" - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - deprecate_type(type(self)) - - def get_example(self): - return "https://example.com" - - -@deprecate_type -@register_type("pythoncode") -class PythonCode(ScalarType): - """Element tag: ``""" - - tag = "pythoncode" - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - deprecate_type(type(self)) - - def get_example(self): - return "print('hello world')" - - -@deprecate_type -@register_type("sql") -class SQLCode(ScalarType): - """Element tag: ``""" - - tag = "sql" - - def __init__(self, *args, **kwargs): - super().__init__(*args, **kwargs) - deprecate_type(type(self)) - - def get_example(self): - return "SELECT * FROM table" - - -@register_type("percentage") -class Percentage(ScalarType): - """Element tag: ``""" - - tag = "percentage" - - def get_example(self): - return "20%" - - @register_type("enum") class Enum(ScalarType): """Element tag: ``""" From ccf3aa0d3114dbc7aacb31bddadc443ac8f5f353 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Wed, 28 Feb 2024 18:54:46 -0800 Subject: [PATCH 02/14] add back percentage --- guardrails/datatypes.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index 14865b36b..8e1c1a43b 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -15,7 +15,13 @@ from guardrails.validator_base import Validator, ValidatorSpec from guardrails.validatorsattr import ValidatorsAttr +# TODO - deprecate these altogether +deprecated_string_types = {"sql", "email", "url", "pythoncode"} + + def update_deprecated_type_to_string(type): + if type in deprecated_string_types: + return "string" return type @@ -343,6 +349,16 @@ def from_xml(cls, element: ET._Element, strict: bool = False) -> "Time": return datatype +@register_type("percentage") +class Percentage(ScalarType): + """Element tag: ``""" + + tag = "percentage" + + def get_example(self): + return "20%" + + @register_type("enum") class Enum(ScalarType): """Element tag: ``""" From 097dcebfca16c9f1a5c0237ff12e6a6518a0eaff Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Wed, 28 Feb 2024 20:44:25 -0800 Subject: [PATCH 03/14] remove imports and other code --- guardrails/datatypes.py | 6 ------ guardrails/utils/json_utils.py | 13 ------------- guardrails/utils/pydantic_utils/v2.py | 2 -- 3 files changed, 21 deletions(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index 8e1c1a43b..34d839d87 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -15,13 +15,7 @@ from guardrails.validator_base import Validator, ValidatorSpec from guardrails.validatorsattr import ValidatorsAttr -# TODO - deprecate these altogether -deprecated_string_types = {"sql", "email", "url", "pythoncode"} - - def update_deprecated_type_to_string(type): - if type in deprecated_string_types: - return "string" return type diff --git a/guardrails/utils/json_utils.py b/guardrails/utils/json_utils.py index 65c1e1442..bbda9dff0 100644 --- a/guardrails/utils/json_utils.py +++ b/guardrails/utils/json_utils.py @@ -6,13 +6,11 @@ import regex from guardrails.datatypes import ( - URL, Boolean, Case, Choice, DataType, Date, - Email, Enum, Float, Integer, @@ -20,10 +18,8 @@ from guardrails.datatypes import List as ListDataType from guardrails.datatypes import ( Object, - PythonCode, String, Time, - deprecated_string_types, ) from guardrails.utils.parsing_utils import get_code_block, has_code_block @@ -56,21 +52,12 @@ def verify( Enum: str, } -ignore_types = [ - Email, # email and url should become string validators - URL, - PythonCode, -] - - @dataclass class ValuePlaceholder(Placeholder): datatype_type: Type[DataType] @property def type_object(self): - if self.datatype_type in ignore_types: - return Any return type_map[self.datatype_type] class VerificationFailed: diff --git a/guardrails/utils/pydantic_utils/v2.py b/guardrails/utils/pydantic_utils/v2.py index 566086259..6f7fcd151 100644 --- a/guardrails/utils/pydantic_utils/v2.py +++ b/guardrails/utils/pydantic_utils/v2.py @@ -8,7 +8,6 @@ from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator from pydantic.fields import FieldInfo -from guardrails.datatypes import URL as URLDataType from guardrails.datatypes import Boolean as BooleanDataType from guardrails.datatypes import Case as CaseDataType from guardrails.datatypes import Choice @@ -20,7 +19,6 @@ from guardrails.datatypes import Integer as IntegerDataType from guardrails.datatypes import List as ListDataType from guardrails.datatypes import Object as ObjectDataType -from guardrails.datatypes import PythonCode as PythonCodeDataType from guardrails.datatypes import String as StringDataType from guardrails.datatypes import Time as TimeDataType from guardrails.validator_base import Validator From 3d7b5b13bdf2bb910373b9f8cf15841543d7a036 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:39:00 -0800 Subject: [PATCH 04/14] fix lint issues --- guardrails/datatypes.py | 5 ----- guardrails/utils/json_utils.py | 6 +----- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index 34d839d87..fb775f83c 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -8,12 +8,7 @@ from dateutil.parser import parse from lxml import etree as ET -from typing_extensions import Self -from guardrails.utils.casting_utils import to_float, to_int, to_string -from guardrails.utils.xml_utils import cast_xml_to_string -from guardrails.validator_base import Validator, ValidatorSpec -from guardrails.validatorsattr import ValidatorsAttr def update_deprecated_type_to_string(type): return type diff --git a/guardrails/utils/json_utils.py b/guardrails/utils/json_utils.py index bbda9dff0..f74a04746 100644 --- a/guardrails/utils/json_utils.py +++ b/guardrails/utils/json_utils.py @@ -16,11 +16,7 @@ Integer, ) from guardrails.datatypes import List as ListDataType -from guardrails.datatypes import ( - Object, - String, - Time, -) +from guardrails.datatypes import Object, String, Time from guardrails.utils.parsing_utils import get_code_block, has_code_block From 9a31ebfe51bc61ac0dee7dda9e4c26aa65755be2 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Wed, 28 Feb 2024 21:57:55 -0800 Subject: [PATCH 05/14] fix lint --- guardrails/datatypes.py | 7 +++++++ guardrails/utils/json_utils.py | 14 +++++++------- guardrails/utils/pydantic_utils/v2.py | 9 +-------- 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index fb775f83c..119e620cf 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -8,6 +8,12 @@ from dateutil.parser import parse from lxml import etree as ET +from typing_extensions import Self + +from guardrails.utils.casting_utils import to_float, to_int, to_string +from guardrails.utils.xml_utils import cast_xml_to_string +from guardrails.validator_base import Validator, ValidatorSpec +from guardrails.validatorsattr import ValidatorsAttr def update_deprecated_type_to_string(type): @@ -338,6 +344,7 @@ def from_xml(cls, element: ET._Element, strict: bool = False) -> "Time": return datatype + @register_type("percentage") class Percentage(ScalarType): """Element tag: ``""" diff --git a/guardrails/utils/json_utils.py b/guardrails/utils/json_utils.py index f74a04746..a1ff6999f 100644 --- a/guardrails/utils/json_utils.py +++ b/guardrails/utils/json_utils.py @@ -48,6 +48,7 @@ def verify( Enum: str, } + @dataclass class ValuePlaceholder(Placeholder): datatype_type: Type[DataType] @@ -310,14 +311,13 @@ def _recurse_schema(schema: DataType): ) else: datatype_type = type(schema) - if schema.tag in deprecated_string_types: - datatype_type = String - warnings.warn( - f"""The '{schema.tag}' type is deprecated. Use the \ + datatype_type = String + warnings.warn( + f"""The '{schema.tag}' type is deprecated. Use the \ string type instead. Support for this type will \ -be dropped in version 0.3.0 and beyond.""", - DeprecationWarning, - ) +be dropped in version 0.4.0 and beyond.""", + DeprecationWarning, + ) return ValuePlaceholder( datatype_type=datatype_type, diff --git a/guardrails/utils/pydantic_utils/v2.py b/guardrails/utils/pydantic_utils/v2.py index 6f7fcd151..141d0d764 100644 --- a/guardrails/utils/pydantic_utils/v2.py +++ b/guardrails/utils/pydantic_utils/v2.py @@ -5,7 +5,7 @@ from enum import Enum from typing import Any, Callable, Dict, List, Optional, Type, TypeVar, Union, get_args -from pydantic import BaseModel, ConfigDict, HttpUrl, field_validator +from pydantic import BaseModel, ConfigDict, field_validator from pydantic.fields import FieldInfo from guardrails.datatypes import Boolean as BooleanDataType @@ -282,9 +282,6 @@ def field_to_datatype(field: Union[FieldInfo, Type]) -> Type[DataType]: # Get the type annotation from the type_annotation type_annotation = prepare_type_annotation(field) - # Use inline import to avoid circular dependency - from guardrails.datatypes import PythonCode - # Map the type annotation to the corresponding field type if is_list(type_annotation): return ListDataType @@ -304,12 +301,8 @@ def field_to_datatype(field: Union[FieldInfo, Type]) -> Type[DataType]: return StringDataType elif type_annotation == time: return TimeDataType - elif type_annotation == HttpUrl: - return URLDataType elif typing.get_origin(type_annotation) == Union: return ChoiceDataType - elif type_annotation == PythonCode: - return PythonCodeDataType else: raise ValueError(f"Unsupported type: {type_annotation}") From 9c9538d7c1bc3da624845cc223f8b19cc415f879 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Mon, 4 Mar 2024 17:44:26 -0800 Subject: [PATCH 06/14] add update deprecated type method --- guardrails/datatypes.py | 4 ++++ guardrails/guard.py | 2 +- 2 files changed, 5 insertions(+), 1 deletion(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index 119e620cf..bed4ca2aa 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -15,8 +15,12 @@ from guardrails.validator_base import Validator, ValidatorSpec from guardrails.validatorsattr import ValidatorsAttr +deprecated_string_types = {"sql", "email", "url", "pythoncode"} + def update_deprecated_type_to_string(type): + if type in deprecated_string_types: + return "string" return type diff --git a/guardrails/guard.py b/guardrails/guard.py index d41ed6391..b1951b582 100644 --- a/guardrails/guard.py +++ b/guardrails/guard.py @@ -616,7 +616,7 @@ async def _call_async( call_log=call_log, prompt_params=prompt_params ) return ValidationOutcome[OT].from_guard_history(call) - + def __repr__(self): return f"Guard(RAIL={self.rail})" From cb905f0491cea9b0bce0e8453d1a3505250f02a9 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Mon, 4 Mar 2024 18:27:40 -0800 Subject: [PATCH 07/14] fix lint issue --- guardrails/guard.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/guardrails/guard.py b/guardrails/guard.py index b1951b582..d41ed6391 100644 --- a/guardrails/guard.py +++ b/guardrails/guard.py @@ -616,7 +616,7 @@ async def _call_async( call_log=call_log, prompt_params=prompt_params ) return ValidationOutcome[OT].from_guard_history(call) - + def __repr__(self): return f"Guard(RAIL={self.rail})" From 2425ba900ce9baa2d55ae8d14889e3439574a6ec Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:12:32 -0800 Subject: [PATCH 08/14] address comments & remove code --- guardrails/datatypes.py | 22 +--------------------- guardrails/utils/json_utils.py | 7 ------- 2 files changed, 1 insertion(+), 28 deletions(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index bed4ca2aa..ac523bd18 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -15,15 +15,6 @@ from guardrails.validator_base import Validator, ValidatorSpec from guardrails.validatorsattr import ValidatorsAttr -deprecated_string_types = {"sql", "email", "url", "pythoncode"} - - -def update_deprecated_type_to_string(type): - if type in deprecated_string_types: - return "string" - return type - - @dataclass class FieldValidation: key: Any @@ -163,17 +154,6 @@ def decorator(cls: T) -> T: return decorator - -# Decorator for deprecation -def deprecate_type(cls: type): - warnings.warn( - f"""The '{cls.__name__}' type is deprecated and will be removed in \ -versions 0.4.0 and beyond. Use the pydantic 'str' primitive instead.""", - DeprecationWarning, - ) - return cls - - class ScalarType(DataType): def set_children_from_xml(self, element: ET._Element): for _ in element: @@ -446,7 +426,7 @@ def set_children_from_xml(self, element: ET._Element): # The child must be the datatype that all items in the list # must conform to. raise ValueError("List data type must have exactly one child.") - child_data_type_tag = update_deprecated_type_to_string(child.tag) + child_data_type_tag = child.tag child_data_type = registry[child_data_type_tag] self._children["item"] = child_data_type.from_xml(child) diff --git a/guardrails/utils/json_utils.py b/guardrails/utils/json_utils.py index a1ff6999f..b4cc21fcf 100644 --- a/guardrails/utils/json_utils.py +++ b/guardrails/utils/json_utils.py @@ -311,13 +311,6 @@ def _recurse_schema(schema: DataType): ) else: datatype_type = type(schema) - datatype_type = String - warnings.warn( - f"""The '{schema.tag}' type is deprecated. Use the \ -string type instead. Support for this type will \ -be dropped in version 0.4.0 and beyond.""", - DeprecationWarning, - ) return ValuePlaceholder( datatype_type=datatype_type, From 713697845db45bdb162a7b38cdb648ef4872ad58 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:16:47 -0800 Subject: [PATCH 09/14] fix lint issue --- guardrails/datatypes.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index ac523bd18..4196f3552 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -15,6 +15,7 @@ from guardrails.validator_base import Validator, ValidatorSpec from guardrails.validatorsattr import ValidatorsAttr + @dataclass class FieldValidation: key: Any @@ -154,6 +155,7 @@ def decorator(cls: T) -> T: return decorator + class ScalarType(DataType): def set_children_from_xml(self, element: ET._Element): for _ in element: From 26c8fc288875b67206761b37946372fdc2993bea Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:20:06 -0800 Subject: [PATCH 10/14] remove warnings import --- guardrails/utils/json_utils.py | 1 - 1 file changed, 1 deletion(-) diff --git a/guardrails/utils/json_utils.py b/guardrails/utils/json_utils.py index b4cc21fcf..90d09b141 100644 --- a/guardrails/utils/json_utils.py +++ b/guardrails/utils/json_utils.py @@ -1,5 +1,4 @@ import json -import warnings from dataclasses import dataclass from typing import Any, Dict, Optional, Tuple, Type, Union From 97a8562365f42cd0b244dac9f6e7aecb67c9d7e8 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Thu, 7 Mar 2024 10:25:02 -0800 Subject: [PATCH 11/14] fix lint issues --- guardrails/datatypes.py | 1 - 1 file changed, 1 deletion(-) diff --git a/guardrails/datatypes.py b/guardrails/datatypes.py index 4196f3552..00c2d1c58 100644 --- a/guardrails/datatypes.py +++ b/guardrails/datatypes.py @@ -1,5 +1,4 @@ import datetime -import warnings from dataclasses import dataclass from types import SimpleNamespace from typing import Any, Dict, Iterable From 91e5445c5b597bf8370d72de1582f4e0b43281e7 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Sun, 10 Mar 2024 17:50:43 -0700 Subject: [PATCH 12/14] fix tests --- guardrails/applications/text2sql.rail | 2 +- .../test_validator_type_deprecation.py | 49 ------------------- 2 files changed, 1 insertion(+), 50 deletions(-) delete mode 100644 tests/integration_tests/test_validator_type_deprecation.py diff --git a/guardrails/applications/text2sql.rail b/guardrails/applications/text2sql.rail index 27732b5df..631d61864 100644 --- a/guardrails/applications/text2sql.rail +++ b/guardrails/applications/text2sql.rail @@ -1,6 +1,6 @@ - - - - - - - - - -noop - - -""" - ) - with pytest.warns(DeprecationWarning): - response = guard.parse(llm_output=json.dumps(llm_output)) - assert response.validated_output == llm_output From 094bb40ad5d961ce5fa9d82089ac93c66d74ad47 Mon Sep 17 00:00:00 2001 From: Aarav Navani <38411399+oofmeister27@users.noreply.github.com> Date: Sun, 10 Mar 2024 18:29:24 -0700 Subject: [PATCH 13/14] fix test --- tests/integration_tests/test_assets/fixtures/__init__.py | 2 -- 1 file changed, 2 deletions(-) diff --git a/tests/integration_tests/test_assets/fixtures/__init__.py b/tests/integration_tests/test_assets/fixtures/__init__.py index 6b08e8913..e203981cb 100644 --- a/tests/integration_tests/test_assets/fixtures/__init__.py +++ b/tests/integration_tests/test_assets/fixtures/__init__.py @@ -11,8 +11,6 @@ def fixture_rail_spec(): - -