diff --git a/litellm/proxy/client/cli/commands/chat.py b/litellm/proxy/client/cli/commands/chat.py index 0748ebf56fe..ced70b71012 100644 --- a/litellm/proxy/client/cli/commands/chat.py +++ b/litellm/proxy/client/cli/commands/chat.py @@ -301,8 +301,8 @@ def _save_conversation(console: Console, messages: list[dict[str, str]], command filename += ".json" try: - with open(filename, "w") as f: - json.dump(messages, f, indent=2) + with open(filename, "w", encoding="utf-8") as f: + json.dump(messages, f, indent=2, ensure_ascii=False) console.print(f"[green]Conversation saved to {filename}[/green]") except Exception as e: console.print(f"[red]Error saving conversation: {e}[/red]") @@ -320,7 +320,7 @@ def _load_conversation(console: Console, command: str, system: str | None) -> li filename += ".json" try: - with open(filename, "r") as f: + with open(filename, "r", encoding="utf-8") as f: loaded: Final[_MessagesView] = {"messages": json.load(f)} console.print(f"[green]Conversation loaded from {filename}[/green]") return loaded["messages"] diff --git a/tests/unit/proxy/client/cli/test_chat_commands.py b/tests/unit/proxy/client/cli/test_chat_commands.py index ed3201ce75f..c45158c371e 100644 --- a/tests/unit/proxy/client/cli/test_chat_commands.py +++ b/tests/unit/proxy/client/cli/test_chat_commands.py @@ -1,4 +1,5 @@ import io +from pathlib import Path from typing import Final import pytest @@ -7,7 +8,11 @@ from pydantic import JsonValue from rich.console import Console from litellm.proxy.client.chat import ChatClient -from litellm.proxy.client.cli.commands.chat import _stream_response +from litellm.proxy.client.cli.commands.chat import ( + _load_conversation, + _save_conversation, + _stream_response, +) BASE_URL: Final = "http://localhost:8000" MESSAGE: Final = "blocked [/pii] :warning: content in [prompt_injection] after 42 tokens" @@ -18,6 +23,25 @@ PARTIAL_THEN_ERROR: Final = ( ) +def test_save_and_load_conversation_roundtrips_non_ascii(tmp_path: Path) -> None: + """/save writes readable UTF-8 (not \\uXXXX escapes) and /load reads it back on any locale.""" + console = Console() + messages = [ + {"role": "user", "content": "Café ☕ 東京"}, + {"role": "assistant", "content": "日本語で返信します"}, + ] + target = tmp_path / "conversation.json" + + _save_conversation(console, messages, f"/save {target}") + + saved = target.read_text(encoding="utf-8") + assert "東京" in saved + assert "\\u" not in saved + + loaded = _load_conversation(console, f"/load {target}", system=None) + assert loaded == messages + + @pytest.mark.parametrize( "status, body, json_body", [