Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions lib/crewai/src/crewai/state/provider/json_provider.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def checkpoint(
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)

Expand Down Expand Up @@ -91,7 +91,7 @@ async def acheckpoint(
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)

Expand Down Expand Up @@ -129,7 +129,7 @@ def from_checkpoint(self, location: str) -> str:
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.
Expand All @@ -140,7 +140,7 @@ async def afrom_checkpoint(self, location: str) -> str:
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()


Expand Down
20 changes: 20 additions & 0 deletions lib/crewai/tests/test_checkpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import sqlite3
import tempfile
import time
from pathlib import Path
from typing import Any
from unittest.mock import MagicMock, patch

Expand Down Expand Up @@ -378,6 +379,25 @@ def test_checkpoint_writes_to_branch_subdir(self) -> None:
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")
Comment thread
Vidit-Ostwal marked this conversation as resolved.
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:
Expand Down
Loading