mirror of
https://github.com/crewAIInc/crewAI.git
synced 2026-01-09 08:08:32 +00:00
docs: document FileWriterTool as solution for file writing issues (#2039)
* docs: add FileWriterTool recommendation for file writing issues - Add FileWriterTool recommendation in _save_file docstring - Update error message to suggest using FileWriterTool for cross-platform compatibility - Resolves #2015 Co-Authored-By: Joe Moura <joao@crewai.com> * docs: enhance FileWriterTool documentation - Add cross-platform compatibility details - Highlight UTF-8 encoding support - Emphasize Windows compatibility - Add recommendation for users experiencing file writing issues Part of #2015 Co-Authored-By: Joe Moura <joao@crewai.com> * refactor: improve _save_file type hints and error messages Co-Authored-By: Joe Moura <joao@crewai.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> Co-authored-by: Joe Moura <joao@crewai.com> Co-authored-by: João Moura <joaomdmoura@gmail.com>
This commit is contained in:
committed by
Devin AI
parent
459dd459d5
commit
e9c29f3a7e
@@ -8,9 +8,9 @@ icon: file-pen
|
|||||||
|
|
||||||
## Description
|
## Description
|
||||||
|
|
||||||
The `FileWriterTool` is a component of the crewai_tools package, designed to simplify the process of writing content to files.
|
The `FileWriterTool` is a component of the crewai_tools package, designed to simplify the process of writing content to files with cross-platform compatibility (Windows, Linux, macOS).
|
||||||
It is particularly useful in scenarios such as generating reports, saving logs, creating configuration files, and more.
|
It is particularly useful in scenarios such as generating reports, saving logs, creating configuration files, and more.
|
||||||
This tool supports creating new directories if they don't exist, making it easier to organize your output.
|
This tool handles path differences across operating systems, supports UTF-8 encoding, and automatically creates directories if they don't exist, making it easier to organize your output reliably across different platforms.
|
||||||
|
|
||||||
## Installation
|
## Installation
|
||||||
|
|
||||||
@@ -43,6 +43,8 @@ print(result)
|
|||||||
|
|
||||||
## Conclusion
|
## Conclusion
|
||||||
|
|
||||||
By integrating the `FileWriterTool` into your crews, the agents can execute the process of writing content to files and creating directories.
|
By integrating the `FileWriterTool` into your crews, the agents can reliably write content to files across different operating systems.
|
||||||
This tool is essential for tasks that require saving output data, creating structured file systems, and more. By adhering to the setup and usage guidelines provided,
|
This tool is essential for tasks that require saving output data, creating structured file systems, and handling cross-platform file operations.
|
||||||
incorporating this tool into projects is straightforward and efficient.
|
It's particularly recommended for Windows users who may encounter file writing issues with standard Python file operations.
|
||||||
|
|
||||||
|
By adhering to the setup and usage guidelines provided, incorporating this tool into projects is straightforward and ensures consistent file writing behavior across all platforms.
|
||||||
|
|||||||
@@ -674,19 +674,32 @@ class Task(BaseModel):
|
|||||||
return OutputFormat.PYDANTIC
|
return OutputFormat.PYDANTIC
|
||||||
return OutputFormat.RAW
|
return OutputFormat.RAW
|
||||||
|
|
||||||
def _save_file(self, result: Any) -> None:
|
def _save_file(self, result: Union[Dict, str, Any]) -> None:
|
||||||
"""Save task output to a file.
|
"""Save task output to a file.
|
||||||
|
|
||||||
|
Note:
|
||||||
|
For cross-platform file writing, especially on Windows, consider using FileWriterTool
|
||||||
|
from the crewai_tools package:
|
||||||
|
pip install 'crewai[tools]'
|
||||||
|
from crewai_tools import FileWriterTool
|
||||||
|
|
||||||
Args:
|
Args:
|
||||||
result: The result to save to the file. Can be a dict or any stringifiable object.
|
result: The result to save to the file. Can be a dict or any stringifiable object.
|
||||||
|
|
||||||
Raises:
|
Raises:
|
||||||
ValueError: If output_file is not set
|
ValueError: If output_file is not set
|
||||||
RuntimeError: If there is an error writing to the file
|
RuntimeError: If there is an error writing to the file. For cross-platform
|
||||||
|
compatibility, especially on Windows, use FileWriterTool from crewai_tools
|
||||||
|
package.
|
||||||
"""
|
"""
|
||||||
if self.output_file is None:
|
if self.output_file is None:
|
||||||
raise ValueError("output_file is not set.")
|
raise ValueError("output_file is not set.")
|
||||||
|
|
||||||
|
FILEWRITER_RECOMMENDATION = (
|
||||||
|
"For cross-platform file writing, especially on Windows, "
|
||||||
|
"use FileWriterTool from crewai_tools package."
|
||||||
|
)
|
||||||
|
|
||||||
try:
|
try:
|
||||||
resolved_path = Path(self.output_file).expanduser().resolve()
|
resolved_path = Path(self.output_file).expanduser().resolve()
|
||||||
directory = resolved_path.parent
|
directory = resolved_path.parent
|
||||||
@@ -702,7 +715,12 @@ class Task(BaseModel):
|
|||||||
else:
|
else:
|
||||||
file.write(str(result))
|
file.write(str(result))
|
||||||
except (OSError, IOError) as e:
|
except (OSError, IOError) as e:
|
||||||
raise RuntimeError(f"Failed to save output file: {e}")
|
raise RuntimeError(
|
||||||
|
"\n".join([
|
||||||
|
f"Failed to save output file: {e}",
|
||||||
|
FILEWRITER_RECOMMENDATION
|
||||||
|
])
|
||||||
|
)
|
||||||
return None
|
return None
|
||||||
|
|
||||||
def __repr__(self):
|
def __repr__(self):
|
||||||
|
|||||||
Reference in New Issue
Block a user