Skip to content

[BUG] Output file writes outdated task result if result was changed after guardrail #4156

@Pasa-Juliano

Description

@Pasa-Juliano

Description

When writing the output of a task to a file, it is written the result before guardrails are executed. This causes the undesired behavior of ignoring changes to the answer performed by the guardrails. This error affects json outputs, pydantic outputs and raw outputs.

Case:

The expected task output is a list of paths, and a guardrail is used to verify if these paths exist. If the agent provides an unexistent path, the guardrail will fail and the agent should correct its output. In this scenario, when the task output is written to the output file, the first result containing the incorrect path will be written, instead of the updated, correct result.

Steps to Reproduce

Executing this piece of code should reproduce the issue. Don't forget to setup your LLM.

from typing import Any
from crewai import Agent, Task, Crew, TaskOutput

def verify_exclamation_points_guardrail(result: TaskOutput) -> tuple[bool, Any]:
    """Guardrail simulation"""

    # Change result value to simulate:
    # 1. Guardrail fails
    # 2. Agent corrects output
    # 3. Guardrail succeeds
    return (True, '{"hello": "beautiful world!!!!!"}')

agent = Agent(
    role="Hello Worlder",
    goal="Be the best agent at saying 'Hello World'",
    backstory="You are a master at understanding Hello World."
)

task = Task(
    description="Output the best Hello World in the World",
    expected_output='A JSON like: {"Hello": "World"}',
    agent=agent,
    guardrails=[verify_exclamation_points_guardrail],
    output_file="helloworld.json"
)

crew = Crew(
    agents=[agent],
    tasks=[task]
)

in_memory_result = crew.kickoff()

with open('helloworld.json', 'r') as f:
    written_result = f.read()

print(f"\nIn memory task result after guardrail: {in_memory_result}\n")
print(f"\nTask result from output file: {written_result}\n")

Expected behavior

The last, most updated task output should be written to the output file.

Screenshots/Code snippets

The code snippet provided above reproduces the problem.

Operating System

Ubuntu 24.04

Python Version

3.12

crewAI Version

1.7.0

crewAI Tools Version

1.4.1

Virtual Environment

Venv

Evidence

Output from executing code snippet above.

Image

Possible Solution

In task.py, save content from the variable task_output, which is updated after each guardrail execution.

Additional context

.

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions