| Developed by | Guardrails AI | | Date of development | Feb 15, 2024 | | Validator type | Format | | Blog | | | License | Apache 2 | | Input/Output | Output |
This validator can perform the following checks:
- If applying this validator on a string: ensures that a generated string is of an expected length
- If applying this validator on a generated JSON object: ensures that a generated list is of an expected length
- Dependencies:
- guardrails-ai>=0.4.0
$ guardrails hub install hub://guardrails/valid_lengthIn this example, we verify that an LLM generated response contains anywhere from 3-6 characters.
# Import Guard and Validator
from guardrails import Guard
from guardrails.hub import ValidLength
# Setup Guard
guard = Guard().use(
ValidLength, min=3, max=6, on_fail="exception"
)
response = guard.validate("hello") # Validator passes
try:
response = guard.validate("hello world!") # Validator fails
except Exception as e:
print(e)Output:
Validation failed for field with errors: Value has length greater than 6. Please return a shorter output, that is shorter than 6 characters.This example applies the validator to a list of a JSON object, and ensures that the length of the list is within an expected range.
# Import Guard and Validator
from pydantic import BaseModel, Field
from guardrails.hub import ValidLength
from guardrails import Guard
val = ValidLength(min=1, max=2, on_fail="exception")
# Create Pydantic BaseModels
class ProductInfo(BaseModel):
"""Information about a single product."""
product_name: str = Field(description="Name of the product")
product_summary: str = Field(description="A summary of the product")
class ProductCategory(BaseModel):
"""List of products."""
category_name: str = Field(description="Name of product category")
products: list[ProductInfo] = Field(
description="List of products", validators=[val]
)
# Create a Guard to check for valid Pydantic output
guard = Guard.from_pydantic(output_class=ProductCategory)
# Run LLM output generating JSON through guard
guard.parse(
"""
{
"category_name": "Hair care",
"products": [
{
"product_name": "Hair spray",
"product_summary": "Helps your styled hair stay in place."
},
{
"product_name": "Shampoo",
"product_summary": "Helps clean your hair."
}
]
"""
)
try:
# Run LLM output generating JSON through guard
guard.parse(
"""
{
"category_name": "Hair care",
"products": [
{
"product_name": "Hair spray",
"product_summary": "Helps your styled hair stay in place."
},
{
"product_name": "Shampoo",
"product_summary": "Helps clean your hair."
},
{
"product_name": "Conditioner",
"product_summary": "Helps condition your hair."
}
]
}
"""
)
except Exception as e:
print(e)Output:
Validation failed for field with errors: Value has length greater than 2. Please return a shorter output, that is shorter than 2 characters.__init__(self, min=None, max=None, on_fail="noop")
-
Initializes a new instance of the Validator class.
min(int): Min expected length of the object (str, list).max(int): Max expected length of the object (str, list).on_fail(str, Callable): The policy to enact when a validator fails. Ifstr, must be one ofreask,fix,filter,refrain,noop,exceptionorfix_reask. Otherwise, must be a function that is called when the validator fails.
Parameters
__call__(self, value, metadata={}) -> ValidationResult
-
Validates the given `value` using the rules defined in this validator, relying on the `metadata` provided to customize the validation process. This method is automatically invoked by `guard.parse(...)`, ensuring the validation logic is applied to the input data.
- This method should not be called directly by the user. Instead, invoke
guard.parse(...)where this method will be called internally for each associated Validator. - When invoking
guard.parse(...), ensure to pass the appropriatemetadatadictionary that includes keys and values required by this validator. Ifguardis associated with multiple validators, combine all necessary metadata into a single dictionary. value(Any): The input value to validate.metadata(dict): A dictionary containing metadata required for validation. No additional metadata keys are needed for this validator.
Note:
Parameters