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-classicchainsloading
    Module●Since v1.0

    loading

    Functionality for loading chains.

    Attributes

    attribute
    URL_BASE: str
    attribute
    type_to_loader_dict: dict

    Functions

    function
    load_llm

    Import error for load_llm.

    function
    load_llm_from_config

    Import error for load_llm_from_config.

    deprecatedfunction
    load_chain_from_config

    Load chain from Config Dict.

    deprecatedfunction
    load_chain

    Unified method for loading a chain from LangChainHub or local fs.

    Classes

    class
    APIChain

    Raise an ImportError if APIChain is used without langchain_community.

    class
    Chain

    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.

    class
    HypotheticalDocumentEmbedder

    Generate hypothetical document for query, and then embed that.

    Based on https://arxiv.org/abs/2212.10496

    class
    RetrievalQAWithSourcesChain

    Question-answering with sources over an index.

    class
    VectorDBQAWithSourcesChain

    Question-answering with sources over a vector database.

    deprecatedclass
    MapReduceDocumentsChain

    Combining documents by mapping a chain over them, then combining results.

    We first call llm_chain on each document individually, passing in the page_content and any other kwargs. This is the map step.

    We then process the results of that map step in a reduce step. This should likely be a ReduceDocumentsChain.

    deprecatedclass
    MapRerankDocumentsChain

    Combining documents by mapping a chain over them, then reranking results.

    This algorithm calls an LLMChain on each input document. The LLMChain is expected to have an OutputParser that parses the result into both an answer (answer_key) and a score (rank_key). The answer with the highest score is then returned.

    deprecatedclass
    RefineDocumentsChain

    Combine documents by doing a first pass and then refining on more documents.

    This algorithm first calls initial_llm_chain on the first document, passing that first document in with the variable name document_variable_name, and produces a new variable with the variable name initial_response_name.

    Then, it loops over every remaining document. This is called the "refine" step. It calls refine_llm_chain, passing in that document with the variable name document_variable_name as well as the previous response with the variable name initial_response_name.

    deprecatedclass
    StuffDocumentsChain

    Chain that combines documents by stuffing into context.

    This chain takes a list of documents and first combines them into a single string. It does this by formatting each document into a string with the document_prompt and then joining them together with document_separator. It then adds that new string to the inputs with the variable name set by document_variable_name. Those inputs are then passed to the llm_chain.

    deprecatedclass
    LLMChain

    Chain to run queries against LLMs.

    This class is deprecated. See below for an example implementation using LangChain runnables:

    from langchain_core.output_parsers import StrOutputParser
    from langchain_core.prompts import PromptTemplate
    from langchain_openai import OpenAI
    
    prompt_template = "Tell me a {adjective} joke"
    prompt = PromptTemplate(input_variables=["adjective"], template=prompt_template)
    model = OpenAI()
    chain = prompt | model | StrOutputParser()
    
    chain.invoke("your adjective here")
    deprecatedclass
    LLMCheckerChain

    Chain for question-answering with self-verification.

    deprecatedclass
    LLMMathChain

    Chain that interprets a prompt and executes python code to do math.

    Note

    This class is deprecated. See below for a replacement implementation using LangGraph. The benefits of this implementation are:

    • Uses LLM tool calling features;
    • Support for both token-by-token and step-by-step streaming;
    • Support for checkpointing and memory of chat history;
    • Easier to modify or extend (e.g., with additional tools, structured responses, etc.)

    Install LangGraph with:

    pip install -U langgraph
    import math
    from typing import Annotated, Sequence
    
    from langchain_core.messages import BaseMessage
    from langchain_core.runnables import RunnableConfig
    from langchain_core.tools import tool
    from langchain_openai import ChatOpenAI
    from langgraph.graph import END, StateGraph
    from langgraph.graph.message import add_messages
    from langgraph.prebuilt.tool_node import ToolNode
    import numexpr
    from typing_extensions import TypedDict
    
    @tool
    def calculator(expression: str) -> str:
        """Calculate expression using Python's numexpr library.
    
        Expression should be a single line mathematical expression
        that solves the problem.
    deprecatedclass
    QAWithSourcesChain

    Question answering with sources over documents.

    deprecatedclass
    RetrievalQA

    Chain for question-answering against an index.

    This class is deprecated. See below for an example implementation using create_retrieval_chain:

    from langchain_classic.chains import create_retrieval_chain
    from langchain_classic.chains.combine_documents import (
        create_stuff_documents_chain,
    )
    from langchain_core.prompts import ChatPromptTemplate
    from langchain_openai import ChatOpenAI
    
    retriever = ...  # Your retriever
    model = ChatOpenAI()
    
    system_prompt = (
        "Use the given context to answer the question. "
        "If you don't know the answer, say you don't know. "
        "Use three sentence maximum and keep the answer concise. "
        "Context: {context}"
    )
    prompt = ChatPromptTemplate.from_messages(
        [
            ("system", system_prompt),
            ("human", "{input}"),
        ]
    )
    question_answer_chain = create_stuff_documents_chain(model, prompt)
    chain = create_retrieval_chain(retriever, question_answer_chain)
    
    chain.invoke({"input": query})
    deprecatedclass
    VectorDBQA

    Chain for question-answering against a vector database.

    View source on GitHub