diff --git a/libs/core/langchain_core/prompts/chat.py b/libs/core/langchain_core/prompts/chat.py index 489cbe7a7034a..892272d705cd7 100644 --- a/libs/core/langchain_core/prompts/chat.py +++ b/libs/core/langchain_core/prompts/chat.py @@ -1300,13 +1300,6 @@ def _prompt_type(self) -> str: """Name of prompt type. Used for serialization.""" return "chat" - def save(self, file_path: Union[Path, str]) -> None: - """Save prompt to file. - - Args: - file_path: path to file. - """ - raise NotImplementedError @override def pretty_repr(self, html: bool = False) -> str: diff --git a/tests/unit_tests/prompts/test_chat_prompt_template_save.py b/tests/unit_tests/prompts/test_chat_prompt_template_save.py new file mode 100644 index 0000000000000..9fd2d7f57f0f1 --- /dev/null +++ b/tests/unit_tests/prompts/test_chat_prompt_template_save.py @@ -0,0 +1,38 @@ +"""Test ChatPromptTemplate save method.""" + +import json +import tempfile +from pathlib import Path +from langchain_core.prompts import ChatPromptTemplate + + +def test_chat_prompt_template_save() -> None: + """Test that ChatPromptTemplate can be saved to a file.""" + # Create a simple chat prompt template + prompt = ChatPromptTemplate.from_messages([ + ("system", "You are a helpful assistant."), + ("user", "{input}") + ]) + + # Save to a temporary file + with tempfile.NamedTemporaryFile(suffix=".json", delete=False) as tmp_file: + temp_path = Path(tmp_file.name) + + try: + # This should not raise NotImplementedError anymore + prompt.save(temp_path) + + # Verify the file was created and contains valid JSON + assert temp_path.exists() + + with open(temp_path, "r") as f: + data = json.load(f) + + # Verify basic structure + assert "_type" in data + assert data["_type"] == "chat" + + finally: + # Clean up + if temp_path.exists(): + temp_path.unlink()