Validate task outputs with structured result types.
result_type
parameter. This ensures that the result conforms to a specific data schema, making it easier to work with and reducing the risk of errors in downstream tasks.
result_type
of a task is a string, which essentially means the agent can return any value that satisfies the task’s objective.
For example, if you ask an agent to “Say hello in three languages”, it might return a simple string like "Hello; Hola; Bonjour"
or a more complex, conversational response instead:
result_type
as int
or float
:
bool
for tasks whose result is a simple true/false value:
list[str]
. This forces the agent to produce the result you probably expected (three separate strings, each representing a greeting in a different language):
result_type
as Annotated[str, "a 5 digit zip code"]
.
Literal
, or enum of allowed values for the result type. Here, we classify the media type of “Star Wars: Return of the Jedi” from a list of options:
Literal
values or enum members.
In the following example, two media types are provided as context, and because the result type is a list, the agent is able to produce two responses:
Literal
or enum will work:result_type
. Pydantic models provide a powerful way to define data schemas and validate input data.
result_type=None
. For example, you might want to ask an agent to use a tool or post a message to the workflow thread, without requiring any task output.
result_type
, you can use any of Pydantic’s built-in or custom validators to further constrain or modify the result after it has been produced.
result_validator
, it can be used to further validate or even modify the result after it has been produced by an agent.
The result validator will be called with the LLM result after it has been coerced into the result_type
, and must either return a validated result or raise an exception. ControlFlow supplies a few common validators to get you started:
between(min_value, max_value)
: Validates that the result is a float between min_value
and max_value
.has_len(min_length, max_length)
: Validates that the result is a string, list, or tuple with a length between min_length
and max_length
.has_keys(required_keys)
: Validates that the result is a dictionary with all of the specified keys.is_url()
: Validates that the result is a string that is a URL.is_email()
: Validates that the result is a string that is an email address.controlflow.tasks.validators
module, along with a convenient chain
function that allows you to combine multiple validators into a single function.