From 6b4710a8d1978eaab1fef265efa3b10a923a9a7d Mon Sep 17 00:00:00 2001 From: Taleb <76521427+rumble773@users.noreply.github.com> Date: Sun, 28 Jul 2024 21:03:18 +0300 Subject: [PATCH] Improve _save_file method to handle both dict and str inputs (#1011) - Add check for dict type input - Use json.dump for dict serialization - Convert non-dict inputs to string - Remove type ignore comments --- src/crewai/task.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/crewai/task.py b/src/crewai/task.py index 017d21083..807e373fb 100644 --- a/src/crewai/task.py +++ b/src/crewai/task.py @@ -360,8 +360,12 @@ class Task(BaseModel): if directory and not os.path.exists(directory): os.makedirs(directory) - with open(self.output_file, "w", encoding="utf-8") as file: # type: ignore # Argument 1 to "open" has incompatible type "str | None"; expected "int | str | bytes | PathLike[str] | PathLike[bytes]" - file.write(result) + with open(self.output_file, "w", encoding="utf-8") as file: + if isinstance(result, dict): + import json + json.dump(result, file, ensure_ascii=False, indent=2) + else: + file.write(str(result)) return None def __repr__(self):