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-classicchainsconversational_retrievalbaseConversationalRetrievalChain
    Class●Since v1.0Deprecated

    ConversationalRetrievalChain

    Copy
    ConversationalRetrievalChain()

    Bases

    BaseConversationalRetrievalChain

    Used in Docs

    • Kay.ai integration
    • Outline integration
    • Rememberizer integration
    • Sec filing integration

    Attributes

    attribute
    retriever: BaseRetriever
    attribute
    max_tokens_limit: int | None

    Methods

    method
    from_llm

    Inherited fromBaseConversationalRetrievalChain

    Attributes

    Acombine_docs_chain: BaseCombineDocumentsChainAquestion_generator: LLMChain
    —

    The chain used to generate a new question for the sake of retrieval.

    Aoutput_key: strArephrase_question: bool
    —

    Whether or not to pass the new generated question to the combine_docs_chain.

    Areturn_source_documents: bool
    —

    Return the source documents.

    Areturn_generated_question: bool
    —

    Return the generated question as part of the final result.

    Aget_chat_history: Callable[[list[CHAT_TURN_TYPE]], str] | None
    —

    An optional function to get a string of the chat history.

    Aresponse_if_no_docs_found: str | None
    —

    If specified, the chain will return a fixed response if no docs

    Amodel_configAinput_keys: list[str]Aoutput_keys: list[str]
    —

    The keys to use for the output.

    Methods

    Mget_input_schemaMsave
    —

    Save the agent.

    Inherited fromChain

    Attributes

    Amemory: BaseMemory | None
    —

    Optional memory object.

    Acallbacks: CallbacksAverbose: boolAtags: list[str] | None

    Inherited fromRunnableSerializable(langchain_core)

    Attributes

    AnameAmodel_config

    Methods

    Mto_jsonMconfigurable_fields

    Inherited fromSerializable(langchain_core)

    Attributes

    Alc_secretsAlc_attributesAmodel_config

    Methods

    Mis_lc_serializable

    Inherited fromRunnable(langchain_core)

    Attributes

    AnameAInputTypeAOutputTypeAinput_schemaA
    View source on GitHub

    Chain for having a conversation based on retrieved documents.

    This class is deprecated. See below for an example implementation using create_retrieval_chain. Additional walkthroughs can be found at https://python.langchain.com/docs/use_cases/question_answering/chat_history

    from langchain_classic.chains import (
        create_history_aware_retriever,
        create_retrieval_chain,
    )
    from langchain_classic.chains.combine_documents import (
        create_stuff_documents_chain,
    )
    from langchain_core.prompts import ChatPromptTemplate, MessagesPlaceholder
    from langchain_openai import ChatOpenAI
    
    retriever = ...  # Your retriever
    
    model = ChatOpenAI()
    
    # Contextualize question
    contextualize_q_system_prompt = (
        "Given a chat history and the latest user question "
        "which might reference context in the chat history, "
        "formulate a standalone question which can be understood "
        "without the chat history. Do NOT answer the question, just "
        "reformulate it if needed and otherwise return it as is."
    )
    contextualize_q_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", contextualize_q_system_prompt),
            MessagesPlaceholder("chat_history"),
            ("human", "{input}"),
        ]
    )
    history_aware_retriever = create_history_aware_retriever(
        model, retriever, contextualize_q_prompt
    )
    
    # Answer question
    qa_system_prompt = (
        "You are an assistant for question-answering tasks. Use "
        "the following pieces of retrieved context to answer the "
        "question. If you don't know the answer, just say that you "
        "don't know. Use three sentences maximum and keep the answer "
        "concise."
        "\n\n"
        "{context}"
    )
    qa_prompt = ChatPromptTemplate.from_messages(
        [
            ("system", qa_system_prompt),
            MessagesPlaceholder("chat_history"),
            ("human", "{input}"),
        ]
    )
    # Below we use create_stuff_documents_chain to feed all retrieved context
    # into the LLM. Note that we can also use StuffDocumentsChain and other
    # instances of BaseCombineDocumentsChain.
    question_answer_chain = create_stuff_documents_chain(model, qa_prompt)
    rag_chain = create_retrieval_chain(history_aware_retriever, question_answer_chain)
    
    # Usage:
    chat_history = []  # Collect chat history here (a sequence of messages)
    rag_chain.invoke({"input": query, "chat_history": chat_history})

    This chain takes in chat history (a list of messages) and new questions, and then returns an answer to that question. The algorithm for this chain consists of three parts:

    1. Use the chat history and the new question to create a "standalone question". This is done so that this question can be passed into the retrieval step to fetch relevant documents. If only the new question was passed in, then relevant context may be lacking. If the whole conversation was passed into retrieval, there may be unnecessary information there that would distract from retrieval.

    2. This new question is passed to the retriever and relevant documents are returned.

    3. The retrieved documents are passed to an LLM along with either the new question (default behavior) or the original question and chat history to generate a final response.

    Example:

    from langchain_classic.chains import (
        StuffDocumentsChain,
        LLMChain,
        ConversationalRetrievalChain,
    )
    from langchain_core.prompts import PromptTemplate
    from langchain_openai import OpenAI
    
    combine_docs_chain = StuffDocumentsChain(...)
    vectorstore = ...
    retriever = vectorstore.as_retriever()
    
    # This controls how the standalone question is generated.
    # Should take `chat_history` and `question` as input variables.
    template = (
        "Combine the chat history and follow up question into "
        "a standalone question. Chat History: {chat_history}"
        "Follow up question: {question}"
    )
    prompt = PromptTemplate.from_template(template)
    model = OpenAI()
    question_generator_chain = LLMChain(llm=model, prompt=prompt)
    chain = ConversationalRetrievalChain(
        combine_docs_chain=combine_docs_chain,
        retriever=retriever,
        question_generator=question_generator_chain,
    )
    Ametadata: dict[str, Any] | None
    Acallback_manager: BaseCallbackManager | None
    —

    [DEPRECATED] Use callbacks instead.

    Amodel_config
    Ainput_keys: list[str]
    Aoutput_keys: list[str]
    —

    The keys to use for the output.

    Methods

    Mget_input_schemaMget_output_schemaMinvokeMainvokeMraise_callback_manager_deprecation
    —

    Raise deprecation warning if callback_manager is used.

    Mset_verbose
    —

    Set the chain verbosity.

    Macall
    —

    Asynchronously execute the chain.

    Mprep_outputs
    —

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

    Maprep_outputs
    —

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

    Mprep_inputs
    —

    Prepare chain inputs, including adding inputs from memory.

    Maprep_inputs
    —

    Prepare chain inputs, including adding inputs from memory.

    Mrun
    —

    Convenience method for executing chain.

    Marun
    —

    Convenience method for executing chain.

    Mdict
    —

    Return dictionary representation of agent.

    Msave
    —

    Save the agent.

    Mapply
    —

    Utilize the LLM generate method for speed gains.

    M
    configurable_alternatives
    M
    get_lc_namespace
    Mlc_id
    Mto_json
    Mto_json_not_implemented
    output_schema
    Aconfig_specs

    Methods

    Mget_nameMget_input_schemaMget_input_jsonschemaMget_output_schemaMget_output_jsonschemaMconfig_schemaMget_config_jsonschemaMget_graphMget_promptsMpipeMpickMassignMinvokeMainvokeMbatchMbatch_as_completedMabatchMabatch_as_completedMstreamMastreamMastream_logMastream_eventsMtransformMatransformMbindMwith_configMwith_listenersMwith_alistenersMwith_typesMwith_retryMmapMwith_fallbacksMas_tool

    Retriever to use to fetch documents.

    If set, enforces that the documents returned are less than this limit.

    This is only enforced if combine_docs_chain is of type StuffDocumentsChain.

    Convenience method to load chain from LLM and retriever.

    This provides some logic to create the question_generator chain as well as the combine_docs_chain.