Skip to content

validate()

Parse a source into a target type and then validate it against one or more constraints. You can choose to raise on violations or receive a structured result.

API Reference
validate()

Synchronously parse a source into a target type, then validate the result against constraints.

Parameters:

Name Type Description Default
source SourceParam

The value to parse and validate.

required
target TargetParam[Output]

The type or schema to parse into. Defaults to str.

str
context ContextType | List[ContextType] | None

Optional context or conversation history. Defaults to None.

None
constraints List[str] | None

List of constraint strings to validate against. Defaults to None.

None
raise_on_error bool

If True (default), raise AssertionError if validation fails. If False, return a ValidationResult with any violations. Defaults to True.

True
confidence bool

If True, includes confidence scoring. Defaults to False.

False
model ModelParam

The model to use for parsing/validation. Defaults to "openai:gpt-4o-mini".

'openai:gpt-4o-mini'
model_settings PydanticAIModelSettings | None

Model settings to use for the operation. Defaults to None.

None
instructions PydanticAIInstructions | None

Additional instructions for the model. Defaults to None.

None
attachments AttachmentType | List[AttachmentType] | None

Attachments to provide to the model. Defaults to None.

None
tools ToolType | List[ToolType] | None

Tools available to the model. Defaults to None.

None
deps Deps | None

Optional RunContext dependencies. Defaults to None.

None
usage_limits PydanticAIUsageLimits | None

Usage limits for the model call. Defaults to None.

None
observe bool | Observer | None

If True or provided, enables CLI observation output.

None

Returns:

Type Description
Result[Output] | ValidationResult[Output]

Result[Output] | ValidationResult[Output]: Result[Output] if raise_on_error is True and validation passes. ValidationResult[Output] if raise_on_error is False (always includes violations).

avalidate()

Asynchronously parse a source into a target type, then validate the result against constraints.

Parameters:

Name Type Description Default
source SourceParam

The value to parse and validate.

required
target TargetParam[Output]

The type or schema to parse into. Defaults to str.

str
context ContextType | List[ContextType] | None

Optional context or conversation history. Defaults to None.

None
constraints List[str] | None

List of constraint strings to validate against. Defaults to None.

None
raise_on_error bool

If True (default), raise AssertionError if validation fails. If False, return a ValidationResult with any violations. Defaults to True.

True
confidence bool

If True, includes confidence scoring. Defaults to False.

False
model ModelParam

The model to use for parsing/validation. Defaults to "openai:gpt-4o-mini".

'openai:gpt-4o-mini'
model_settings PydanticAIModelSettings | None

Model settings to use for the operation. Defaults to None.

None
instructions PydanticAIInstructions | None

Additional instructions for the model. Defaults to None.

None
attachments AttachmentType | List[AttachmentType] | None

Attachments to provide to the model. Defaults to None.

None
tools ToolType | List[ToolType] | None

Tools available to the model. Defaults to None.

None
deps Deps | None

Optional RunContext dependencies. Defaults to None.

None
usage_limits PydanticAIUsageLimits | None

Usage limits for the model call. Defaults to None.

None
observe bool | Observer | None

If True or provided, enables CLI observation output.

None

Returns:

Type Description
Result[Output] | ValidationResult[Output]

Result[Output] | ValidationResult[Output]: Result[Output] if raise_on_error is True and validation passes. ValidationResult[Output] if raise_on_error is False (always includes violations).


Overview

The validate() semantic operation parses a source into a target type and then validates it against one or more constraints. You can choose to raise on violations or receive a structured result.

Parsing + Constraint Validation
from zyx import validate
from pydantic import BaseModel


class User(BaseModel):
    name: str
    age: int


result = validate(
    source="Sam is 15 years old.",
    target=User,
    constraints=[
        "age must be at least 18",
    ],
    raise_on_error=False,
)


print(result.output)
print(result.violations.violations)
"""
User(name="Sam", age=15)
[ConstraintViolation(constraint="age must be at least 18", reason="age is 15")]
"""
Parsing + Constraint Validation
from zyx import avalidate
from pydantic import BaseModel


class User(BaseModel):
    name: str
    age: int


async def main():
    result = await avalidate(
        source="Sam is 15 years old.",
        target=User,
        constraints=[
            "age must be at least 18",
        ],
        raise_on_error=False,
    )
Raise on Error

Set raise_on_error=True to raise an AssertionError when any constraint is violated. Set it to False to receive a ValidationResult with violations instead.