Home / Function/ save() — langchain Function Reference

save() — langchain Function Reference

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

Entity Profile

Dependency Diagram

graph TD
  257c3f1d_0639_2e4e_fd2a_a566e69f1c1d["save()"]
  da24d66e_6dad_d8e1_dddc_7885d3e6576f["BaseMultiActionAgent"]
  257c3f1d_0639_2e4e_fd2a_a566e69f1c1d -->|defined in| da24d66e_6dad_d8e1_dddc_7885d3e6576f
  008c369d_f4fc_8ebd_1976_f8b1ae09e0b6["save()"]
  008c369d_f4fc_8ebd_1976_f8b1ae09e0b6 -->|calls| 257c3f1d_0639_2e4e_fd2a_a566e69f1c1d
  bc7c843c_a2eb_bfd2_3ec6_b52a757f29d1["save()"]
  bc7c843c_a2eb_bfd2_3ec6_b52a757f29d1 -->|calls| 257c3f1d_0639_2e4e_fd2a_a566e69f1c1d
  8921be96_9d38_54f4_d572_0bc8f4e4559d["dict()"]
  257c3f1d_0639_2e4e_fd2a_a566e69f1c1d -->|calls| 8921be96_9d38_54f4_d572_0bc8f4e4559d
  bc7c843c_a2eb_bfd2_3ec6_b52a757f29d1["save()"]
  257c3f1d_0639_2e4e_fd2a_a566e69f1c1d -->|calls| bc7c843c_a2eb_bfd2_3ec6_b52a757f29d1
  style 257c3f1d_0639_2e4e_fd2a_a566e69f1c1d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/agents/agent.py lines 318–354

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

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

        Raises:
            NotImplementedError: If agent does not support saving.
            ValueError: If `file_path` is not json or yaml.

        Example:
        ```python
        # If working with agent executor
        agent.agent.save(file_path="path/agent.yaml")
        ```
        """
        # Convert file to Path object.
        save_path = Path(file_path) if isinstance(file_path, str) else file_path

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

        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(agent_dict, f, indent=4)
        elif save_path.suffix.endswith((".yaml", ".yml")):
            with save_path.open("w") as f:
                yaml.dump(agent_dict, f, default_flow_style=False)
        else:
            msg = f"{save_path} must be json or yaml"
            raise ValueError(msg)

Subdomains

Calls

Called By

Frequently Asked Questions

What does save() do?
save() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/agents/agent.py.
Where is save() defined?
save() is defined in libs/langchain/langchain_classic/agents/agent.py at line 318.
What does save() call?
save() calls 2 function(s): dict, save.
What calls save()?
save() is called by 2 function(s): save, save.

Analyze Your Own Codebase

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

Try Supermodel Free