Skip to content

CalebCourier/valid_python_validator

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

5 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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.

About

Valid Python Guardrails AI validator - Validates that there are no Python syntactic bugs in the generated code.

Resources

License

Stars

0 stars

Watchers

0 watching

Forks

Releases

No releases published

Packages

 
 
 

Contributors