Skip to content

Schema

logger module-attribute

logger = logging.getLogger(__name__)

FormatAttr

Class for parsing and manipulating the format attribute of an element.

The format attribute is a string that contains semi-colon separated validators e.g. "valid-url; is-reachable". Each validator is itself either: - the name of an parameter-less validator, e.g. "valid-url" - the name of a validator with parameters, separated by a colon with a space-separated list of parameters, e.g. "is-in: 1 2 3"

Parameters can either be written in plain text, or in python expressions enclosed in curly braces. For example, the following are all valid: - "is-in: 1 2 3" - "is-in: {1} {2} {3}" - "is-in: {1 + 2} {2 + 3} {3 + 4}"

empty property

empty: bool

Return True if the format attribute is empty, False otherwise.

format instance-attribute

format: Optional[str]

on_fail_handlers instance-attribute

on_fail_handlers: Dict[str, str]

unregistered_validators instance-attribute

unregistered_validators: List[str]

validator_args instance-attribute

validator_args: Dict[str, List[Any]]

validators instance-attribute

validators: List[Validator]

Config

arbitrary_types_allowed class-attribute instance-attribute

arbitrary_types_allowed = True

from_element classmethod

from_element(element: ET._Element, tag: str, strict: bool = False) -> FormatAttr

Create a FormatAttr object from an XML element.

Parameters:

Name Type Description Default
element _Element

The XML element.

required

Returns:

Type Description
FormatAttr

A FormatAttr object.

get_validators staticmethod

get_validators(validator_args: Dict[str, List[Any]], tag: str, on_fail_handlers: Dict[str, str], strict: bool = False) -> Tuple[List[Validator], List[str]]

Get the list of validators from the format attribute. Only the validators that are registered for this element will be returned.

For example, if the format attribute is "valid-url; is-reachable", and "is-reachable" is not registered for this element, then only the ValidUrl validator will be returned, after instantiating it with the arguments specified in the format attribute (if any).

Parameters:

Name Type Description Default
strict bool

If True, raise an error if a validator is not registered for this element. If False, ignore the validator and print a warning.

False

Returns:

Type Description
Tuple[List[Validator], List[str]]

A list of validators.

parse staticmethod

parse(format_string: str) -> Dict[str, List[Any]]

Parse the format attribute into a dictionary of validators.

Returns:

Type Description
Dict[str, List[Any]]

A dictionary of validators, where the key is the validator name, and

Dict[str, List[Any]]

the value is a list of arguments.

parse_token staticmethod

parse_token(token: str) -> Tuple[str, List[Any]]

Parse a single token in the format attribute, and return the validator name and the list of arguments.

Parameters:

Name Type Description Default
token str

The token to parse, one of the tokens returned by self.tokens.

required

Returns:

Type Description
Tuple[str, List[Any]]

A tuple of the validator name and the list of arguments.

to_prompt

to_prompt(with_keywords: bool = True) -> str

Convert the format string to another string representation for use in prompting. Uses the validators' to_prompt method in order to construct the string to use in prompting.

For example, the format string "valid-url; other-validator: 1.0 {1 + 2}" will be converted to "valid-url other-validator: arg1=1.0 arg2=3".

JsonSchema

JsonSchema(schema: Object, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None)

reask_prompt_template property

reask_prompt_template: Optional[Prompt]

reask_prompt_vars class-attribute instance-attribute

reask_prompt_vars = {'previous_response', 'output_schema'}

root_datatype instance-attribute

root_datatype = schema

__repr__

__repr__() -> str

async_validate async

async_validate(guard_logs: GuardLogs, data: Optional[Dict[str, Any]], metadata: Dict) -> Any

Validate a dictionary of data against the schema.

Parameters:

Name Type Description Default
data Optional[Dict[str, Any]]

The data to validate.

required

Returns:

Type Description
Any

The validated data.

check_valid_reask_prompt

check_valid_reask_prompt(reask_prompt: Optional[str]) -> None

from_element classmethod

from_element(root: ET._Element, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None) -> Self

get_reask_setup

get_reask_setup(reasks: List[FieldReAsk], original_response: Any, use_full_schema: bool, prompt_params: Optional[Dict[str, Any]] = None) -> Tuple[Schema, Prompt, Instructions]

introspect

introspect(data: Any) -> list

parse

parse(output: str) -> Tuple[Union[Optional[Dict], NonParseableReAsk], Optional[Exception]]

preprocess_prompt

preprocess_prompt(prompt_callable: PromptCallableBase, instructions: Optional[Instructions], prompt: Prompt)

transpile

transpile(method: str = 'default') -> str

validate

validate(guard_logs: GuardLogs, data: Optional[Dict[str, Any]], metadata: Dict) -> Any

Validate a dictionary of data against the schema.

Parameters:

Name Type Description Default
data Optional[Dict[str, Any]]

The data to validate.

required

Returns:

Type Description
Any

The validated data.

Schema

Schema(schema: DataType, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None)

Schema class that holds a _schema attribute.

reask_prompt_template property

reask_prompt_template: Optional[Prompt]

reask_prompt_vars instance-attribute

reask_prompt_vars: Set[str]

root_datatype instance-attribute

root_datatype = schema

__repr__

__repr__() -> str

async_validate async

async_validate(guard_logs: GuardLogs, data: Any, metadata: Dict) -> Any

Asynchronously validate a dictionary of data against the schema.

Parameters:

Name Type Description Default
data Any

The data to validate.

required

Returns:

Type Description
Any

The validated data.

check_valid_reask_prompt

check_valid_reask_prompt(reask_prompt: Optional[str]) -> None

from_element classmethod

from_element(root: ET._Element, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None) -> Self

Create a schema from an XML element.

get_reask_setup

get_reask_setup(reasks: List[FieldReAsk], original_response: Any, use_full_schema: bool, prompt_params: Optional[Dict[str, Any]] = None) -> Tuple[Schema, Prompt, Instructions]

Construct a schema for reasking, and a prompt for reasking.

Parameters:

Name Type Description Default
reasks List[FieldReAsk]

List of tuples, where each tuple contains the path to the reasked element, and the ReAsk object (which contains the error message describing why the reask is necessary).

required
original_response Any

The value that was returned from the API, with reasks.

required
use_full_schema bool

Whether to use the full schema, or only the schema for the reasked elements.

required

Returns:

Type Description
Tuple[Schema, Prompt, Instructions]

The schema for reasking, and the prompt for reasking.

introspect

introspect(data: Any) -> List[FieldReAsk]

Inspect the data for reasks.

Parameters:

Name Type Description Default
data Any

The data to introspect.

required

Returns:

Type Description
List[FieldReAsk]

A list of ReAsk objects.

parse

parse(output: str) -> Tuple[Any, Optional[Exception]]

Parse the output from the large language model.

Parameters:

Name Type Description Default
output str

The output from the large language model.

required

Returns:

Type Description
Tuple[Any, Optional[Exception]]

The parsed output, and the exception that was raised (if any).

preprocess_prompt

preprocess_prompt(prompt_callable: PromptCallableBase, instructions: Optional[Instructions], prompt: Prompt)

Preprocess the instructions and prompt before sending it to the model.

Parameters:

Name Type Description Default
prompt_callable PromptCallableBase

The callable to be used to prompt the model.

required
instructions Optional[Instructions]

The instructions to preprocess.

required
prompt Prompt

The prompt to preprocess.

required

transpile

transpile(method: str = 'default') -> str

Convert the XML schema to a string that is used for prompting a large language model.

Returns:

Type Description
str

The prompt.

validate

validate(guard_logs: GuardLogs, data: Any, metadata: Dict) -> Any

Validate a dictionary of data against the schema.

Parameters:

Name Type Description Default
data Any

The data to validate.

required

Returns:

Type Description
Any

The validated data.

Schema2Prompt

Class that contains transpilers to go from a schema to its representation in a prompt.

This is important for communicating the schema to a large language model, and this class will provide multiple alternatives to do so.

datatypes_to_xml staticmethod

datatypes_to_xml(dt: DataType, root: Optional[ET._Element] = None, override_tag_name: Optional[str] = None) -> ET._Element

Recursively convert the datatypes to XML elements.

default classmethod

default(schema: JsonSchema) -> str

Default transpiler.

Converts the XML schema to a string directly after removing
  • Comments
  • Action attributes like 'on-fail-*'

Parameters:

Name Type Description Default
schema JsonSchema

The schema to transpile.

required

Returns:

Type Description
str

The prompt.

StringSchema

StringSchema(schema: String, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None)

reask_prompt_template property

reask_prompt_template: Optional[Prompt]

reask_prompt_vars class-attribute instance-attribute

reask_prompt_vars = {'previous_response', 'output_schema', 'error_messages'}

root_datatype instance-attribute

root_datatype = schema

__repr__

__repr__() -> str

async_validate async

async_validate(guard_logs: GuardLogs, data: Any, metadata: Dict) -> Any

Validate a dictionary of data against the schema.

Parameters:

Name Type Description Default
data Any

The data to validate.

required

Returns:

Type Description
Any

The validated data.

check_valid_reask_prompt

check_valid_reask_prompt(reask_prompt: Optional[str]) -> None

from_element classmethod

from_element(root: ET._Element, reask_prompt_template: Optional[str] = None, reask_instructions_template: Optional[str] = None) -> Self

get_reask_setup

get_reask_setup(reasks: List[FieldReAsk], original_response: FieldReAsk, use_full_schema: bool, prompt_params: Optional[Dict[str, Any]] = None) -> Tuple[Schema, Prompt, Instructions]

introspect

introspect(data: Any) -> List[FieldReAsk]

parse

parse(output: str) -> Tuple[Any, Optional[Exception]]

preprocess_prompt

preprocess_prompt(prompt_callable: PromptCallableBase, instructions: Optional[Instructions], prompt: Prompt)

transpile

transpile(method: str = 'default') -> str

validate

validate(guard_logs: GuardLogs, data: Any, metadata: Dict) -> Any

Validate a dictionary of data against the schema.

Parameters:

Name Type Description Default
data Any

The data to validate.

required

Returns:

Type Description
Any

The validated data.