| Developed by | Reflex |
|---|---|
| Date of development | Feb 15, 2024 |
| Validator type | Format |
| Blog | |
| License | Apache 2 |
| Input/Output | Output |
This validator checks if a string is valid Python syntax by trying to parse the string into an abstract syntax tree. Note that this validator doesn’t check for things such as correct argument names, etc.
$ guardrails hub install hub://reflex/valid_pythonIn this example, we apply the validator to a string output generated by an LLM.
# Import Guard and Validator
from guardrails.hub import BugFreePython
from guardrails import Guard
# Initialize Validator
val = BugFreePython(on_fail="fix")
# Setup Guard
guard = Guard.from_string(
validators=[val, ...],
)
# Correct python
correct_python = """
import os
def foo():
print(f"Current path is: {os.getcwd()}")
foo()
"""
incorrect_python = """
import os
def foo()
print f"Current path is: {os.getcwd()}"
foo()
"""
guard.parse(correct_python) # Validator passes
guard.parse(incorrect_python) # Validator failsIn this example, we apply the validator on the string field of a JSON output.
# Import Guard and Validator
from pydantic import BaseModel
from guardrails.hub import BugFreePython
from guardrails import Guard
val = BugFreePython(on_fail="fix")
# Create Pydantic BaseModel
class ProgramGen(BaseModel):
program_description: str
code: str = Field(
description="Generated code", validators=[val]
)
# Create a Guard to check for valid Pydantic output
guard = Guard.from_pydantic(output_class=ProgramGen)
# Run LLM output generating JSON through guard
guard.parse("""
{
"program_description": "Caesar",
"code": "
import os
def foo():
print(f"Current path is: {os.getcwd()}")
foo()
"
}
""")tbd
tbd
__init__
on_fail: The policy to enact when a validator fails.