This issue doesn’t happen everywhere. I have two files generated by two different bots: one is a YAML file (which has the issue), and the other is a JSON file (which works fine).
Could this be a bug with YAML? Is JSON more compatible when handling special characters, especially those used in the French language?
Do you have any idea how to fix this encoding problem?
This doesn’t seem to be an issue with CrewAI itself, but rather with how you’re saving your file. Did you make sure you’re saving it using UTF-8 encoding?
with open("your_file.yaml", "w", encoding="utf-8") as file:
file.write(content)
Make sure to explicitly set the encoding to UTF-8 when writing your file, like in the example above.
def _run(
self,
**kwargs: Any,
) -> str:
file_path = kwargs.get("file_path", self.file_path)
if file_path is None:
return "Error: No file path provided. Please provide a file path either in the constructor or as an argument."
try:
with open(file_path, "r") as file:
return file.read()
except FileNotFoundError:
return f"Error: File not found at path: {file_path}"
except PermissionError:
return f"Error: Permission denied when trying to read file: {file_path}"
except Exception as e:
return f"Error: Failed to read file {file_path}. {str(e)}"
As you can see, open is called without specifying the encoding parameter. Now, according to the official Python documentation:
In text mode, if encoding is not specified the encoding used is platform-dependent: locale.getencoding() is called to get the current locale encoding.
So, this explains the difference in your output — the default encoding on your system is probably different from the encoding in which the file was created. By the way, the file you’re passing as the output_file in your task is saved in UTF-8. Then, when you feed that same file into the FileReadTool, it’s read using your system’s default encoding, which causes the problem — even though both operations are handled by the same library (CrewAI).
My suggestion? Open the file yourself, explicitly specifying the correct encoding, and then pass the file content directly to your agent. Alternatively, you could write a custom version of the tool tailored to your needs.