chains#

Chains are easily reusable components linked together.

Chains 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: combine Chains with other components, including other Chains.

Class hierarchy:

Chain --> <name>Chain  # Examples: LLMChain, MapReduceChain, RouterChain

Classes

chains.base.Chain

Abstract base class for creating structured sequences of calls to components.

chains.combine_documents.base.BaseCombineDocumentsChain

Base interface for chains combining documents.

chains.combine_documents.map_reduce.MapReduceDocumentsChain

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

chains.combine_documents.map_rerank.MapRerankDocumentsChain

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

chains.combine_documents.reduce.AsyncCombineDocsProtocol(...)

Interface for the combine_docs method.

chains.combine_documents.reduce.CombineDocsProtocol(...)

Interface for the combine_docs method.

chains.combine_documents.reduce.ReduceDocumentsChain

Combine documents by recursively reducing them.

chains.combine_documents.refine.RefineDocumentsChain

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

chains.constitutional_ai.base.ConstitutionalChain

Chain for applying constitutional principles.

chains.constitutional_ai.models.ConstitutionalPrinciple

Class for a constitutional principle.

chains.conversational_retrieval.base.BaseConversationalRetrievalChain

Chain for chatting with an index.

chains.conversational_retrieval.base.ChatVectorDBChain

Chain for chatting with a vector database.

chains.conversational_retrieval.base.InputType

Input type for ConversationalRetrievalChain.

chains.elasticsearch_database.base.ElasticsearchDatabaseChain

Chain for interacting with Elasticsearch Database.

chains.flare.base.FlareChain

Chain that combines a retriever, a question generator, and a response generator.

chains.flare.base.QuestionGeneratorChain

Chain that generates questions from uncertain spans.

chains.flare.prompts.FinishedOutputParser

Output parser that checks if the output is finished.

chains.hyde.base.HypotheticalDocumentEmbedder

Generate hypothetical document for query, and then embed that.

chains.llm_math.base.LLMMathChain

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

chains.moderation.OpenAIModerationChain

Pass input through a moderation endpoint.

chains.natbot.base.NatBotChain

Implement an LLM driven browser.

chains.natbot.crawler.Crawler()

A crawler for web pages.

chains.natbot.crawler.ElementInViewPort

A typed dictionary containing information about elements in the viewport.

chains.openai_functions.citation_fuzzy_match.FactWithEvidence

Class representing a single statement.

chains.openai_functions.citation_fuzzy_match.QuestionAnswer

A question and its answer as a list of facts each one should have a source.

chains.openai_functions.openapi.SimpleRequestChain

Chain for making a simple request to an API endpoint.

chains.openai_functions.qa_with_structure.AnswerWithSources

An answer to the question, with sources.

chains.prompt_selector.BasePromptSelector

Base class for prompt selectors.

chains.prompt_selector.ConditionalPromptSelector

Prompt collection that goes through conditionals.

chains.qa_with_sources.loading.LoadingCallable(...)

Interface for loading the combine documents chain.

chains.qa_with_sources.retrieval.RetrievalQAWithSourcesChain

Question-answering with sources over an index.

chains.qa_with_sources.vector_db.VectorDBQAWithSourcesChain

Question-answering with sources over a vector database.

chains.query_constructor.base.StructuredQueryOutputParser

Output parser that parses a structured query.

chains.query_constructor.parser.ISO8601Date

A date in ISO 8601 format (YYYY-MM-DD).

chains.query_constructor.schema.AttributeInfo

Information about a data source attribute.

chains.question_answering.chain.LoadingCallable(...)

Interface for loading the combine documents chain.

chains.router.base.MultiRouteChain

Use a single chain to route an input to one of multiple candidate chains.

chains.router.base.Route(destination, ...)

Create new instance of Route(destination, next_inputs)

chains.router.base.RouterChain

Chain that outputs the name of a destination chain and the inputs to it.

chains.router.embedding_router.EmbeddingRouterChain

Chain that uses embeddings to route between options.

chains.router.llm_router.RouterOutputParser

Parser for output of router chain in the multi-prompt chain.

chains.router.multi_retrieval_qa.MultiRetrievalQAChain

A multi-route chain that uses an LLM router chain to choose amongst retrieval qa chains.

chains.sequential.SequentialChain

Chain where the outputs of one chain feed directly into next.

chains.sequential.SimpleSequentialChain

Simple chain where the outputs of one step feed directly into next.

chains.sql_database.query.SQLInput

Input for a SQL Chain.

chains.sql_database.query.SQLInputWithTables

Input for a SQL Chain.

chains.summarize.chain.LoadingCallable(...)

Interface for loading the combine documents chain.

chains.transform.TransformChain

Chain that transforms the chain output.

Functions

chains.combine_documents.reduce.acollapse_docs(...)

Execute a collapse function on a set of documents and merge their metadatas.

chains.combine_documents.reduce.collapse_docs(...)

Execute a collapse function on a set of documents and merge their metadatas.

chains.combine_documents.reduce.split_list_of_docs(...)

Split Documents into subsets that each meet a cumulative length constraint.

chains.combine_documents.stuff.create_stuff_documents_chain(...)

Create a chain for passing a list of Documents to a model.

chains.example_generator.generate_example(...)

Return another example given a list of examples for a prompt.

chains.history_aware_retriever.create_history_aware_retriever(...)

Create a chain that takes conversation history and returns documents.

chains.openai_functions.citation_fuzzy_match.create_citation_fuzzy_match_runnable(llm)

Create a citation fuzzy match Runnable.

chains.openai_functions.openapi.openapi_spec_to_openai_fn(spec)

Convert a valid OpenAPI spec to the JSON Schema format expected for OpenAI

chains.openai_functions.utils.get_llm_kwargs(...)

Return the kwargs for the LLMChain constructor.

chains.prompt_selector.is_chat_model(llm)

Check if the language model is a chat model.

chains.prompt_selector.is_llm(llm)

Check if the language model is a LLM.

chains.query_constructor.base.construct_examples(...)

Construct examples from input-output pairs.

chains.query_constructor.base.fix_filter_directive(...)

Fix invalid filter directive.

chains.query_constructor.base.get_query_constructor_prompt(...)

Create query construction prompt.

chains.query_constructor.base.load_query_constructor_runnable(...)

Load a query constructor runnable chain.

chains.query_constructor.parser.get_parser([...])

Return a parser for the query language.

chains.query_constructor.parser.v_args(...)

Dummy decorator for when lark is not installed.

chains.retrieval.create_retrieval_chain(...)

Create retrieval chain that retrieves documents and then passes them on.

chains.sql_database.query.create_sql_query_chain(llm, db)

Create a chain that generates SQL queries.

chains.structured_output.base.get_openai_output_parser(...)

Get the appropriate function output parser given the user functions.

chains.summarize.chain.load_summarize_chain(llm)

Load summarizing chain.

Deprecated classes

chains.api.base.APIChain

Deprecated since version 0.2.13: This class is deprecated and will be removed in langchain 1.0. See API reference for replacement: https://api.python.langchain.com/en/latest/chains/langchain.chains.api.base.APIChain.html

chains.combine_documents.base.AnalyzeDocumentChain

Deprecated since version 0.2.7: Use example in API reference with more detail: https://api.python.langchain.com/en/latest/chains/langchain.chains.combine_documents.base.AnalyzeDocumentChain.html instead.

chains.combine_documents.stuff.StuffDocumentsChain

Deprecated since version 0.2.13: This class is deprecated. Use the create_stuff_documents_chain constructor instead. See migration guide here: https://python.langchain.com/v0.2/docs/versions/migrating_chains/stuff_docs_chain/

chains.conversation.base.ConversationChain

Deprecated since version 0.2.7: Use RunnableWithMessageHistory: https://api.python.langchain.com/en/latest/runnables/langchain_core.runnables.history.RunnableWithMessageHistory.html instead.

chains.conversational_retrieval.base.ConversationalRetrievalChain

Deprecated since version 0.1.17: Use create_history_aware_retriever together with create_retrieval_chain (see example in docstring) instead.

chains.llm.LLMChain

Deprecated since version 0.1.17: Use RunnableSequence, e.g., `prompt | llm` instead.

chains.llm_checker.base.LLMCheckerChain

Deprecated since version 0.2.13: See LangGraph guides for a variety of self-reflection and corrective strategies for question-answering and other tasks: https://langchain-ai.github.io/langgraph/tutorials/rag/langgraph_self_rag/

chains.llm_summarization_checker.base.LLMSummarizationCheckerChain

Deprecated since version 0.2.13: See LangGraph guides for a variety of self-reflection and corrective strategies for question-answering and other tasks: https://langchain-ai.github.io/langgraph/tutorials/rag/langgraph_self_rag/

chains.mapreduce.MapReduceChain

Deprecated since version 0.2.13: Refer here for a recommended map-reduce implementation using langgraph: https://langchain-ai.github.io/langgraph/how-tos/map-reduce/. See also migration guide: https://python.langchain.com/v0.2/docs/versions/migrating_chains/map_reduce_chain/

chains.qa_generation.base.QAGenerationChain

Deprecated since version 0.2.7: Use example in API reference with more detail: https://api.python.langchain.com/en/latest/chains/langchain.chains.qa_generation.base.QAGenerationChain.html instead.

chains.qa_with_sources.base.BaseQAWithSourcesChain

Deprecated since version 0.2.13: This class is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/v0.2/docs/how_to/qa_sources/

chains.qa_with_sources.base.QAWithSourcesChain

Deprecated since version 0.2.13: This class is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/v0.2/docs/how_to/qa_sources/

chains.retrieval_qa.base.BaseRetrievalQA

Deprecated since version 0.2.13: This class is deprecated. Use the create_retrieval_chain constructor instead. See migration guide here: https://python.langchain.com/v0.2/docs/versions/migrating_chains/retrieval_qa/

chains.retrieval_qa.base.RetrievalQA

Deprecated since version 0.1.17: This class is deprecated. Use the create_retrieval_chain constructor instead. See migration guide here: https://python.langchain.com/v0.2/docs/versions/migrating_chains/retrieval_qa/

chains.retrieval_qa.base.VectorDBQA

Deprecated since version 0.2.13: This class is deprecated. Use the create_retrieval_chain constructor instead. See migration guide here: https://python.langchain.com/v0.2/docs/versions/migrating_chains/retrieval_qa/

chains.router.llm_router.LLMRouterChain

Deprecated since version 0.2.12: Use RunnableLambda to select from multiple prompt templates. See example in API reference: https://api.python.langchain.com/en/latest/chains/langchain.chains.router.llm_router.LLMRouterChain.html

chains.router.multi_prompt.MultiPromptChain

Deprecated since version 0.2.12: Use RunnableLambda to select from multiple prompt templates. See example in API reference: https://api.python.langchain.com/en/latest/chains/langchain.chains.router.multi_prompt.MultiPromptChain.html

Deprecated functions

chains.loading.load_chain(path, **kwargs)

Deprecated since version 0.2.13: This function is deprecated and will be removed in langchain 1.0. At that point chains must be imported from their respective modules.

chains.loading.load_chain_from_config(...)

Deprecated since version 0.2.13: This function is deprecated and will be removed in langchain 1.0. At that point chains must be imported from their respective modules.

chains.openai_functions.base.create_openai_fn_chain(...)

Deprecated since version 0.1.1: Use create_openai_fn_runnable instead.

chains.openai_functions.base.create_structured_output_chain(...)

Deprecated since version 0.1.1: Use ChatOpenAI.with_structured_output instead.

chains.openai_functions.citation_fuzzy_match.create_citation_fuzzy_match_chain(llm)

Deprecated since version 0.2.13: Use create_citation_fuzzy_match_runnable instead.

chains.openai_functions.extraction.create_extraction_chain(...)

Deprecated since version 0.1.14: LangChain has introduced a method called with_structured_output thatis available on ChatModels capable of tool calling.You can read more about the method here: <https://python.langchain.com/docs/modules/model_io/chat/structured_output/>. Please follow our extraction use case documentation for more guidelineson how to do information extraction with LLMs.<https://python.langchain.com/docs/use_cases/extraction/>. If you notice other issues, please provide feedback here:<langchain-ai/langchain#18154> Use ``

chains.openai_functions.extraction.create_extraction_chain_pydantic(...)

Deprecated since version 0.1.14: LangChain has introduced a method called with_structured_output thatis available on ChatModels capable of tool calling.You can read more about the method here: <https://python.langchain.com/docs/modules/model_io/chat/structured_output/>. Please follow our extraction use case documentation for more guidelineson how to do information extraction with LLMs.<https://python.langchain.com/docs/use_cases/extraction/>. If you notice other issues, please provide feedback here:<langchain-ai/langchain#18154> Use ``

chains.openai_functions.openapi.get_openapi_chain(spec)

Deprecated since version 0.2.13: This function is deprecated and will be removed in langchain 1.0. See API reference for replacement: https://api.python.langchain.com/en/latest/chains/langchain.chains.openai_functions.openapi.get_openapi_chain.html

chains.openai_functions.qa_with_structure.create_qa_with_sources_chain(llm)

Deprecated since version 0.2.13: This function is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/v0.2/docs/how_to/qa_sources/#structure-sources-in-model-response

chains.openai_functions.qa_with_structure.create_qa_with_structure_chain(...)

Deprecated since version 0.2.13: This function is deprecated. Refer to this guide on retrieval and question answering with structured responses: https://python.langchain.com/v0.2/docs/how_to/qa_sources/#structure-sources-in-model-response

chains.openai_functions.tagging.create_tagging_chain(...)

Deprecated since version 0.2.13: LangChain has introduced a method called with_structured_output that is available on ChatModels capable of tool calling. See API reference for this function for replacement: <https://api.python.langchain.com/en/latest/chains/langchain.chains.openai_functions.tagging.create_tagging_chain.html> You can read more about with_structured_output here: <https://python.langchain.com/v0.2/docs/how_to/structured_output/>. If you notice other issues, please provide feedback here: <langchain-ai/langchain#18154>

chains.openai_functions.tagging.create_tagging_chain_pydantic(...)

Deprecated since version 0.2.13: LangChain has introduced a method called with_structured_output that is available on ChatModels capable of tool calling. See API reference for this function for replacement: <https://api.python.langchain.com/en/latest/chains/langchain.chains.openai_functions.tagging.create_tagging_chain_pydantic.html> You can read more about with_structured_output here: <https://python.langchain.com/v0.2/docs/how_to/structured_output/>. If you notice other issues, please provide feedback here: <langchain-ai/langchain#18154>

chains.openai_tools.extraction.create_extraction_chain_pydantic(...)

Deprecated since version 0.1.14: LangChain has introduced a method called with_structured_output thatis available on ChatModels capable of tool calling.You can read more about the method here: <https://python.langchain.com/docs/modules/model_io/chat/structured_output/>. Please follow our extraction use case documentation for more guidelineson how to do information extraction with LLMs.<https://python.langchain.com/docs/use_cases/extraction/>. with_structured_output does not currently support a list of pydantic schemas. If this is a blocker or if you notice other issues, please provide feedback here:<langchain-ai/langchain#18154> Use ``

chains.qa_with_sources.loading.load_qa_with_sources_chain(llm)

Deprecated since version 0.2.13: This function is deprecated. Refer to this guide on retrieval and question answering with sources: https://python.langchain.com/v0.2/docs/how_to/qa_sources/

chains.query_constructor.base.load_query_constructor_chain(...)

Deprecated since version 0.2.13: Use load_query_constructor_runnable instead.

chains.question_answering.chain.load_qa_chain(llm)

Deprecated since version 0.2.13: This class is deprecated. See the following migration guides for replacements based on chain_type:

chains.structured_output.base.create_openai_fn_runnable(...)

Deprecated since version 0.1.14: LangChain has introduced a method called with_structured_output that is available on ChatModels capable of tool calling. You can read more about the method here: <https://python.langchain.com/docs/modules/model_io/chat/structured_output/>. Please follow our extraction use case documentation for more guidelines on how to do information extraction with LLMs. <https://python.langchain.com/docs/use_cases/extraction/>. If you notice other issues, please provide feedback here: <langchain-ai/langchain#18154> Use ``

chains.structured_output.base.create_structured_output_runnable(...)

Deprecated since version 0.1.17: LangChain has introduced a method called with_structured_output that is available on ChatModels capable of tool calling. You can read more about the method here: <https://python.langchain.com/docs/modules/model_io/chat/structured_output/>.Please follow our extraction use case documentation for more guidelines on how to do information extraction with LLMs. <https://python.langchain.com/docs/use_cases/extraction/>. If you notice other issues, please provide feedback here: <langchain-ai/langchain#18154> Use ``