Skip to content

Latest commit

 

History

History
113 lines (79 loc) · 2.1 KB

File metadata and controls

113 lines (79 loc) · 2.1 KB

Overview

Developed by Reflex
Date of development Feb 15, 2024
Validator type Format
Blog
License Apache 2
Input/Output Output

Description

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.

Installation

$ guardrails hub install hub://reflex/valid_python

Usage Examples

Validating string output via Python

In 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 fails

Validating JSON output via Python

In 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()
    "
}
""")

Validating string output via RAIL

tbd

Validating JSON output via RAIL

tbd

API Reference

__init__

  • on_fail: The policy to enact when a validator fails.