@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
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
Base class for all list chat message histories. All list chat message
Class for storing chat message history in-memory. It extends the
Abstract class that provides a default implementation for the
Document loader integration with LangSmith.
Abstract base class for document transformation systems.
Interface for interacting with a document.
Class for document transformers that return exactly one transformed document
An abstract class that provides methods for embedding documents and
Error class representing a context window overflow in a language model operation.
Base error class for all LangChain errors.
Error class representing an aborted model operation in LangChain.
Base class for example selectors.
Abstract class that defines the interface for selecting a prompt for a
Concrete implementation of BasePromptSelector that selects a prompt
A specialized example selector that selects examples based on their
Class that selects examples based on semantic similarity. It extends
HashedDocument is a Document with hashes calculated.
Base class for language models, chains, tools.
Base class for language models.
Base class for chat models. It extends the BaseLanguageModel class and
An abstract class that extends BaseChatModel and provides a simple
LLM Wrapper. Takes in a prompt (or prompts) and returns a string.
LLM class that provides a simpler interface to subclass than BaseLLM.
Abstract base class for memory in LangChain's Chains. Memory refers to
Base class for all types of messages in a conversation. It includes
Represents a chunk of an AI message, which can be concatenated with
Base class for all types of messages in a conversation. It includes
Represents a chunk of a message, which can be concatenated with other
Represents a chat message in a conversation.
Represents a chunk of a chat message, which can be concatenated with
Represents a function message in a conversation.
Represents a chunk of a function message, which can be concatenated
Represents a human message in a conversation.
Represents a chunk of a human message, which can be concatenated with
Message responsible for deleting other messages.
Represents a system message in a conversation.
Represents a chunk of a system message, which can be concatenated with
Represents a tool message in a conversation.
Represents a chunk of a tool message, which can be concatenated
Represents a tool message in a conversation.
Represents a chunk of a tool message, which can be concatenated
A type of StructuredOutputParser that handles asymmetric input and
A base class for output parsers that can handle streaming input. It
Abstract base class for parsing the output of a Large Language Model
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
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
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.
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
Class for parsing the output of an LLM into a JSON object and returning
Class for parsing the output of an LLM into a JSON object. Uses an
Class for parsing the output of an LLM. Can be configured to return
Class for parsing the output of a tool-calling LLM into a JSON object if you are
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
Class that represents an image prompt value. It extends the
Represents a prompt value as a string. It extends the BasePromptValue
Class that represents an AI message prompt template. It extends the
Abstract class that serves as a base for creating chat prompt
Abstract class that serves as a base for creating message prompt
Abstract class that serves as a base for creating message string prompt
Base class for prompt templates. Exposes a format method that returns a
Base class for string prompt templates. It extends the
Class that represents a chat message prompt template. It extends the
Class that represents a chat prompt. It extends the
A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
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
An image prompt template for a multimodal model.
Class that represents a placeholder for messages in a chat prompt. It
Class that handles a sequence of prompts, each of which may require
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
Abstract base class for a document retrieval system, designed to
Base Document Compression class. All compressors should extend this class.
A runnable that routes to a set of runnables based on Input['key'].
A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
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
A runnable that delegates calls to another runnable
A runnable that wraps an arbitrary function that takes a single argument.
A runnable that runs a mapping of runnables in parallel,
A runnable that runs a mapping of runnables in parallel,
A runnable to passthrough inputs unchanged or with additional keys.
A runnable that assigns key-value pairs to inputs of type Record<string, unknown>.
Base class for runnables that can be retried a
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.
Wraps a LCEL chain and manages history. It appends input messages
Abstract interface for a key-value store.
In-memory implementation of the BaseStore using a dictionary. Used for
Abstract class that provides a blueprint for creating specific
Class that extends the BaseTranslator class and provides concrete
Class representing a comparison filter directive. It extends the
Abstract class representing an expression. Subclasses must implement
Abstract class representing a filter directive. It extends the
A class that extends BaseTranslator to translate structured queries
Class representing an operation filter directive. It extends the
Class representing a structured query expression. It extends the
Abstract class for visiting expressions. Subclasses must implement
Abstract base class for toolkits in LangChain. Toolkits are collections
A tool that can be created dynamically from a function, name, and
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.
Abstract base class for creating callback handlers in the LangChain
A tracer that logs all events to the console. It extends from the
Class that extends the BaseTracer class from the
List of jsonpatch JSONPatchOperations, which describe how to create the run state
List of jsonpatch JSONPatchOperations, which describe how to create the run state
A callback handler that collects traced runs and makes it easy to fetch the traced run object from calls through any langchain object.
Interface for the input parameters of the BaseCallbackHandler class. It
A class that can be used to make async calls with concurrency and retry logic.
Base class for all chat message histories. All chat message histories
Base class for chat models. It extends the BaseLanguageModel class and
A class that provides fake embeddings by overriding the embedDocuments
Base class for all list chat message histories. All list chat message
A fake Chat Model that returns a predefined list of responses. It can be used
LLM class that provides a simpler interface to subclass than BaseLLM.
Abstract base class for a document retrieval system, designed to
A Runnable is a generic unit of work that can be invoked, batched, streamed, and/or
Parser for comma-separated values. It splits the input text by commas
Base class for chat models. It extends the BaseLanguageModel class and
LLM class that provides a simpler interface to subclass than BaseLLM.
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
Class that extends VectorStore to store vectors in memory. Provides
Abstract base class for creating callback handlers in the LangChain
A class that provides synthetic embeddings by overriding the
Abstract class extending VectorStore that defines a contract for saving
Abstract class representing a vector storage system for performing
Class for retrieving documents from a VectorStore based on vector similarity
Dispatch a custom event.
Dispatch a custom event. Requires an explicit config object.
Waits for all promises in the queue to resolve. If the queue is
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
Register a callback configure hook to automatically add callback handlers to all runs.
Set a context variable. Context variables are scoped to any
Type guard function that checks if a given language model is of type
Type guard function that checks if a given language model is of type
Index data from the doc source into the vector store.
Get the context window size (max input tokens) for a given model.
Whether or not the input matches the OpenAI tool definition.
Load a LangChain object from a JSON string.
Get a unique name for the module, rather than parent class implementations.
This function is used by memory classes to select the input value
This function is used by memory classes to select the output value
Function used by memory classes to get the key of the prompt input,
Transforms an array of BaseMessage instances into an array of
Confirm whether the inputted tool is an instance of StructuredToolInterface.
Confirm whether the inputted tool is an instance of StructuredToolInterface.
Interface for the fields of a MessageStringPromptTemplate.