Skip to content

Commit

Permalink
Add conditional check for output file directory creation (#523)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
Tavernari committed May 2, 2024
1 parent 5fde03f commit dae0aed
Showing 1 changed file with 6 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 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
Expand Down Expand Up @@ -281,6 +282,11 @@ def _is_gpt(self, llm) -> bool:
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
Expand Down

0 comments on commit dae0aed

Please sign in to comment.