Looking for the JS/TS version? Check out LangChain.js.
To help you ship LangChain apps to production faster, check out LangSmith. LangSmith is a unified developer platform for building, testing, and monitoring LLM applications.
uv add langchain-classic
Legacy chains, langchain-community re-exports, indexing API, deprecated functionality, and more.
In most cases, you should be using the main langchain package.
For full documentation, see the API reference. For conceptual guides, tutorials, and examples on using LangChain, see the LangChain Docs.
See our Releases and Versioning policies.
As an open-source project in a rapidly developing field, we are extremely open to contributions, whether it be in the form of a new feature, improved infrastructure, or better documentation.
For detailed information on how to contribute, see the Contributing Guide.
A utility to experiment with and compare the performance of different models.
Tool that is run when invalid tool name is encountered by agent.
Base Single Action Agent class.
Base Multi Action Agent class.
Base class for parsing agent output into agent action/finish.
Base class for parsing agent output into agent actions/finish.
This is used for agents that can return multiple actions.
Agent powered by Runnables.
Agent powered by Runnables.
Tool that just returns the query.
Agent that is using tools.
Iterator for AgentExecutor.
Chat prompt template for the agent scratchpad.
AgentFinish with run and thread metadata.
AgentAction with info needed to submit custom tool output to existing run.
Run an OpenAI Assistant.
Output parser for the conversational agent.
Output parser for the chat agent.
Output parser for the conversational agent.
Output parser for the ReAct agent.
Memory used to save agent output AND intermediate steps.
Configuration for a chain to use in MRKL system.
MRKL Output parser for the chat agent.
Tool agent action.
Parses a message into agent actions/finish.
If a tool_calls parameter is passed, then that is used to get the tool names and tool inputs.
If one is not passed, then the AIMessage is assumed to be th
Parses a message into agent actions/finish.
Is meant to be used with OpenAI models, as it relies on the specific tool_calls parameter from OpenAI to convey what tools to use.
If a tool_calls paramet
Parses a message into agent action/finish.
Is meant to be used with OpenAI models, as it relies on the specific function_call parameter from OpenAI to convey what tools to use.
If a function_call pa
Parses self-ask style LLM calls.
Expects output to be in one of two formats.
If the output signals that an action should be taken, should be in the below format. This will result in an AgentAction b
Parses ReAct-style LLM calls that have a single tool input.
Expects output to be in one of two formats.
If the output signals that an action should be taken, should be in the below format. This will
Parses tool invocations and final answers in JSON format.
Expects output to be in one of two formats.
If the output signals that an action should be taken, should be in the below format. This will r
Parses tool invocations and final answers from XML-formatted agent output.
This parser extracts structured information from XML tags to determine whether an agent should perform a tool action or prov
Parses ReAct-style LLM calls that have a single tool input in json format.
Expects output to be in one of two formats.
If the output signals that an action should be taken, should be in the below fo
Output parser for the structured chat agent.
Output parser with retries for the structured chat agent.
Information about a VectorStore.
Toolkit for interacting with a VectorStore.
Toolkit for routing between Vector Stores.
Interface for caching results from embedding models.
The interface allows works with any store that implements the abstract store interface accepting keys of type str and values of list of floats.
I
Callback handler that returns an async iterator.
Only the final output of the agent will be iterated.
Callback handler that returns an async iterator.
Callback handler for streaming in agents.
Only works with agents using LLMs that support streaming.
Only the final output of the agent will be streamed.
Tracer that logs via the input Logger.
A function description for ChatOpenAI.
A runnable that routes to the selected function.
The types of the evaluators.
A base class for evaluators that use an LLM.
String evaluator interface.
Grade, tag, or otherwise evaluate predictions relative to their inputs and/or reference labels.
Compare the output of two models (or two outputs of the same model).
Interface for evaluating agent trajectories.
A named tuple containing the score and reasoning for a trajectory.
Trajectory output parser.
A chain for evaluating ReAct style agents.
This chain is used to evaluate ReAct style agents by reasoning about the sequence of actions taken and their outcomes. Based on the paper "ReAct: Synergizin
Compute a regex match between the prediction and the reference.
Examples:
evaluator = RegexMatchStringEvaluator(flags=re.IGNORECASE) evaluator.evaluate_strings( prediction=
Evaluate whether the prediction is valid JSON.
This evaluator checks if the prediction is a valid JSON string. It does not require any input or reference.
Json Equality Evaluator.
Evaluate whether the prediction is equal to the reference after parsing both as JSON.
This evaluator checks if the prediction, after parsing as JSON, is equal to the ref
An evaluator that validates a JSON prediction against a JSON schema reference.
This evaluator checks if a given JSON prediction conforms to the provided JSON schema. If the prediction is valid, the s
An evaluator that calculates the edit distance between JSON strings.
This evaluator computes a normalized Damerau-Levenshtein distance between two JSON strings after parsing them and converting them
A parser for the output of the PairwiseStringEvalChain.
Pairwise String Evaluation Chain.
A chain for comparing two outputs, such as the outputs of two models, prompts, or outputs of a single model on similar inputs.
Labeled Pairwise String Evaluation Chain.
A chain for comparing two outputs, such as the outputs of two models, prompts, or outputs of a single model on similar inputs, with labeled preferences.
LLM Chain for evaluating question answering.
LLM Chain for evaluating QA w/o GT based on context.
LLM Chain for evaluating QA using chain of thought reasoning.
LLM Chain for generating examples for question answering.
Compute an exact match between the prediction and the reference.
Examples:
evaluator = ExactMatchChain() evaluator.evaluate_strings( prediction="Mindy is the CTO",
Embedding Distance Metric.
Embedding distance evaluation chain.
Use embedding distances to score semantic difference between a prediction and reference.
Use embedding distances to score semantic difference between two predictions.
Examples:
chain = PairwiseEmbeddingDistanceEvalChain() result = chain.evaluate_string_pairs(prediction="Hello", p
A Criteria to evaluate.
A parser for the output of the CriteriaEvalChain.
LLM Chain for evaluating runs against criteria.
Parameters
llm : BaseLanguageModel The language model to use for evaluation. criteria : Union[Mapping[str, str]] The criteria or rub
Criteria evaluation chain that requires references.
Distance metric to use.
Compute string distances between the prediction and the reference.
Examples:
from langchain_classic.evaluation import StringDistanceEvalChain evaluator = StringDistanceEvalChain()
Compute string edit distances between two predictions.
A parser for the output of the ScoreStringEvalChain.
A chain for scoring on a scale of 1-10 the output of a model.
A chain for scoring the output of a model on a scale of 1-10.
Chain that transforms the chain output.
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., a
Pass input through a moderation endpoint.
To use, you should have the openai python package installed, and the
environment variable OPENAI_API_KEY set with your API key.
Any parameters that are
Chain where the outputs of one chain feed directly into next.
Simple chain where the outputs of one step feed directly into next.
Base class for prompt selectors.
Prompt collection that goes through conditionals.
Input type for ConversationalRetrievalChain.
Chain for chatting with an index.
Chain for chatting with a vector database.
A typed dictionary containing information about elements in the viewport.
A crawler for web pages.
Security Note: This is an implementation of a crawler that uses a browser via Playwright.
This crawler can be used to load arbitrary webpages INCLUDING content
Input for a SQL Chain.
Input for a SQL Chain.
Interface for loading the combine documents chain.
Question-answering with sources over a vector database.
Question-answering with sources over an index.
Interface for loading the combine documents chain.
A route to a destination chain.
Chain that outputs the name of a destination chain and the inputs to it.
Use a single chain to route an input to one of multiple candidate chains.
Chain that uses embeddings to route between options.
Parser for output of router chain in the multi-prompt chain.
Multi Retrieval QA Chain.
A multi-route chain that uses an LLM router chain to choose amongst retrieval qa chains.
Generate hypothetical document for query, and then embed that.
Based on https://arxiv.org/abs/2212.10496
Interface for loading the combine documents chain.
Raise an ImportError if APIChain is used without langchain_community.
An answer to the question, with sources.
Class representing a single statement.
Each fact has a body and a list of sources. If there are multiple facts make sure to break them apart such that each one only uses a set of sources that are rel
A question and its answer as a list of facts.
Each fact should have a source. Each sentence contains a body and a list of sources.
Chain for interacting with Elasticsearch Database.
Class for a constitutional principle.
Base interface for chains combining documents.
Subclasses of this chain deal with combining documents in a variety of ways. This base class exists to add some uniformity in the interface these types
Interface for the combine_docs method.
Interface for the combine_docs method.
Output parser that parses a structured query.
A date in ISO 8601 format (YYYY-MM-DD).
A datetime in ISO 8601 format (YYYY-MM-DDTHH:MM:SS).
Transform a query string into an intermediate representation.
Information about a data source attribute.
Chain that generates questions from uncertain spans.
Flare chain.
Chain that combines a retriever, a question generator, and a response generator.
See Active Retrieval Augmented Generation paper.
Output parser that checks if the output is finished.
Configuration for a given run evaluator.
Configuration for a run evaluator that only requires a single key.
Configuration for a run evaluation.
Configuration for a reference-free criteria evaluator.
Configuration for a labeled (with references) criteria evaluator.
Configuration for an embedding distance evaluator.
Configuration for a string distance evaluator.
Configuration for a QA evaluator.
Configuration for a context-based QA evaluator.
Configuration for a context-based QA evaluator.
Configuration for a json validity evaluator.
Configuration for a json equality evaluator.
Configuration for an exact match string evaluator.
Configuration for a regex match string evaluator.
Configuration for a score string evaluator.
This is like the criteria evaluator but it is configured by default to return a score on the scale from 1-10.
It is recommended to normalize these scores
Configuration for a labeled score string evaluator.
Extract items to evaluate from the run object.
Extract items to evaluate from the run object.
Extract items to evaluate from the run object from a chain.
Map an input to the tool.
Map an example, or row in the dataset, to the inputs of an evaluation.
Evaluate Run and optional examples.
Raised when the input format is invalid.
A dictionary of the results of a single test run.
Your architecture raised an error.
Input for a chat model.
A simple progress bar for the console.
Wraps a store with key and value encoders/decoders.
Examples that uses JSON for encoding/decoding:
import json
def key_encoder(key: int) -> str:
return json.dumps(key)
def value_seri
BaseStore interface that works on the local file system.
Input for the retry chain of the OutputFixingParser.
Wrap a parser and try to fix parsing errors.
Retry chain input for RetryOutputParser.
Retry chain input for RetryWithErrorOutputParser.
Wrap a parser and try to fix parsing errors.
Does this by passing the original prompt and the completion to another LLM, and telling it the completion did not satisfy criteria in the prompt.
Wrap a parser and try to fix parsing errors.
Does this by passing the original prompt, the completion, AND the error that was raised to another language model and telling it that the completion did n
Parse the output of an LLM call to a datetime.
Parse an output using Pandas DataFrame format.
Parse an output that is one of a set of values.
Parse the output of an LLM call into a Dictionary using a regex.
Parse the output of an LLM call using a regex.
Parse YAML output using a Pydantic model.
Parse the output of an LLM call to a boolean.
Combine multiple output parsers into one.
Schema for a response from a structured output parser.
Parse the output of an LLM call to a structured output.
Retriever that ensembles the multiple retrievers.
It uses a rank fusion.
Retrieve small chunks then retrieve their parent documents.
When splitting documents for retrieval, there are often conflicting desires:
Retriever that merges the results of multiple retrievers.
Enumerator of the types of search to perform.
Retriever that supports multiple embeddings per parent document.
This retriever is designed for scenarios where documents are split into smaller chunks for embedding and vector search, but retrieval
Retriever that wraps a base retriever and compresses the results.
Time Weighted Vector Store Retriever.
Retriever that combines embedding similarity with recency in retrieving values.
Output parser for a list of lines.
Given a query, use an LLM to write a set of queries.
Retrieve docs for each query. Return the unique union of all retrieved docs.
Given a query, use an LLM to re-phrase it.
Then, retrieve docs for the re-phrased query.
Self Query Retriever.
Retriever that uses a vector store and an LLM to generate the vector store queries.
Filter that drops documents that aren't relevant to the query.
Document compressor that uses CrossEncoder for reranking.
Document compressor that uses a pipeline of Transformers.
Embeddings Filter.
Document compressor that uses embeddings to drop documents unrelated to the query.
Parse outputs that could return a null string of some sort.
LLM Chain Extractor.
Document compressor that uses an LLM chain to extract the relevant parts of documents.
Document compressor that uses Zero-Shot Listwise Document Reranking.
Adapted from: https://arxiv.org/pdf/2305.02156.pdf
LLMListwiseRerank uses a language model to rerank a list of documents base
Combining multiple memories' data together.
Simple Memory.
Simple memory for storing context or other information that shouldn't ever change between prompts.
Conversation chat memory with token limit and vectordb backing.
load_memory_variables() will return a dict with the key "history". It contains background information retrieved from the vector store p
Memory wrapper that is read-only and cannot be changed.
Wrapper around a VectorStore for easy access.
Logic for creating indexes.
Table used to keep track of when a key was last updated.
A SQL Alchemy based implementation of the record manager.
Abstract base class for memory in Chains.
Memory refers to state in Chains. Memory can be used to store information about past executions of a Chain and inject that information into the inputs of
Base class for single action agents.
Agent that calls the language model and deciding the action.
This is driven by a LLMChain. The prompt in the LLMChain MUST include a variable called "agent_scratchpad" where the agent can put its int
An enum for agent types.
Agent for the self-ask-with-search paper.
[Deprecated] Chain that does self-ask with search.
An agent that holds a conversation in addition to using tools.
Agent driven by OpenAIs function powered API.
Agent that uses XML tags.
Chat Agent.
An agent designed to hold a conversation in addition to using tools.
Agent for the ReAct chain.
Class to assist with exploration of a document store.
Agent for the ReAct TextWorld chain.
[Deprecated] Chain that implements the ReAct paper.
An Agent driven by OpenAIs function powered API.
Agent for the MRKL chain.
Chain that implements the MRKL system.
Structured Chat Agent.
An instance of a runnable stored in the LangChain Hub.
Map-reduce chain.
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 StrOutputP
Chain for question-answering with self-verification.
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
Implement an LLM driven browser.
Security Note: This toolkit provides code to control a web-browser.
The web-browser can be used to navigate to:
- Any URL (including any internal networ
Chain to have a conversation and load context from memory.
This class is deprecated in favor of RunnableWithMessageHistory. Please refer
to this tutorial for more detail: https://python.langchain.c
Chain that interprets a prompt and executes python code to do math.
This class is deprecated. See below for a replacement implementation using LangGraph. The benefits of this impleme
Question answering chain with sources over documents.
Question answering with sources over documents.
Base class for question-answering chains.
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 impor
Chain for question-answering against a vector database.
A multi-route chain that uses an LLM router chain to choose amongst prompts.
This class is deprecated. See below for a replacement, which offers several benefits, including streaming and batch suppor
A router chain that uses an LLM chain to perform routing.
This class is deprecated. See below for a replacement, which offers several benefits, including streaming and batch support.
Below is an exa
Chain for making a simple request to an API endpoint.
Chain for question-answering with self-verification.
Base class for question-answer generation chains.
This class is deprecated. See below for an alternative implementation.
Advantages of this implementation include:
Chain for applying constitutional principles.
This class is deprecated. See below for a replacement implementation using LangGraph. The benefits of this implementation are:
Chain that splits documents, then analyzes it in pieces.
This chain is parameterized by a TextSplitter and a CombineDocumentsChain. This chain takes a single document as input, and then splits it up
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 res
Combine documents by recursively reducing them.
This involves
combine_documents_chaincollapse_documents_chaincombine_documents_chain is ALWAYS provided. This is final chain that is call
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
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
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 nam
Document compressor that uses Cohere Rerank API.
Abstract base class for chat memory.
ATTENTION This abstraction was created prior to when chat models had native tool calling capabilities. It does NOT support native tool calling cap
Abstract base class for Entity store.
In-memory Entity store.
Upstash Redis backed Entity store.
Entities get a TTL of 1 day by default, and that TTL is extended by 3 days every time the entity is read back.
Redis-backed Entity store.
Entities get a TTL of 1 day by default, and that TTL is extended by 3 days every time the entity is read back.
SQLite-backed Entity store with safe query construction.
Entity extractor & summarizer memory.
Extracts named entities from the recent chat history and generates summaries. With a swappable entity store, persisting entities across conversations. Defaults t
Conversation chat memory with token limit.
Keeps only the most recent messages in the conversation under the constraint that the total number of tokens in the conversation does not exceed a certain l
Vector Store Retriever Memory.
Store the conversation history in a vector store and retrieves the relevant parts of past conversation based on the input.
A basic memory implementation that simply stores the conversation history.
This stores the entire conversation history in memory without any additional processing.
Note that additional processing ma
A basic memory implementation that simply stores the conversation history.
This stores the entire conversation history in memory without any additional processing.
Equivalent to ConversationBufferMe
Mixin for summarizer.
Continually summarizes the conversation history.
The summary is updated after each conversation turn. The implementations returns a summary of the conversation history which can be used to provide co
Use to keep track of the last k turns of a conversation.
If the number of messages in the conversation is more than the maximum number of messages to keep, the oldest messages are dropped.
Buffer with summarizer for storing conversation memory.
Provides a running summary of the conversation together with the most recent messages in the conversation under the constraint that the total n
Parse an AI message potentially containing tool_calls.
Get the appropriate function output parser given the user functions.
Use a single chain to route an input to one of multiple retrieval qa chains.