From dae0aedc99ef06dbc3d4d08723a3bd502f3d052e Mon Sep 17 00:00:00 2001 From: Victor Carvalho Tavernari Date: Thu, 2 May 2024 07:13:51 +0100 Subject: [PATCH] Add conditional check for output file directory creation (#523) This commit adds a conditional check to ensure that the output file directory exists before attempting to create it. This ensures that the code does not fail in cases where the directory does not exist and needs to be created. The condition is added in the `_save_file` method of the `Task` class, ensuring that the correct behavior is maintained for saving results to a file. --- src/crewai/task.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/crewai/task.py b/src/crewai/task.py index c762375d0..46ffa5cb5 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -1,6 +1,7 @@ import threading import uuid from typing import Any, Dict, List, Optional, Type +import os from langchain_openai import ChatOpenAI from pydantic import UUID4, BaseModel, Field, field_validator, model_validator @@ -281,6 +282,11 @@ class Task(BaseModel): return isinstance(llm, ChatOpenAI) and llm.openai_api_base == None def _save_file(self, result: Any) -> None: + directory = os.path.dirname(self.output_file) + + if not os.path.exists(directory): + os.makedirs(directory) + with open(self.output_file, "w") as file: file.write(result) return None