Home / Function/ save_context() — langchain Function Reference

save_context() — langchain Function Reference

Architecture documentation for the save_context() function in buffer.py from the langchain codebase.

Entity Profile

Dependency Diagram

graph TD
  61cfaae8_ec1f_bda3_4ebd_926b2c966831["save_context()"]
  1ba96309_8c4c_141b_b130_88f38c9c1dd9["ConversationStringBufferMemory"]
  61cfaae8_ec1f_bda3_4ebd_926b2c966831 -->|defined in| 1ba96309_8c4c_141b_b130_88f38c9c1dd9
  4ffe6c20_74c9_d3f6_79e4_1656bbcdeec0["asave_context()"]
  4ffe6c20_74c9_d3f6_79e4_1656bbcdeec0 -->|calls| 61cfaae8_ec1f_bda3_4ebd_926b2c966831
  style 61cfaae8_ec1f_bda3_4ebd_926b2c966831 fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

libs/langchain/langchain_classic/memory/buffer.py lines 142–157

    def save_context(self, inputs: dict[str, Any], outputs: dict[str, str]) -> None:
        """Save context from this conversation to buffer."""
        if self.input_key is None:
            prompt_input_key = get_prompt_input_key(inputs, self.memory_variables)
        else:
            prompt_input_key = self.input_key
        if self.output_key is None:
            if len(outputs) != 1:
                msg = f"One output key expected, got {outputs.keys()}"
                raise ValueError(msg)
            output_key = next(iter(outputs.keys()))
        else:
            output_key = self.output_key
        human = f"{self.human_prefix}: " + inputs[prompt_input_key]
        ai = f"{self.ai_prefix}: " + outputs[output_key]
        self.buffer += f"\n{human}\n{ai}"

Domain

Subdomains

Called By

Frequently Asked Questions

What does save_context() do?
save_context() is a function in the langchain codebase, defined in libs/langchain/langchain_classic/memory/buffer.py.
Where is save_context() defined?
save_context() is defined in libs/langchain/langchain_classic/memory/buffer.py at line 142.
What calls save_context()?
save_context() is called by 1 function(s): asave_context.

Analyze Your Own Codebase

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

Try Supermodel Free