Output Validation
Implementing validators for agents and tools.
Validating the outputs of agents and tools is crucial for building reliable and secure AI agents. Validators help ensure data integrity and handle LLM hallucinations.
Validators
There are 4 different validators in Agency Swarm:
- Tool validators:
field_validator
: Validate individual fields independently.model_validator
: Perform complex checks involving multiple fields.
- Agent validators:
response_validator
: Validate the response before sending it to the user or other agents.
- Universal validators:
llm_validator
: Validate outputs against specified natural language rules.
Agent Response Validator
You can define a response_validator
method inside your Agent class to validate responses before sending them to the user or other agents. This method should raise an error if the response is invalid, allowing the agent to handle the error and generate a corrected response.
Example:
In this example, CustomerSupportAgent
checks the response for the presence of “bad word” and raises a ValueError
if it is found. The error is passed to the Agent to generate a corrected response.
Tool Validators
When defining tools, you can use Pydantic validators to prevent invalid data from being passed to the tool by the calling agent.
There are 2 types of validators used specifically in tools: field-level and model-level validators. Here is the comparison table to help you understand the difference between them:
Type | Purpose | Usage |
---|---|---|
Field Validators | Validate individual fields independently. | Use the @field_validator decorator on methods, specifying the field(s) to validate. |
Model Validators | Validate the entire model, allowing checks involving multiple fields. | Use the @model_validator decorator on methods. |
This example ensures that the username
field does not contain spaces using a field validator:
This example ensures that the username
field does not contain spaces using a field validator:
This example, a model validator checks that password
and confirm_password
match, which requires access to multiple fields:
LLM Validator
The llm_validator
is a powerful way to validate outputs against specified natural language rules.
Example:
In this example, the llm_validator
will throw an error if the message is not related to customer support. The caller agent will then have to fix the recipient or the message and send it again.
Since llm_validator
uses LLMs for validation, it may incur additional costs and latency due to extra API calls. Use it for fields that require complex validation beyond simple checks.
By combining all the validators described above, you can create robust validation logic to ensure your agents and tools perform reliably.