Skip to content

Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.

Fixed output_file not respecting system path (#1726)

Frieda Huangbc2a86d66a6e
diff --git a/src/crewai/task.py b/src/crewai/task.py
index af5b34c54..cedb73c09 100644
--- a/src/crewai/task.py
+++ b/src/crewai/task.py
@@ -1,6 +1,6 @@
import datetime
import json
-import os
+from pathlib import Path
import threading
import uuid
from concurrent.futures import Future
@@ -393,12 +393,13 @@ class Task(BaseModel):
if self.output_file is None:
raise ValueError("output_file is not set.")
- directory = os.path.dirname(self.output_file) # type: ignore # Value of type variable "AnyOrLiteralStr" of "dirname" cannot be "str | None"
+ resolved_path = Path(self.output_file).expanduser().resolve()
+ directory = resolved_path.parent
- if directory and not os.path.exists(directory):
- os.makedirs(directory)
+ if not directory.exists():
+ directory.mkdir(parents=True, exist_ok=True)
- with open(self.output_file, "w", encoding="utf-8") as file:
+ with resolved_path.open("w", encoding="utf-8") as file:
if isinstance(result, dict):
import json