Middleware that moderates agent traffic using OpenAI's moderation endpoint.
This middleware checks messages for content policy violations at different stages:
Configuration options for the middleware
Options for configuring the OpenAI Moderation middleware.
OptionalcheckInput?: booleanWhether to check user input messages.
OptionalcheckOutput?: booleanWhether to check model output messages.
OptionalcheckToolResults?: booleanWhether to check tool result messages.
OptionalexitBehavior?: "end" | "error" | "replace"How to handle violations.
"error": Throw an error when content is flagged"end": End the agent execution and return a violation message"replace": Replace the flagged content with a violation messageOpenAI model to use for moderation. Can be either a model name or a BaseChatModel instance.
OptionalmoderationModel?: ModerationModelModeration model to use.
OptionalviolationMessage?: stringCustom template for violation messages.
Available placeholders: {categories}, {category_scores}, {original_content}
Middleware function that can be used to moderate agent traffic.
import { createAgent, openAIModerationMiddleware } from "langchain";
const middleware = openAIModerationMiddleware({
checkInput: true,
checkOutput: true,
exitBehavior: "end"
});
const agent = createAgent({
model: "openai:gpt-4o",
tools: [...],
middleware: [middleware],
});
import { createAgent, openAIModerationMiddleware } from "langchain";
const middleware = openAIModerationMiddleware({
model: "gpt-4o-mini",
checkInput: true,
checkOutput: true,
exitBehavior: "end"
});
const agent = createAgent({
model: "openai:gpt-4o",
tools: [...],
middleware: [middleware],
});
Provider specific middleware