Skip to content

Conversation

@20ns
Copy link

@20ns 20ns commented Oct 26, 2025

Fix ChatPromptTemplate.save() Implementation

Summary

This PR fixes issue #32637 by enabling the save() method for ChatPromptTemplate, which was previously raising NotImplementedError.

Problem

Users were unable to save ChatPromptTemplate instances to disk, unlike other prompt types such as PromptTemplate and FewShotPromptTemplate. When calling .save(), the method would raise a NotImplementedError, preventing serialization to YAML or JSON files.

Solution

The fix is straightforward: I removed the save() method override that was raising NotImplementedError. This allows ChatPromptTemplate to inherit the fully functional save() implementation from its parent class BasePromptTemplate.

The parent class implementation already handles serialization correctly using:

  • self.dict() for converting the prompt to a dictionary
  • self._prompt_type property (which ChatPromptTemplate already implements, returning "chat")

Changes

  • Removed: 8 lines from libs/core/langchain_core/prompts/chat.py (lines 1276-1283)
  • No new code added - simply leveraging existing functionality

Testing

  • Verified that ChatPromptTemplate instances can now be saved to both YAML and JSON formats
  • Confirmed all existing prompt tests pass (118 passed, 18 skipped)
  • No breaking changes or regressions introduced

Impact

After this change, users can:

  • Save ChatPromptTemplate to YAML files: chat_prompt.save("prompt.yaml")
  • Save ChatPromptTemplate to JSON files: chat_prompt.save("prompt.json")
  • Serialize prompts consistently across different prompt types

Fixes #32637

…2637)

Remove NotImplementedError override in ChatPromptTemplate.save() to allow
the parent BasePromptTemplate implementation to handle serialization.
ChatPromptTemplate already has the required _prompt_type property ('chat')
for proper serialization.

This enables users to save ChatPromptTemplate instances to YAML and JSON
files, matching the functionality of PromptTemplate and FewShotPromptTemplate.

Fixes langchain-ai#32637
@20ns 20ns requested a review from eyurtsev as a code owner October 26, 2025 15:52
@github-actions github-actions bot added fix core Related to the package `langchain-core` and removed fix labels Oct 26, 2025
@codspeed-hq
Copy link

codspeed-hq bot commented Oct 26, 2025

CodSpeed Performance Report

Merging #33676 will not alter performance

Comparing 20ns:fix/chat-prompt-template-save-method (d0cffd4) with master (f3d7152)

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

Summary

✅ 13 untouched
⏩ 21 skipped1

Footnotes

  1. 21 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports.

Copy link
Collaborator

@ccurme ccurme left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Duplicated with #33631

@ccurme ccurme closed this Oct 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

core Related to the package `langchain-core`

Projects

None yet

Development

Successfully merging this pull request may close these issues.

ChatPromptTemplate save() not implemented

2 participants