Chain Class — langchain Architecture
Architecture documentation for the Chain class in base.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 097a4781_5519_0b5d_6244_98c64eadc0d6["Chain"] 2cceb861_027c_90f5_97a1_8cd0c6490b42["base.py"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|defined in| 2cceb861_027c_90f5_97a1_8cd0c6490b42 74e59845_c675_5823_626f_32eec40e542c["get_input_schema()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 74e59845_c675_5823_626f_32eec40e542c 02358b98_e168_e424_09b5_9269678ac6d5["get_output_schema()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 02358b98_e168_e424_09b5_9269678ac6d5 4028008b_f653_0846_c07a_83a9b1124616["invoke()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 4028008b_f653_0846_c07a_83a9b1124616 bf455e04_bff0_d145_d8f3_ed10680d80f9["ainvoke()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| bf455e04_bff0_d145_d8f3_ed10680d80f9 0326e8ac_ed69_695e_44e9_86f5536f41e8["_chain_type()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 0326e8ac_ed69_695e_44e9_86f5536f41e8 54442b4e_b32c_6efa_d5b3_f96be0680b25["raise_callback_manager_deprecation()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 54442b4e_b32c_6efa_d5b3_f96be0680b25 67547bb2_a351_b223_f86c_b434abf577f2["set_verbose()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 67547bb2_a351_b223_f86c_b434abf577f2 6cb2fde5_4f89_dac0_3e1b_e69a7a121f56["input_keys()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 6cb2fde5_4f89_dac0_3e1b_e69a7a121f56 f53f5c7c_a92a_1871_3ad6_fb5493b266f0["output_keys()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| f53f5c7c_a92a_1871_3ad6_fb5493b266f0 4c3683b6_56fb_2f6e_0a73_511bf42ed6c2["_validate_inputs()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 4c3683b6_56fb_2f6e_0a73_511bf42ed6c2 b864e201_ccf3_9596_f443_25609ebbb07b["_validate_outputs()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| b864e201_ccf3_9596_f443_25609ebbb07b 0afe367c_fc41_464e_cdcc_e1870eaaede3["_call()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 0afe367c_fc41_464e_cdcc_e1870eaaede3 2bdf9351_03f3_de9e_9220_47dd991b7fd8["_acall()"] 097a4781_5519_0b5d_6244_98c64eadc0d6 -->|method| 2bdf9351_03f3_de9e_9220_47dd991b7fd8
Relationship Graph
Source Code
libs/langchain/langchain_classic/chains/base.py lines 52–806
class Chain(RunnableSerializable[dict[str, Any], dict[str, Any]], ABC):
"""Abstract base class for creating structured sequences of calls to components.
Chains should be used to encode a sequence of calls to components like
models, document retrievers, other chains, etc., and provide a simple interface
to this sequence.
The Chain interface makes it easy to create apps that are:
- Stateful: add Memory to any Chain to give it state,
- Observable: pass Callbacks to a Chain to execute additional functionality,
like logging, outside the main sequence of component calls,
- Composable: the Chain API is flexible enough that it is easy to combine
Chains with other components, including other Chains.
The main methods exposed by chains are:
- `__call__`: Chains are callable. The `__call__` method is the primary way to
execute a Chain. This takes inputs as a dictionary and returns a
dictionary output.
- `run`: A convenience method that takes inputs as args/kwargs and returns the
output as a string or object. This method can only be used for a subset of
chains and cannot return as rich of an output as `__call__`.
"""
memory: BaseMemory | None = None
"""Optional memory object.
Memory is a class that gets called at the start
and at the end of every chain. At the start, memory loads variables and passes
them along in the chain. At the end, it saves any returned variables.
There are many different types of memory - please see memory docs
for the full catalog."""
callbacks: Callbacks = Field(default=None, exclude=True)
"""Optional list of callback handlers (or callback manager).
Callback handlers are called throughout the lifecycle of a call to a chain,
starting with on_chain_start, ending with on_chain_end or on_chain_error.
Each custom chain can optionally call additional callback methods, see Callback docs
for full details."""
verbose: bool = Field(default_factory=_get_verbosity)
"""Whether or not run in verbose mode. In verbose mode, some intermediate logs
will be printed to the console. Defaults to the global `verbose` value,
accessible via `langchain.globals.get_verbose()`."""
tags: list[str] | None = None
"""Optional list of tags associated with the chain.
These tags will be associated with each call to this chain,
and passed as arguments to the handlers defined in `callbacks`.
You can use these to eg identify a specific instance of a chain with its use case.
"""
metadata: builtins.dict[str, Any] | None = None
"""Optional metadata associated with the chain.
This metadata will be associated with each call to this chain,
and passed as arguments to the handlers defined in `callbacks`.
You can use these to eg identify a specific instance of a chain with its use case.
"""
callback_manager: BaseCallbackManager | None = Field(default=None, exclude=True)
"""[DEPRECATED] Use `callbacks` instead."""
model_config = ConfigDict(
arbitrary_types_allowed=True,
)
@override
def get_input_schema(
self,
config: RunnableConfig | None = None,
) -> type[BaseModel]:
# This is correct, but pydantic typings/mypy don't think so.
return create_model("ChainInput", **dict.fromkeys(self.input_keys, (Any, None)))
@override
def get_output_schema(
self,
config: RunnableConfig | None = None,
) -> type[BaseModel]:
# This is correct, but pydantic typings/mypy don't think so.
return create_model(
"ChainOutput",
**dict.fromkeys(self.output_keys, (Any, None)),
)
@override
def invoke(
self,
Source
Frequently Asked Questions
What is the Chain class?
Chain is a class in the langchain codebase, defined in libs/langchain/langchain_classic/chains/base.py.
Where is Chain defined?
Chain is defined in libs/langchain/langchain_classic/chains/base.py at line 52.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free