From d1cdb00f89ced8f17f372471a5ef11572e586ec8 Mon Sep 17 00:00:00 2001 From: qizwiz Date: Wed, 17 Sep 2025 19:10:34 -0500 Subject: [PATCH] fix: Enable save method for ChatPromptTemplate by removing NotImplementedError override This PR fixes Issue #32637 where ChatPromptTemplate's save method was raising NotImplementedError instead of inheriting the working implementation from BasePromptTemplate. The fix removes the override that was preventing inheritance of the parent class's functional save method, allowing ChatPromptTemplate instances to be saved to JSON/YAML files as expected. Fixes #32637 --- libs/core/langchain_core/prompts/chat.py | 7 ---- .../prompts/test_chat_prompt_template_save.py | 38 +++++++++++++++++++ 2 files changed, 38 insertions(+), 7 deletions(-) create mode 100644 tests/unit_tests/prompts/test_chat_prompt_template_save.py 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()