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
  d186db38_b7f2_5804_1ee6_7ab67774faa3["save()"]
  f3cef70e_11b0_61c9_7ec0_7308f4b45056["Chain"]
  d186db38_b7f2_5804_1ee6_7ab67774faa3 -->|defined in| f3cef70e_11b0_61c9_7ec0_7308f4b45056
  style d186db38_b7f2_5804_1ee6_7ab67774faa3 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/chains/base.py lines 759–797

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

        Expects `Chain._chain_type` property to be implemented and for memory to be
            null.

        Args:
            file_path: Path to file to save the chain to.

        Example:
            ```python
            chain.save(file_path="path/chain.yaml")
            ```
        """
        if self.memory is not None:
            msg = "Saving of memory is not yet supported."
            raise ValueError(msg)

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

        # Convert file to Path object.
        save_path = Path(file_path) if isinstance(file_path, str) else 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") as f:
                json.dump(chain_dict, f, indent=4)
        elif save_path.suffix.endswith((".yaml", ".yml")):
            with save_path.open("w") as f:
                yaml.dump(chain_dict, f, default_flow_style=False)
        else:
            msg = f"{save_path} must be json or yaml"
            raise ValueError(msg)

Subdomains

Frequently Asked Questions

What does save() do?
save() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/chains/base.py.
Where is save() defined?
save() is defined in libs/langchain/langchain_classic/chains/base.py at line 759.

Analyze Your Own Codebase

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

Try Supermodel Free