Skip to content

Commit

Permalink
Improve _save_file method to handle both dict and str inputs (#1011)
Browse files Browse the repository at this point in the history
- Add check for dict type input
- Use json.dump for dict serialization
- Convert non-dict inputs to string
- Remove type ignore comments
  • Loading branch information
rumble773 committed Jul 28, 2024
1 parent 6f2a8f0 commit 6b4710a
Showing 1 changed file with 6 additions and 2 deletions.
8 changes: 6 additions & 2 deletions src/crewai/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -360,8 +360,12 @@ def _save_file(self, result: Any) -> None:
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):
Expand Down

0 comments on commit 6b4710a

Please sign in to comment.