Skip to main content

OpenAI middleware integration

还没翻译译自英文(docs.langchain.com)本页 26 段都还没译

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

MiddlewareDescription
Content moderationModerate agent traffic using OpenAI's moderation endpoint
Model routingRoute each agent run to a model using the OpenAI Decisions API (beta)
Tool-risk gatingBlock risky tool calls using the OpenAI Decisions API (beta)

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

from langchain_openai import ChatOpenAI
from langchain_openai.middleware import OpenAIModerationMiddleware
from langchain.agents import create_agent

agent = create_agent(
    model=ChatOpenAI(model="gpt-5.5"),
    tools=[search_tool, database_tool],
    middleware=[
        OpenAIModerationMiddleware(
            model="omni-moderation-latest",
            check_input=True,
            check_output=True,
            exit_behavior="end",
        ),
    ],
)