Skip to content
Closed
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
2 changes: 1 addition & 1 deletion libs/core/langchain_core/prompts/chat.py
Original file line number Diff line number Diff line change
Expand Up @@ -1287,7 +1287,7 @@ def save(self, file_path: Path | str) -> None:
Args:
file_path: path to file.
"""
raise NotImplementedError
return super().save(file_path)

@override
def pretty_repr(self, html: bool = False) -> str:
Expand Down
42 changes: 42 additions & 0 deletions libs/core/tests/unit_tests/prompts/test_chat_save.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import json
from collections.abc import Callable
from pathlib import Path
from typing import IO, Any

import pytest
import yaml

from langchain_core.prompts.chat import ChatPromptTemplate, HumanMessagePromptTemplate


@pytest.mark.parametrize(
("suffix", "loader"),
[
(".json", json.load),
(".yaml", yaml.safe_load),
],
)
def test_chat_prompt_template_save_roundtrip(
tmp_path: Path, suffix: str, loader: Callable[[IO[str]], Any]
) -> None:
prompt = ChatPromptTemplate.from_messages(
[HumanMessagePromptTemplate.from_template("Hello {name}")]
)

out = tmp_path / f"prompt{suffix}"
prompt.save(out)

assert out.exists()

with out.open("r", encoding="utf-8") as f:
data = loader(f)

# minimal structural assertions
assert data["_type"] == "chat"
# messages should have exactly one human template entry
assert isinstance(data["messages"], list)
assert len(data["messages"]) == 1
first = data["messages"][0]
# Accept various serializer shapes; only assert that an entry exists
# and is structured
assert isinstance(first, (dict, list))