Skip to content

agentix.validation

validation

Output validation.

An output_validator is a callable that takes the model's final answer and either returns a validated/parsed value or raises on failure. When set on an Agent, a failure re-prompts the model with the error (bounded by max_output_retries); on success the parsed value is exposed as AgentOutcome.parsed.

The "return parsed / raise on failure" contract is deliberately Pythonic so it works with anything: json.loads (malformed JSON raises), a Pydantic model (model_validate_json raises ValidationError), or your own deterministic checker ("does this SQL run?", "do the tests pass?"). Validators may be sync or async.

json_output

json_output(answer: str) -> Any

Validate that the answer is valid JSON; returns the parsed object.

pydantic_output

pydantic_output(model_cls: Any) -> OutputValidator

Validator factory: validate the answer against a Pydantic model.

Returns the validated model instance; raises pydantic.ValidationError on a mismatch. agentix doesn't import pydantic — you pass your model class::

agent = Agent(..., output_validator=pydantic_output(MyModel), max_output_retries=2)
outcome.parsed  # a validated MyModel instance

regex_output

regex_output(pattern: str) -> OutputValidator

Validator factory: require the answer to match pattern; returns the answer.