This package contains the LangChain.js integrations for OpenAI through their SDK.
npm install @langchain/openai @langchain/core
This package, along with the main LangChain package, depends on @langchain/core.
If you are using this package with other LangChain packages, you should make sure that all of the packages depend on the same instance of @langchain/core.
You can do so by adding appropriate fields to your project's package.json like this:
{
"name": "your-project",
"version": "0.0.0",
"dependencies": {
"@langchain/core": "^0.3.0",
"@langchain/openai": "^0.0.0"
},
"resolutions": {
"@langchain/core": "^0.3.0"
},
"overrides": {
"@langchain/core": "^0.3.0"
},
"pnpm": {
"overrides": {
"@langchain/core": "^0.3.0"
}
}
}
The field you need depends on the package manager you're using, but we recommend adding a field for the common pnpm, npm, and yarn to maximize compatibility.
This package contains the ChatOpenAI class, which is the recommended way to interface with the OpenAI series of models.
To use, install the requirements, and configure your environment.
export OPENAI_API_KEY=your-api-key
Then initialize
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-1106-preview",
});
const response = await model.invoke(new HumanMessage("Hello world!"));
import { ChatOpenAI } from "@langchain/openai";
const model = new ChatOpenAI({
apiKey: process.env.OPENAI_API_KEY,
model: "gpt-4-1106-preview",
});
const response = await model.stream(new HumanMessage("Hello world!"));
This package provides LangChain-compatible wrappers for OpenAI's built-in tools for the Responses API.
The web search tool allows OpenAI models to search the web for up-to-date information before generating a response. Web search supports three main types:
o3-deep-research or gpt-5 with high reasoning effortimport { ChatOpenAI, tools } from "@langchain/openai";
const model = new ChatOpenAI({
model: "gpt-4o",
});
// Basic usage
const response = await model.invoke(
"What was a positive news story from today?",
{
tools: [tools.webSearch()],
}
);
Domain filtering - Limit search results to specific domains (up to 100):
const response = await model.invoke("Latest AI research news", {
tools: [
tools.webSearch({
filters: {
allowedDomains: ["arxiv.org", "nature.com", "science.org"],
},
}),
],
});
User location - Refine search results based on geography:
const response = await model.invoke("What are the best restaurants near me?", {
tools: [
tools.webSearch({
userLocation: {
type: "approximate",
country: "US",
city: "San Francisco",
region: "California",
timezone: "America/Los_Angeles",
},
}),
],
});
Cache-only mode - Disable live internet access:
const response = await model.invoke("Find information about OpenAI", {
tools: [
tools.webSearch({
externalWebAccess: false,
}),
],
});
For more information, see OpenAI's Web Search Documentation.
The MCP tool allows OpenAI models to connect to remote MCP servers and OpenAI-maintained service connectors, giving models access to external tools and services.
There are two ways to use MCP tools:
Remote MCP server - Connect to any MCP-compatible server:
import { ChatOpenAI, tools } from "@langchain/openai";
const model = new ChatOpenAI({ model: "gpt-4o" });
const response = await model.invoke("Roll 2d4+1", {
tools: [
tools.mcp({
serverLabel: "dmcp",
serverDescription: "A D&D MCP server for dice rolling",
serverUrl: "https://dmcp-server.deno.dev/sse",
requireApproval: "never",
}),
],
});
Service connectors - Use OpenAI-maintained connectors for popular services:
const response = await model.invoke("What's on my calendar today?", {
tools: [
tools.mcp({
serverLabel: "google_calendar",
connectorId: "connector_googlecalendar",
authorization: "<oauth-access-token>",
requireApproval: "never",
}),
],
});
For more information, see OpenAI's MCP Documentation.
The Code Interpreter tool allows models to write and run Python code in a sandboxed environment to solve complex problems.
Use Code Interpreter for:
import { ChatOpenAI, tools } from "@langchain/openai";
const model = new ChatOpenAI({ model: "gpt-4.1" });
// Basic usage with auto container (default 1GB memory)
const response = await model.invoke("Solve the equation 3x + 11 = 14", {
tools: [tools.codeInterpreter()],
});
Memory configuration - Choose from 1GB (default), 4GB, 16GB, or 64GB:
const response = await model.invoke(
"Analyze this large dataset and create visualizations",
{
tools: [
tools.codeInterpreter({
container: { memoryLimit: "4g" },
}),
],
}
);
With files - Make uploaded files available to the code:
const response = await model.invoke("Process the uploaded CSV file", {
tools: [
tools.codeInterpreter({
container: {
memoryLimit: "4g",
fileIds: ["file-abc123", "file-def456"],
},
}),
],
});
Explicit container - Use a pre-created container ID:
const response = await model.invoke("Continue working with the data", {
tools: [
tools.codeInterpreter({
container: "cntr_abc123",
}),
],
});
Note: Containers expire after 20 minutes of inactivity. While called "Code Interpreter", the model knows it as the "python tool" - for explicit prompting, ask for "the python tool" in your prompts.
For more information, see OpenAI's Code Interpreter Documentation.
The File Search tool allows models to search your files for relevant information using semantic and keyword search. It enables retrieval from a knowledge base of previously uploaded files stored in vector stores.
Prerequisites: Before using File Search, you must:
purpose: "assistants"import { ChatOpenAI, tools } from "@langchain/openai";
const model = new ChatOpenAI({ model: "gpt-4.1" });
const response = await model.invoke("What is deep research by OpenAI?", {
tools: [
tools.fileSearch({
vectorStoreIds: ["vs_abc123"],
// maxNumResults: 5, // Limit results for lower latency
// filters: { type: "eq", key: "category", value: "blog" }, // Metadata filtering
// filters: { type: "and", filters: [ // Compound filters (AND/OR)
// { type: "eq", key: "category", value: "technical" },
// { type: "gte", key: "year", value: 2024 },
// ]},
// rankingOptions: { scoreThreshold: 0.8, ranker: "auto" }, // Customize scoring
}),
],
});
Filter operators: eq (equals), ne (not equal), gt (greater than), gte (greater than or equal), lt (less than), lte (less than or equal).
For more information, see OpenAI's File Search Documentation.
The Image Generation tool allows models to generate or edit images using text prompts and optional image inputs. It leverages the GPT Image model and automatically optimizes text inputs for improved performance.
Use Image Generation for:
import { ChatOpenAI, tools } from "@langchain/openai";
const model = new ChatOpenAI({ model: "gpt-4o" });
// Basic usage - generate an image
const response = await model.invoke(
"Generate an image of a gray tabby cat hugging an otter with an orange scarf",
{ tools: [tools.imageGeneration()] }
);
// Access the generated image (base64-encoded)
const imageOutput = response.additional_kwargs.tool_outputs?.find(
(output) => output.type === "image_generation_call"
);
if (imageOutput?.result) {
const fs = await import("fs");
fs.writeFileSync("output.png", Buffer.from(imageOutput.result, "base64"));
}
Custom size and quality - Configure output dimensions and quality:
const response = await model.invoke("Draw a beautiful sunset over mountains", {
tools: [
tools.imageGeneration({
size: "1536x1024", // Landscape format (also: "1024x1024", "1024x1536", "auto")
quality: "high", // Quality level (also: "low", "medium", "auto")
}),
],
});
Output format and compression - Choose format and compression level:
const response = await model.invoke("Create a product photo", {
tools: [
tools.imageGeneration({
outputFormat: "jpeg", // Format (also: "png", "webp")
outputCompression: 90, // Compression 0-100 (for JPEG/WebP)
}),
],
});
Transparent background - Generate images with transparency:
const response = await model.invoke(
"Create a logo with transparent background",
{
tools: [
tools.imageGeneration({
background: "transparent", // Background type (also: "opaque", "auto")
outputFormat: "png",
}),
],
}
);
Streaming with partial images - Get visual feedback during generation:
const response = await model.invoke("Draw a detailed fantasy castle", {
tools: [
tools.imageGeneration({
partialImages: 2, // Number of partial images (0-3)
}),
],
});
Force image generation - Ensure the model uses the image generation tool:
const response = await model.invoke("A serene lake at dawn", {
tools: [tools.imageGeneration()],
tool_choice: { type: "image_generation" },
});
Multi-turn editing - Refine images across conversation turns:
// First turn: generate initial image
const response1 = await model.invoke("Draw a red car", {
tools: [tools.imageGeneration()],
});
// Second turn: edit the image
const response2 = await model.invoke(
[response1, new HumanMessage("Now change the car color to blue")],
{ tools: [tools.imageGeneration()] }
);
Prompting tips: Use terms like "draw" or "edit" for best results. For combining images, say "edit the first image by adding this element" instead of "combine" or "merge".
Supported models: gpt-4o, gpt-4o-mini, gpt-4.1, gpt-4.1-mini, gpt-4.1-nano, o3
For more information, see OpenAI's Image Generation Documentation.
The Computer Use tool allows models to control computer interfaces by simulating mouse clicks, keyboard input, scrolling, and more. It uses OpenAI's Computer-Using Agent (CUA) model to understand screenshots and suggest actions.
Beta: Computer use is in beta. Use in sandboxed environments only and do not use for high-stakes or authenticated tasks. Always implement human-in-the-loop for important decisions.
How it works: The tool operates in a continuous loop:
import { ChatOpenAI, tools } from "@langchain/openai";
const model = new ChatOpenAI({ model: "computer-use-preview" });
// With execute callback for automatic action handling
const computer = tools.computerUse({
displayWidth: 1024,
displayHeight: 768,
environment: "browser",
execute: async (action) => {
if (action.type === "screenshot") {
return captureScreenshot();
}
if (action.type === "click") {
await page.mouse.click(action.x, action.y, { button: action.button });
return captureScreenshot();
}
if (action.type === "type") {
await page.keyboard.type(action.text);
return captureScreenshot();
}
if (action.type === "scroll") {
await page.mouse.move(action.x, action.y);
await page.evaluate(
`window.scrollBy(${action.scroll_x}, ${action.scroll_y})`
);
return captureScreenshot();
}
// Handle other actions...
return captureScreenshot();
},
});
const llmWithComputer = model.bindTools([computer]);
const response = await llmWithComputer.invoke(
"Check the latest news on bing.com"
);
For more information, see OpenAI's Computer Use Documentation.
The Local Shell tool allows models to run shell commands locally on a machine you provide. Commands are executed inside your own runtime—the API only returns the instructions.
Security Warning: Running arbitrary shell commands can be dangerous. Always sandbox execution or add strict allow/deny-lists before forwarding commands to the system shell. Note: This tool is designed to work with Codex CLI and the
codex-mini-latestmodel.
import { ChatOpenAI, tools } from "@langchain/openai";
import { exec } from "child_process";
import { promisify } from "util";
const execAsync = promisify(exec);
const model = new ChatOpenAI({ model: "codex-mini-latest" });
// With execute callback for automatic command handling
const shell = tools.localShell({
execute: async (action) => {
const { command, env, working_directory, timeout_ms } = action;
const result = await execAsync(command.join(" "), {
cwd: working_directory ?? process.cwd(),
env: { ...process.env, ...env },
timeout: timeout_ms ?? undefined,
});
return result.stdout + result.stderr;
},
});
const llmWithShell = model.bindTools([shell]);
const response = await llmWithShell.invoke(
"List files in the current directory"
);
Action properties: The model returns actions with these properties:
command - Array of argv tokens to executeenv - Environment variables to setworking_directory - Directory to run the command intimeout_ms - Suggested timeout (enforce your own limits)user - Optional user to run the command asFor more information, see OpenAI's Local Shell Documentation.
The Shell tool allows models to run shell commands through your integration. Unlike Local Shell, this tool supports executing multiple commands concurrently and is designed for gpt-5.1.
Security Warning: Running arbitrary shell commands can be dangerous. Always sandbox execution or add strict allow/deny-lists before forwarding commands to the system shell.
Use cases:
pip install and pytestapply_patch for file operationsimport { ChatOpenAI, tools } from "@langchain/openai";
import { exec } from "node:child_process/promises";
const model = new ChatOpenAI({ model: "gpt-5.1" });
// With execute callback for automatic command handling
const shellTool = tools.shell({
execute: async (action) => {
const outputs = await Promise.all(
action.commands.map(async (cmd) => {
try {
const { stdout, stderr } = await exec(cmd, {
timeout: action.timeout_ms ?? undefined,
});
return {
stdout,
stderr,
outcome: { type: "exit" as const, exit_code: 0 },
};
} catch (error) {
const timedOut = error.killed && error.signal === "SIGTERM";
return {
stdout: error.stdout ?? "",
stderr: error.stderr ?? String(error),
outcome: timedOut
? { type: "timeout" as const }
: { type: "exit" as const, exit_code: error.code ?? 1 },
};
}
})
);
return {
output: outputs,
maxOutputLength: action.max_output_length,
};
},
});
const llmWithShell = model.bindTools([shellTool]);
const response = await llmWithShell.invoke(
"Find the largest PDF file in ~/Documents"
);
Action properties: The model returns actions with these properties:
commands - Array of shell commands to execute (can run concurrently)timeout_ms - Optional timeout in milliseconds (enforce your own limits)max_output_length - Optional maximum characters to return per commandReturn format: Your execute function should return a ShellResult:
interface ShellResult {
output: Array<{
stdout: string;
stderr: string;
outcome: { type: "exit"; exit_code: number } | { type: "timeout" };
}>;
maxOutputLength?: number | null; // Pass back from action if provided
}
For more information, see OpenAI's Shell Documentation.
The Apply Patch tool allows models to propose structured diffs that your integration applies. This enables iterative, multi-step code editing workflows where the model can create, update, and delete files in your codebase.
When to use:
Security Warning: Applying patches can modify files in your codebase. Always validate paths, implement backups, and consider sandboxing. Note: This tool is designed to work with
gpt-5.1model.
import { ChatOpenAI, tools } from "@langchain/openai";
import { applyDiff } from "@openai/agents";
import * as fs from "fs/promises";
const model = new ChatOpenAI({ model: "gpt-5.1" });
// With execute callback for automatic patch handling
const patchTool = tools.applyPatch({
execute: async (operation) => {
if (operation.type === "create_file") {
const content = applyDiff("", operation.diff, "create");
await fs.writeFile(operation.path, content);
return `Created ${operation.path}`;
}
if (operation.type === "update_file") {
const current = await fs.readFile(operation.path, "utf-8");
const newContent = applyDiff(current, operation.diff);
await fs.writeFile(operation.path, newContent);
return `Updated ${operation.path}`;
}
if (operation.type === "delete_file") {
await fs.unlink(operation.path);
return `Deleted ${operation.path}`;
}
return "Unknown operation type";
},
});
const llmWithPatch = model.bindTools([patchTool]);
const response = await llmWithPatch.invoke(
"Rename the fib() function to fibonacci() in lib/fib.py"
);
Operation types: The model returns operations with these properties:
create_file – Create a new file at path with content from diffupdate_file – Modify an existing file at path using V4A diff format in diffdelete_file – Remove a file at pathBest practices:
For more information, see OpenAI's Apply Patch Documentation.
This package also adds support for OpenAI's embeddings model.
import { OpenAIEmbeddings } from "@langchain/openai";
const embeddings = new OpenAIEmbeddings({
apiKey: process.env.OPENAI_API_KEY,
});
const res = await embeddings.embedQuery("Hello world");
To develop the OpenAI package, you'll need to follow these instructions:
pnpm install
pnpm build
Or from the repo root:
pnpm build --filter=@langchain/openai
Test files should live within a tests/ file in the src/ folder. Unit tests should end in .test.ts and integration tests should
end in .int.test.ts:
pnpm test
pnpm test:int
Run the linter & formatter to ensure your code is up to standard:
pnpm lint && pnpm format
If you add a new file to be exported, either import & re-export from src/index.ts, or add it to the exports field in the package.json file and run pnpm build to generate the new entrypoint.
Azure OpenAI chat model integration.
Setup:
Install @langchain/openai and set the following environment variables:
npm install @langchain/openai
export AZURE_OPENAI_API_KEY="your-api-key"
Wrapper around OpenAI large language models.
To use you should have the openai package installed, with the
OPENAI_API_KEY environment variable set.
To use with Azure, import the AzureOpenAI cl
Class for generating embeddings using the OpenAI API.
To use with Azure, import the AzureOpenAIEmbeddings class.
OpenAI chat model integration.
To use with Azure, import the AzureChatOpenAI class.
Setup:
Install @langchain/openai and set an environment variable named OPENAI_API_KEY.
npm install
A tool for generating images with Open AIs Dall-E 2 or 3 API.
Wrapper around OpenAI large language models.
To use you should have the openai package installed, with the
OPENAI_API_KEY environment variable set.
To use with Azure, import the AzureOpenAI cl
Ask typed questions about text, messages or JSON state with the OpenAI Decisions
API (beta). Answer types are inferred from the questions passed to invoke.
Class for generating embeddings using the OpenAI API.
To use with Azure, import the AzureOpenAIEmbeddings class.
Convert an async iterable of OpenAI Chat Completions-shaped stream chunks into
LangChain ChatModelStreamEvents with typed deltas.
Convert a formatted LangChain prompt (e.g. pulled from the hub) into a format expected by OpenAI's JS SDK.
Requires the "@langchain/openai" package to be installed in addition to the OpenAI SDK.
This function generates an endpoint URL for (Azure) OpenAI based on the configuration parameters provided.
Normalizes various header formats into a consistent Record format.
This function accepts headers in multiple formats and converts them to a Record<string, HeaderValue | readonly HeaderValue[]> for co
Options for the Apply Patch tool.
Select one option. Keys are the option values; values describe when each applies.
Auto container configuration for Code Interpreter. Creates a new container automatically or reuses an active one.
Options for the Code Interpreter tool.
Input structure for the Computer Use tool.
The action is wrapped in an action property.
Options for the Computer Use tool.
An interface for the Dall-E API Wrapper.
One Decisions request: what to evaluate and the named questions to ask.
A filter used to compare a specified attribute key to a given value.
Combine multiple filters using and or or.
Weights for hybrid search balancing semantic and keyword matches.
Options for the File Search tool.
Ranking options for file search results.
Optional mask for inpainting. Allows you to specify areas of the image that should be regenerated.
Options for the Image Generation tool.
Options for the Local Shell tool.
Filter object for approval requirements.
Options for connecting to an OpenAI-maintained service connector.
Options for connecting to a remote MCP server via URL.
Filter object to specify which tools are allowed.
Interface for OpenAIEmbeddings parameters. Extends EmbeddingsParams and defines additional parameters specific to the OpenAIEmbeddings class.
Input to OpenAI class.
Estimate the probability that a condition is true.
Rate the input against two or more levels, ordered lowest to highest.
Options for the Shell tool.
Result of executing shell commands. Contains an array of outputs (one per command) and the max_output_length parameter.
Domain filtering configuration for web search.
Options for the OpenAI web search tool.
The answer type a question produces.
Answers keyed by question name, typed from the questions that were asked.
Re-export operation types from OpenAI SDK for convenience.
Union type of all apply patch operations from OpenAI SDK.
OpenAI Apply Patch tool type for the Responses API.
The summary of a model's reasoning step.
Memory limit options for the Code Interpreter container. Higher tiers offer more RAM and are billed at different rates.
OpenAI Code Interpreter tool type for the Responses API.
Union type of all computer use actions from OpenAI SDK.
Re-export action types from OpenAI SDK for convenience.
The type of computer environment to control.
OpenAI Computer Use tool type for the Responses API.
Text, LangChain messages, or JSON state to evaluate. Messages are sent as a native
conversation; JSON objects, which may contain messages, as an input_json part.
Comparison operators for file attribute filtering.
Filter type for file search - can be a comparison or compound filter.
OpenAI File Search tool type for the Responses API.
OpenAI Image Generation tool type for the Responses API.
One level of a Score rubric.
Re-export action type from OpenAI SDK for convenience. The action contains command details like argv tokens, environment variables, working directory, timeout, and user.
OpenAI Local Shell tool type for the Responses API.
Available connector IDs for OpenAI's built-in service connectors. These are OpenAI-maintained MCP wrappers for popular services.
OpenAI MCP tool type for the Responses API.
score is the probability-weighted average of the zero-based level indices.
Re-export action type from OpenAI SDK for convenience. The action contains command details like commands array, timeout, and max output length.
Outcome type for shell command execution - either exit with code or timeout.
Result of a single shell command execution. Contains stdout, stderr, and the outcome (exit code or timeout).
OpenAI Shell tool type for the Responses API.
OpenAI web search tool type for the Responses API.