Skip to main content

OpenAI integrations

LangChain integrates with OpenAI and Azure OpenAI through the @langchain/openai package.

OpenAI is American artificial intelligence (AI) research laboratory consisting of the non-profit OpenAI Incorporated and its for-profit subsidiary corporation OpenAI Limited Partnership. OpenAI conducts AI research with the declared intention of promoting and developing a friendly AI. OpenAI systems run on an Azure-based supercomputing platform from Microsoft.

The OpenAI API is powered by a diverse set of models with different capabilities and price points.

ChatGPT is the Artificial Intelligence (AI) chatbot developed by OpenAI.

Installation and setup

  • Get an OpenAI api key and set it as an environment variable (OPENAI_API_KEY)

Chat model

See a usage example.

import { ChatOpenAI } from "@langchain/openai";

LLM

See a usage example.

npm install @langchain/openai @langchain/core
import { OpenAI } from "@langchain/openai";

Text embedding model

See a usage example

import { OpenAIEmbeddings } from "@langchain/openai";

Chain

import { OpenAIModerationChain } from "@langchain/classic/chains";

Middleware

Middleware specifically designed for OpenAI models. Learn more about middleware.

MiddlewareDescription
Content moderationModerate agent traffic using OpenAI's moderation endpoint

Content moderation

Moderate agent traffic (user input, model output, and tool results) using OpenAI's moderation endpoint to detect and handle unsafe content. Content moderation is useful for the following:

  • Applications requiring content safety and compliance
  • Filtering harmful, hateful, or inappropriate content
  • Customer-facing agents that need safety guardrails
  • Meeting platform moderation requirements

Info

Learn more about OpenAI's moderation models and categories.

API reference: openAIModerationMiddleware

import { createAgent, openAIModerationMiddleware } from "langchain";

const agent = createAgent({
  model: "openai:gpt-5.5",
  tools: [searchTool, databaseTool],
  middleware: [
    openAIModerationMiddleware({
      model: "openai:gpt-5.5",
      moderationModel: "omni-moderation-latest",
      checkInput: true,
      checkOutput: true,
      exitBehavior: "end",
    }),
  ],
});