Home / Function/ save() — langchain Function Reference

save() — langchain Function Reference

Architecture documentation for the save() function in base.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  40ea1f08_c628_27cc_09d4_5781e42cf86a["save()"]
  dddce6a3_2420_c71d_01fe_f214c3eb2503["BasePromptTemplate"]
  40ea1f08_c628_27cc_09d4_5781e42cf86a -->|defined in| dddce6a3_2420_c71d_01fe_f214c3eb2503
  08753471_4b72_7243_882e_931271f22646["dict()"]
  40ea1f08_c628_27cc_09d4_5781e42cf86a -->|calls| 08753471_4b72_7243_882e_931271f22646
  style 40ea1f08_c628_27cc_09d4_5781e42cf86a fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/core/langchain_core/prompts/base.py lines 353–393

    def save(self, file_path: Path | str) -> None:
        """Save the prompt.

        Args:
            file_path: Path to directory to save prompt to.

        Raises:
            ValueError: If the prompt has partial variables.
            ValueError: If the file path is not json or yaml.
            NotImplementedError: If the prompt type is not implemented.

        Example:
            ```python
            prompt.save(file_path="path/prompt.yaml")
            ```
        """
        if self.partial_variables:
            msg = "Cannot save prompt with partial variables."
            raise ValueError(msg)

        # Fetch dictionary to save
        prompt_dict = self.dict()
        if "_type" not in prompt_dict:
            msg = f"Prompt {self} does not support saving."
            raise NotImplementedError(msg)

        # Convert file to Path object.
        save_path = Path(file_path)

        directory_path = save_path.parent
        directory_path.mkdir(parents=True, exist_ok=True)

        if save_path.suffix == ".json":
            with save_path.open("w", encoding="utf-8") as f:
                json.dump(prompt_dict, f, indent=4)
        elif save_path.suffix.endswith((".yaml", ".yml")):
            with save_path.open("w", encoding="utf-8") as f:
                yaml.dump(prompt_dict, f, default_flow_style=False)
        else:
            msg = f"{save_path} must be json or yaml"
            raise ValueError(msg)

Subdomains

Calls

Frequently Asked Questions

What does save() do?
save() is a function in the langchain codebase, defined in libs/core/langchain_core/prompts/base.py.
Where is save() defined?
save() is defined in libs/core/langchain_core/prompts/base.py at line 353.
What does save() call?
save() calls 1 function(s): dict.

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free