How to use Output Pydantic in a Task?
To use Output Pydantic in a task, you need to define the expected output of the task as a Pydantic model. Here's an example:
1. First, you need to define a Pydantic model. For instance, let's create a simple model for a user:
from pydantic import BaseModel
class User(BaseModel):
name: str
age: int
2. Then, when creating a task, specify the expected output as this Pydantic model:
from crewai import Task, Crew, Agent
# Import the User model
from my_models import User
# Create a task with Output Pydantic
task = Task(
description="Create a user with the provided name and age",
expected_output=User, # This is the Pydantic model
agent=agent,
tools=[tool1, tool2]
)
3. In your agent, make sure to set the output_pydantic attribute to the Pydantic model you're using:
from crewai import Agent
# Import the User model
from my_models import User
# Create an agent with Output Pydantic
agent = Agent(
role='User Creator',
goal='Create users',
backstory='I am skilled in creating user accounts',
tools=[tool1, tool2],
output_pydantic=User
)
from crewai import Crew
# Create a crew with the agent and task
crew = Crew(agents=[agent], tasks=[task])
# Kick off the crew
result = crew.kickoff()
# The output of the task will be a User object
print(result.tasks[0].output)