Framework for orchestrating role-playing, autonomous AI agents. By fostering collaborative intelligence, CrewAI empowers agents to work together seamlessly, tackling complex tasks.
fix(state): persist json checkpoints as utf-8 (#7257)
Rolly Calma09997bfd6f39
diff --git a/lib/crewai/src/crewai/state/provider/json_provider.py b/lib/crewai/src/crewai/state/provider/json_provider.pyindex 904526292..93556f0e3 100644--- a/lib/crewai/src/crewai/state/provider/json_provider.py+++ b/lib/crewai/src/crewai/state/provider/json_provider.py@@ -63,7 +63,7 @@ class JsonProvider(BaseProvider):file_path = _build_path(location, branch, parent_id)file_path.parent.mkdir(parents=True, exist_ok=True)- with open(file_path, "w") as f:+ with open(file_path, "w", encoding="utf-8") as f:f.write(data)return str(file_path)@@ -91,7 +91,7 @@ class JsonProvider(BaseProvider):file_path = _build_path(location, branch, parent_id)await aiofiles.os.makedirs(str(file_path.parent), exist_ok=True)- async with aiofiles.open(file_path, "w") as f:+ async with aiofiles.open(file_path, "w", encoding="utf-8") as f:await f.write(data)return str(file_path)@@ -129,7 +129,7 @@ class JsonProvider(BaseProvider):Returns:The raw JSON string."""- return Path(location).read_text()+ return Path(location).read_text(encoding="utf-8")async def afrom_checkpoint(self, location: str) -> str:"""Read a JSON checkpoint file asynchronously.@@ -140,7 +140,7 @@ class JsonProvider(BaseProvider):Returns:The raw JSON string."""- async with aiofiles.open(location) as f:+ async with aiofiles.open(location, encoding="utf-8") as f:return await f.read()diff --git a/lib/crewai/tests/test_checkpoint.py b/lib/crewai/tests/test_checkpoint.pyindex 4d316afe8..ab49786c5 100644--- a/lib/crewai/tests/test_checkpoint.py+++ b/lib/crewai/tests/test_checkpoint.py@@ -8,6 +8,7 @@ import osimport sqlite3import tempfileimport time+from pathlib import Pathfrom typing import Anyfrom unittest.mock import MagicMock, patch@@ -378,6 +379,25 @@ class TestJsonProviderFork:assert path.endswith(".json")assert os.path.isfile(path)+ def test_checkpoint_uses_utf8_for_non_ascii_json(self) -> None:+ provider = JsonProvider()+ data = '{"message": "olá niño"}'+ with tempfile.TemporaryDirectory() as d:+ path = provider.checkpoint(data, d, branch="main")++ assert Path(path).read_bytes() == data.encode("utf-8")+ assert provider.from_checkpoint(path) == data++ @pytest.mark.asyncio+ async def test_acheckpoint_uses_utf8_for_non_ascii_json(self) -> None:+ provider = JsonProvider()+ data = '{"message": "olá niño"}'+ with tempfile.TemporaryDirectory() as d:+ path = await provider.acheckpoint(data, d, branch="main")++ assert Path(path).read_bytes() == data.encode("utf-8")+ assert await provider.afrom_checkpoint(path) == data+def test_checkpoint_fork_branch_subdir(self) -> None:provider = JsonProvider()with tempfile.TemporaryDirectory() as d: