On a task
Attach short_enough
Attaching a guardrail function to a Task validates that task's output before the team accepts it or hands it to the next agent. If the function returns (False, 'reason'), PraisonAI tells the agent what failed so it can retry and fix the reply.
Pass the function as guardrails on the Task. The team runs the agent, then the function checks the result.
The same pair decides whether the task result stands.
Run the example to see the guardrail inspect the task output and confirm whether the final answer passes your length check.
Example
from praisonai import Agent, Task, AgentTeam
def short_enough(output):
text = output.raw
if len(text) <= 400:
return (True, output)
return (False, "Too long")
agent = Agent(instructions="Answer in one sentence.")
task = Task(
description="What is an agent?",
expected_output="One sentence",
agent=agent,
guardrails=short_enough,
)
print(AgentTeam(agents=[agent], tasks=[task]).start())Example explained
- guardrails — The function Task should call with the result.
Task guardrails vs agent-wide guardrails
Attach a guardrail directly to a Task when only one step in a multi-agent workflow requires strict formatting (for example, the final customer-facing reply), while letting upstream research tasks stay flexible.
Common pitfall: writing overly rigid regex checks on an intermediate brainstorming task causes unnecessary retries before the writer even runs.
Reference: documentation