@langchain/core contains the core abstractions and schemas of LangChain.js, including base classes for language models,
chat models, vectorstores, retrievers, and runnables.
pnpm install @langchain/core
@langchain/core contains the base abstractions that power the rest of the LangChain ecosystem.
These abstractions are designed to be as modular and simple as possible.
Examples of these abstractions include those for language models, document loaders, embedding models, vectorstores, retrievers, and more.
The benefit of having these abstractions is that any provider can implement the required interface and then easily be used in the rest of the LangChain ecosystem.
For example, you can install other provider-specific packages like this:
pnpm install @langchain/openai
And use them as follows:
import { StringOutputParser } from "@langchain/core/output_parsers";
import { ChatPromptTemplate } from "@langchain/core/prompts";
import { ChatOpenAI } from "@langchain/openai";
const prompt = ChatPromptTemplate.fromTemplate(
`Answer the following question to the best of your ability:\n{question}`
);
const model = new ChatOpenAI({
model: "gpt-4o-mini",
temperature: 0.8,
});
const outputParser = new StringOutputParser();
const chain = prompt.pipe(model).pipe(outputParser);
const stream = await chain.stream({
question: "Why is the sky blue?",
});
for await (const chunk of stream) {
console.log(chunk);
}
/*
The
sky
appears
blue
because
of
a
phenomenon
known
as
Ray
leigh
scattering
*/
Note that for compatibility, all used LangChain packages (including the base LangChain package, which itself depends on core!) must share the same version of @langchain/core.
This means that you may need to install/resolve a specific version of @langchain/core that matches the dependencies of your used packages.
Other LangChain packages should add this package as a dependency and extend the classes within. For an example, see the @langchain/anthropic in this repo.
Because all used packages must share the same version of core, packages should never directly depend on @langchain/core. Instead they should have core as a peer dependency and a dev dependency. We suggest using a tilde dependency to allow for different (backwards-compatible) patch versions:
{
"name": "@langchain/anthropic",
"version": "0.0.3",
"description": "Anthropic integrations for LangChain.js",
"type": "module",
"author": "LangChain",
"license": "MIT",
"dependencies": {
"@anthropic-ai/sdk": "^0.10.0"
},
"peerDependencies": {
"@langchain/core": "~0.3.0"
},
"devDependencies": {
"@langchain/core": "~0.3.0"
}
}
We suggest making all packages cross-compatible with ESM and CJS using a build step like the one in
@langchain/anthropic, then running pnpm build before running npm publish.
Because @langchain/core is a low-level package whose abstractions will change infrequently, most contributions should be made in the higher-level LangChain package.
Bugfixes or suggestions should be made using the same guidelines as the main package. See here for detailed information.
Please report any security issues or concerns following our security guidelines.
Base class for all caches. All caches should extend this class.
A cache for storing LLM generations that stores data in memory.
Abstract base class for creating callback handlers in the LangChain framework. It provides a set of optional methods that can be overridden in derived classes to handle various events during the execu
Manage callbacks from different components of LangChain.
Base class for run manager in LangChain.
Base class for run manager in LangChain.
Base class for run manager in LangChain.
Manages callbacks for retriever runs.
Base class for run manager in LangChain.
Base class for all chat message histories. All chat message histories should extend this class.
Base class for all list chat message histories. All list chat message histories should extend this class.
Class for storing chat message history in-memory. It extends the BaseListChatMessageHistory class and provides methods to get, add, and clear messages.
Abstract class that provides a default implementation for the loadAndSplit() method from the DocumentLoader interface. The load() method is left abstract and needs to be implemented by subclasses.
Document loader integration with LangSmith.
Constructor args
Abstract base class for document transformation systems.
A document transformation system takes an array of Documents and returns an array of transformed Documents. These arrays do not necessarily ha
Interface for interacting with a document.
Class for document transformers that return exactly one transformed document for each input document.
An abstract class that provides methods for embedding documents and queries using LangChain.
Error class representing a context window overflow in a language model operation.
This error is thrown when the combined input to a language model (such as prompt tokens, historical messages, and/or
Base error class for all LangChain errors.
All LangChain error classes should extend this class (directly or
indirectly). Use LangChainError.isInstance(obj) to check if an
object is any LangChain e
Error class representing an aborted model operation in LangChain.
This error is thrown when a model operation (such as invocation, streaming, or batching) is cancelled before it completes, commonly d
Base class for example selectors.
Abstract class that defines the interface for selecting a prompt for a given language model.
Concrete implementation of BasePromptSelector that selects a prompt
based on a set of conditions. It has a default prompt that it returns
if none of the conditions are met.
A specialized example selector that selects examples based on their length, ensuring that the total length of the selected examples does not exceed a specified maximum length.
Class that selects examples based on semantic similarity. It extends the BaseExampleSelector class.
HashedDocument is a Document with hashes calculated. Hashes are calculated based on page content and metadata. It is used for indexing.
Base class for language models, chains, tools.
Base class for language models.
Base class for chat models. It extends the BaseLanguageModel class and provides methods for generating chat based on input messages.
An abstract class that extends BaseChatModel and provides a simple implementation of _generate.
LLM Wrapper. Takes in a prompt (or prompts) and returns a string.
LLM class that provides a simpler interface to subclass than BaseLLM.
Requires only implementing a simpler _call method instead of _generate.
The main stream object returned by chat model streaming.
Implements AsyncIterable<ChatModelStreamEvent> for raw event access
and PromiseLike<AIMessage> for simple await usage.
Typed stream for reasoning content (chain-of-thought). Same interface as TextContentStream but for reasoning blocks.
Typed stream for text content.
.full: yields the running accumulated text after each deTyped stream for tool calls.
ToolCall objects as each completes..full: yields the accumulated array after each newTyped stream for usage metadata.
Abstract base class for memory in LangChain's Chains. Memory refers to the state in Chains. It can be used to store information about past executions of a Chain and inject that information into the in
Base class for all types of messages in a conversation. It includes
properties like content, name, and additional_kwargs. It also
includes methods like toDict() and _getType().
Represents a chunk of an AI message, which can be concatenated with other AI message chunks.
Base class for all types of messages in a conversation. It includes
properties like content, name, and additional_kwargs. It also
includes methods like toDict() and _getType().
Represents a chunk of a message, which can be concatenated with other
message chunks. It includes a method _merge_kwargs_dict() for merging
additional keyword arguments from another `BaseMessageChun
Represents a chat message in a conversation.
Represents a chunk of a chat message, which can be concatenated with other chat message chunks.
Represents a function message in a conversation.
Represents a chunk of a function message, which can be concatenated with other function message chunks.
Represents a human message in a conversation.
Represents a chunk of a human message, which can be concatenated with other human message chunks.
Message responsible for deleting other messages.
RemoveMessage is intentionally not generic over MessageStructure.
Its content is always [] (empty), so carrying a structure type parameter
would
Represents a system message in a conversation.
Represents a chunk of a system message, which can be concatenated with other system message chunks.
Represents a tool message in a conversation.
Represents a chunk of a tool message, which can be concatenated with other tool message chunks.
Represents a tool message in a conversation.
Represents a chunk of a tool message, which can be concatenated with other tool message chunks.
A type of StructuredOutputParser that handles asymmetric input and
output schemas.
A base class for output parsers that can handle streaming input. It
extends the BaseTransformOutputParser class and provides a method for
converting parsed outputs into a diff format.
Abstract base class for parsing the output of a Large Language Model (LLM) call. It provides methods for parsing the result of an LLM call and invoking the parser with a given input.
Class to parse the output of an LLM call.
Class to parse the output of an LLM call that also allows streaming inputs.
OutputParser that parses LLMResult into the top likely string and encodes it into bytes.
Class to parse the output of an LLM call as a comma-separated list.
Class to parse the output of an LLM call to a list with a specific length and separator.
A specific type of StructuredOutputParser that parses JSON data
formatted as a markdown code snippet.
Class for parsing the output of an LLM into a JSON object.
Class to parse the output of an LLM call to a list.
Class to parse the output of an LLM call to a list.
Class to parse the output of an LLM call to a list.
Exception that output parsers should raise to signify a parsing error.
This exists to differentiate parsing errors from other code or execution errors that also may arise inside the output parser. Ou
Class to parse the output of an LLM call.
OutputParser that parses LLMResult into the top likely string.
Class to parse the output of an LLM call.
A base class for output parsers that can handle streaming input. It
extends the BaseTransformOutputParser class and provides a method for
converting parsed outputs into a diff format.
Class for parsing the output of an LLM into a JSON object and returning
a specific attribute. Uses an instance of JsonOutputFunctionsParser
to parse the output.
Class for parsing the output of an LLM into a JSON object. Uses an
instance of OutputFunctionsParser to parse the output.
Class for parsing the output of an LLM. Can be configured to return only the arguments of the function call in the output.
Class for parsing the output of a tool-calling LLM into a JSON object if you are expecting only a single tool to be called.
Class for parsing the output of a tool-calling LLM into a JSON object.
Output of a single generation.
Chunk of a single generation. Used for streaming.
Base PromptValue class. All prompt values should extend this class.
Class that represents a chat prompt value. It extends the BasePromptValue and includes an array of BaseMessage instances.
Class that represents an image prompt value. It extends the BasePromptValue and includes an ImageURL instance.
Represents a prompt value as a string. It extends the BasePromptValue class and overrides the toString and toChatMessages methods.
Class that represents an AI message prompt template. It extends the BaseMessageStringPromptTemplate.
Abstract class that serves as a base for creating chat prompt templates. It extends the BasePromptTemplate.
Abstract class that serves as a base for creating message prompt templates. It defines how to format messages for different roles in a conversation.
Abstract class that serves as a base for creating message string prompt templates. It extends the BaseMessagePromptTemplate.
Base class for prompt templates. Exposes a format method that returns a string prompt given a set of input values.
Base class for string prompt templates. It extends the BasePromptTemplate class and overrides the formatPromptValue method to return a StringPromptValue.
Class that represents a chat message prompt template. It extends the BaseMessageStringPromptTemplate.
Class that represents a chat prompt. It extends the BaseChatPromptTemplate and uses an array of BaseMessagePromptTemplate instances to format a series of messages for a conversation.
A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or transformed.
Chat prompt template that contains few-shot examples.
Prompt template that contains few-shot examples.
Class that represents a human message prompt template. It extends the BaseMessageStringPromptTemplate.
An image prompt template for a multimodal model.
Class that represents a placeholder for messages in a chat prompt. It extends the BaseMessagePromptTemplate.
Class that handles a sequence of prompts, each of which may require different input variables. Includes methods for formatting these prompts, extracting required input values, and handling partial pro
Schema to represent a basic prompt for an LLM.
Interface for the input of a ChatPromptTemplate.
Class that represents a system message prompt template. It extends the BaseMessageStringPromptTemplate.
Abstract base class for a document retrieval system, designed to process string queries and return the most relevant documents from a source.
BaseRetriever provides common properties and methods fo
Base Document Compression class. All compressors should extend this class.
A runnable that routes to a set of runnables based on Input['key']. Returns the output of the selected runnable.
A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or transformed.
A runnable that assigns key-value pairs to inputs of type Record<string, unknown>.
Wraps a runnable and applies partial config upon invocation.
Class that represents a runnable branch. The RunnableBranch is initialized with an array of branches and a default branch. When invoked, it evaluates the condition of each branch in order and executes
A runnable that delegates calls to another runnable with each element of the input sequence.
A runnable that wraps an arbitrary function that takes a single argument.
A runnable that runs a mapping of runnables in parallel, and returns a mapping of their outputs.
A runnable that runs a mapping of runnables in parallel, and returns a mapping of their outputs.
A runnable to passthrough inputs unchanged or with additional keys.
This runnable behaves almost like the identity function, except that it can be configured to add additional keys to the output, if
A runnable that assigns key-value pairs to inputs of type Record<string, unknown>.
Useful for streaming, can be automatically created and chained by calling runnable.pick();.
Base class for runnables that can be retried a specified number of times.
A sequence of runnables, where the output of each is the input of the next.
Wraps a runnable and applies partial config upon invocation.
A Runnable that can fallback to other Runnables if it fails. External APIs (e.g., APIs for a language model) may at times experience degraded performance or even downtime.
In these cases, it can be u
Abstract interface for a key-value store.
In-memory implementation of the BaseStore using a dictionary. Used for storing key-value pairs in memory.
Abstract class that provides a blueprint for creating specific translator classes. Defines two abstract methods: formatFunction and mergeFilters.
Class that extends the BaseTranslator class and provides concrete implementations for the abstract methods. Also declares three types: VisitOperationOutput, VisitComparisonOutput, and VisitStructuredQ
Class representing a comparison filter directive. It extends the FilterDirective class.
Abstract class representing an expression. Subclasses must implement the exprName property and the accept method.
Abstract class representing a filter directive. It extends the Expression class.
A class that extends BaseTranslator to translate structured queries
into functional filters.
Class representing an operation filter directive. It extends the FilterDirective class.
Class representing a structured query expression. It extends the Expression class.
Abstract class for visiting expressions. Subclasses must implement visitOperation, visitComparison, and visitStructuredQuery methods.
A fake chat model for testing, created via fakeModel.
Queue responses with .respond() and .respondWithTools(), then
pass the instance directly wherever a chat model is expected.
Responses are con
Abstract base class for toolkits in LangChain. Toolkits are collections
of tools that agents can use. Subclasses must implement the tools
property to provide the specific tools for the toolkit.
A tool that can be created dynamically from a function, name, and description, designed to work with structured data. It extends the StructuredTool class and overrides the _call method to execute the
A tool that can be created dynamically from a function, name, and description.
Base class for Tools that accept input of any shape defined by a Zod schema.
Base class for Tools that accept input as a string.
Custom error class used to handle exceptions related to tool input parsing.
It extends the built-in Error class and adds an optional output
property that can hold the output that caused the except
Abstract base class for creating callback handlers in the LangChain framework. It provides a set of optional methods that can be overridden in derived classes to handle various events during the execu
A tracer that logs all events to the console. It extends from the
BaseTracer class and overrides its methods to provide custom logging
functionality.
Class that extends the BaseTracer class from the
langchain.callbacks.tracers.base module. It represents a callback
handler that logs the execution of runs and emits RunLog instances to a
`RunLog
List of jsonpatch JSONPatchOperations, which describe how to create the run state from an empty dict. This is the minimal representation of the log, designed to be serialized as JSON and sent over the
List of jsonpatch JSONPatchOperations, which describe how to create the run state from an empty dict. This is the minimal representation of the log, designed to be serialized as JSON and sent over the
A callback handler that collects traced runs and makes it easy to fetch the traced run object from calls through any langchain object. For instance, it makes it easy to fetch the run ID and then do th
Interface for the input parameters of the BaseCallbackHandler class. It allows to specify which types of events should be ignored by the callback handler.
A class that can be used to make async calls with concurrency and retry logic.
This is useful for making calls to any kind of "expensive" external resource, be it because it's rate-limited, subject t
Base class for all chat message histories. All chat message histories should extend this class.
Base class for chat models. It extends the BaseLanguageModel class and provides methods for generating chat based on input messages.
A class that provides fake embeddings by overriding the embedDocuments and embedQuery methods to return fixed values.
Base class for all list chat message histories. All list chat message histories should extend this class.
A fake Chat Model that returns a predefined list of responses. It can be used for testing purposes.
LLM class that provides a simpler interface to subclass than BaseLLM.
Requires only implementing a simpler _call method instead of _generate.
Abstract base class for a document retrieval system, designed to process string queries and return the most relevant documents from a source.
BaseRetriever provides common properties and methods fo
A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or transformed.
Parser for comma-separated values. It splits the input text by commas and trims the resulting values.
Base class for chat models. It extends the BaseLanguageModel class and provides methods for generating chat based on input messages.
LLM class that provides a simpler interface to subclass than BaseLLM.
Requires only implementing a simpler _call method instead of _generate.
Base class for Tools that accept input of any shape defined by a Zod schema.
Abstract base class for creating callback handlers in the LangChain framework. It provides a set of optional methods that can be overridden in derived classes to handle various events during the execu
Class that extends VectorStore to store vectors in memory. Provides
methods for adding documents, performing similarity searches, and
creating instances from texts, documents, or an existing index.
Abstract base class for creating callback handlers in the LangChain framework. It provides a set of optional methods that can be overridden in derived classes to handle various events during the execu
A class that provides synthetic embeddings by overriding the embedDocuments and embedQuery methods to generate embeddings based on the input documents. The embeddings are generated by converting each
Abstract class extending VectorStore that defines a contract for saving
and loading vector store instances.
The SaveableVectorStore class allows vector store implementations to
persist their data
Abstract class representing a vector storage system for performing similarity searches on embedded documents.
VectorStore provides methods for adding precomputed vectors or documents,
removing docu
Class for retrieving documents from a VectorStore based on vector similarity
or maximal marginal relevance (MMR).
VectorStoreRetriever extends BaseRetriever, implementing methods for
adding doc
Wraps a LCEL chain and manages history. It appends input messages and chain outputs as history, and adds the current history messages to the chain input.
Dispatch a custom event.
Note: this method is only supported in non-web environments due to usage of async_hooks to infer config.
If you are using this method in the browser, please import and use f
Dispatch a custom event. Requires an explicit config object.
Waits for all promises in the queue to resolve. If the queue is undefined, it immediately resolves a promise.
Consume a promise, either adding it to the queue or waiting for it to resolve
Get the value of a previously set context variable. Context variables are scoped to any child runnables called by the current runnable, or globally if set outside of any runnable.
Register a callback configure hook to automatically add callback handlers to all runs.
There are two ways to use this:
contextVar to specify the variable nameSet a context variable. Context variables are scoped to any child runnables called by the current runnable, or globally if set outside of any runnable.
Read an error's retryability mark.
Mark an error as safe or unsafe to retry.
Sets a non-enumerable symbol on the error itself, leaving its class and shape untouched, so it is safe to apply to a provider SDK's own error.
Type guard function that checks if a given language model is of type
BaseChatModel.
Type guard function that checks if a given language model is of type
BaseLLM.
Index data from the doc source into the vector store.
Indexing functionality uses a manager to keep track of which documents are in the vector store.
This allows us to keep track of which documents
Get the context window size (max input tokens) for a given model.
Context window sizes are sourced from official model documentation:
Whether or not the input matches the OpenAI tool definition.
Convert an async iterable of legacy ChatGenerationChunks into
ChatModelStreamEvents with typed deltas.
Finalize a content block for the finish event. For tool calls, parse the accumulated JSON args string.
Convert an async iterable of OpenAI Chat Completions-shaped stream chunks into
LangChain ChatModelStreamEvents with typed deltas.
Pipes an LLM through an output parser, optionally wrapping the result to include the raw LLM response alongside the parsed output.
When includeRaw is true, returns `{ raw: BaseMessage, parsed: RunO
Creates the appropriate content-based output parser for a schema. Use this for jsonMode/jsonSchema methods where the LLM returns JSON text.
Creates the appropriate tool-calling output parser for a schema. Use this for function calling / tool use methods where the LLM returns structured tool calls.
Load a LangChain object from a JSON string.
WARNING — insecure deserialization risk. This function instantiates
classes and invokes constructors based on the contents of text. If text
origina
Get a unique name for the module, rather than parent class implementations. Should not be subclassed, subclass lc_name above instead.
This function is used by memory classes to select the input value to use for the memory. If there is only one input value, it is used. If there are multiple input values, the inputKey must be specifie
This function is used by memory classes to select the output value to use for the memory. If there is only one output value, it is used. If there are multiple output values, the outputKey must be spec
Function used by memory classes to get the key of the prompt input, excluding any keys that are memory variables or the "stop" key. If there is not exactly one prompt input key, an error is thrown.
'Merge' two statuses. If either value passed is 'error', it will return 'error'. Else it will return 'success'.
Collapses an array of tool call chunks into complete tool calls.
This function groups tool call chunks by their id and/or index, then attempts to parse and validate the accumulated arguments for each
The default text splitter function that splits text by newlines.
Filter messages based on name, type or id.
This function is used by memory classes to get a string representation of the chat message history, based on the message content and role.
Produces compact output like:
Human: What's the weather?
Confirm whether the inputted tool is an instance of StructuredToolInterface.
Confirm whether the inputted tool is an instance of StructuredToolInterface.
Incremental encoded data. Append data to the active multimodal block's
data field.