LangChain Reference home pageLangChain ReferenceLangChain Reference
  • GitHub
  • Main Docs
Deep Agents
LangChain
LangGraph
Integrations
LangSmith
  • Overview
  • MCP Adapters
    • Overview
    • Agents
    • Callbacks
    • Chains
    • Chat models
    • Embeddings
    • Evaluation
    • Globals
    • Hub
    • Memory
    • Output parsers
    • Retrievers
    • Runnables
    • LangSmith
    • Storage
    Standard Tests
    Text Splitters
    ⌘I

    LangChain Assistant

    Ask a question to get started

    Enter to send•Shift+Enter new line

    Menu

    MCP Adapters
    OverviewAgentsCallbacksChainsChat modelsEmbeddingsEvaluationGlobalsHubMemoryOutput parsersRetrieversRunnablesLangSmithStorage
    Standard Tests
    Text Splitters
    Language
    Theme
    Pythonlangchain-classicchainsbaseChain
    Class●Since v1.0

    Chain

    Copy
    Chain()

    Bases

    RunnableSerializable[dict[str, Any], dict[str, Any]]ABC

    Attributes

    Methods

    Inherited fromRunnableSerializable(langchain_core)

    Attributes

    Aname

    Methods

    Mto_jsonMconfigurable_fieldsMconfigurable_alternatives
    View source on GitHub

    Inherited fromSerializable(langchain_core)

    Attributes

    Alc_secretsAlc_attributes

    Methods

    Mis_lc_serializableMget_lc_namespaceMlc_idMto_jsonMto_json_not_implemented

    Inherited fromRunnable(langchain_core)

    Attributes

    AnameAInputTypeAOutputTypeAinput_schemaAoutput_schemaAconfig_specs

    Methods

    Mget_nameMget_input_jsonschemaMget_output_jsonschemaMconfig_schemaMget_config_jsonschemaM
    attribute
    memory: BaseMemory | 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.

    attribute
    callbacks: Callbacks

    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.

    attribute
    verbose: bool

    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().

    attribute
    tags: list[str] | 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.

    attribute
    metadata: builtins.dict[str, Any] | 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.

    attribute
    callback_manager: BaseCallbackManager | None

    [DEPRECATED] Use callbacks instead.

    attribute
    model_config
    attribute
    input_keys: list[str]

    Keys expected to be in the chain input.

    attribute
    output_keys: list[str]

    Keys expected to be in the chain output.

    method
    get_input_schema
    method
    get_output_schema
    method
    invoke
    method
    ainvoke
    method
    raise_callback_manager_deprecation

    Raise deprecation warning if callback_manager is used.

    method
    set_verbose

    Set the chain verbosity.

    Defaults to the global setting if not specified by the user.

    method
    prep_outputs

    Validate and prepare chain outputs, and save info about this run to memory.

    method
    aprep_outputs

    Validate and prepare chain outputs, and save info about this run to memory.

    method
    prep_inputs

    Prepare chain inputs, including adding inputs from memory.

    method
    aprep_inputs

    Prepare chain inputs, including adding inputs from memory.

    method
    dict

    Dictionary representation of chain.

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

    method
    save

    Save the chain.

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

    deprecatedmethod
    acall

    Asynchronously execute the chain.

    deprecatedmethod
    run

    Convenience method for executing chain.

    The main difference between this method and Chain.__call__ is that this method expects inputs to be passed directly in as positional arguments or keyword arguments, whereas Chain.__call__ expects a single input dictionary with all the inputs

    deprecatedmethod
    arun

    Convenience method for executing chain.

    The main difference between this method and Chain.__call__ is that this method expects inputs to be passed directly in as positional arguments or keyword arguments, whereas Chain.__call__ expects a single input dictionary with all the inputs

    deprecatedmethod
    apply

    Call the chain on all inputs in the list.

    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__.
    get_graph
    Mget_prompts
    Mpipe
    Mpick
    Massign
    Mbatch
    Mbatch_as_completed
    Mabatch
    Mabatch_as_completed
    Mstream
    Mastream
    Mastream_log
    Mastream_events
    Mtransform
    Matransform
    Mbind
    Mwith_config
    Mwith_listeners
    Mwith_alisteners
    Mwith_types
    Mwith_retry
    Mmap
    Mwith_fallbacks
    Mas_tool