BUG+FIX: Optional field output_file not optional
The optional field, output_file, does not behave like an optional field, and the field_validator throws an exception because it does not handle the condition when the output_file is None.
The following error occurs:
\crewai\task.py", line 121, in output_file_validattion
if value.startswith("/"):
AttributeError: 'NoneType' object has no attribute 'startswith'
Example task initialization:
Task(
description=self.description,
expected_output=self.expected_output,
agent=self.agent_id._build_agent() if self.agent_id else None,
tools=[tool.build_tool() for tool in self.tool_ids],
async_execution=self.async_execution,
callback=self._callback,
output_pydantic=output_pydantic,
output_json=output_json,
output_file=self.output_file if self.output_file else None,
)
Related Field:
output_file: Optional[str] = Field(
description="A file path to be used to create a file output.",
default=None,
)
Related Method:
@field_validator("output_file")
@classmethod
def output_file_validattion(cls, value: str) -> str:
"""Validate the output file path by removing the / from the beginning of the path."""
if value.startswith("/"):
return value[1:]
return value
Solution:
@field_validator("output_file")
@classmethod
def output_file_validattion(cls, value: str) -> str:
"""Validate the output file path by removing the / from the beginning of the path."""
if value and value.startswith("/"):
return value[1:]
return value
@joaomdmoura I don't think I will get around to forking the repo and submitting a pull request with the solution stated in this issue at this time. I have, however, rebuilt the distribution and installed it, and the solution resolved the issue.
This issue is stale because it has been open for 30 days with no activity. Remove stale label or comment or this will be closed in 5 days.
This issue was closed because it has been stalled for 5 days with no activity.