> ## Documentation Index
> Fetch the complete documentation index at: https://nvd-54.mintlify.site/llms.txt
> Use this file to discover all available pages before exploring further.

# OpenAI 中间件集成

> 使用 LangChain Python 集成 OpenAI 中间件。

专为 OpenAI 模型设计的中间件。了解更多关于[中间件](/oss/python/langchain/middleware/overview)的信息。

| 中间件                                       | 描述                     |
| ----------------------------------------- | ---------------------- |
| [Content moderation](#content-moderation) | 使用 OpenAI 的审核端点审核智能体流量 |

## 内容审核

使用 OpenAI 的审核端点审核智能体流量（用户输入、模型输出和工具结果），以检测和处理不安全内容。 内容审核适用于以下场景：

* 需要内容安全和合规的应用
* 过滤有害、仇恨或不当内容
* 需要安全护栏的面向客户的智能体
* 满足平台审核要求

<Info>
  了解更多关于 [OpenAI's moderation models](https://platform.openai.com/docs/guides/moderation) and categories.
</Info>

**API reference:** [`OpenAIModerationMiddleware`](https://reference.langchain.com/python/langchain-openai/middleware/openai_moderation/OpenAIModerationMiddleware)

```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
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.4"),
    tools=[search_tool, database_tool],
    middleware=[
        OpenAIModerationMiddleware(
            model="omni-moderation-latest",
            check_input=True,
            check_output=True,
            exit_behavior="end",
        ),
    ],
)
```

<Accordion title="配置选项">
  <ParamField body="model" type="ModerationModel" default="omni-moderation-latest">
    OpenAI moderation model to use. Options: `'omni-moderation-latest'`, `'omni-moderation-2024-09-26'`, `'text-moderation-latest'`, `'text-moderation-stable'`
  </ParamField>

  <ParamField body="check_input" type="bool" default="True">
    Whether to check user input messages before the model is called
  </ParamField>

  <ParamField body="check_output" type="bool" default="True">
    Whether to check model output messages after the model is called
  </ParamField>

  <ParamField body="check_tool_results" type="bool" default="False">
    Whether to check tool result messages before the model is called
  </ParamField>

  <ParamField body="exit_behavior" type="string" default="end">
    How to handle violations when content is flagged. Options:

    * `'end'` - End agent execution immediately with a violation message
    * `'error'` - Raise `OpenAIModerationError` exception
    * `'replace'` - Replace the flagged content with the violation message and continue
  </ParamField>

  <ParamField body="violation_message" type="str | None">
    Custom template for violation messages. Supports template variables:

    * `{categories}` - Comma-separated list of flagged categories
    * `{category_scores}` - JSON string of category scores
    * `{original_content}` - The original flagged content

    Default: `"I'm sorry, but I can't comply with that request. It was flagged for {categories}."`
  </ParamField>

  <ParamField body="client" type="OpenAI | None">
    Optional pre-configured OpenAI client to reuse. If not provided, a new client will be created.
  </ParamField>

  <ParamField body="async_client" type="AsyncOpenAI | None">
    Optional pre-configured AsyncOpenAI client to reuse. If not provided, a new async client will be created.
  </ParamField>
</Accordion>

<Accordion title="完整示例">
  The middleware integrates OpenAI's moderation endpoint to check content at different stages:

  **Moderation stages:**

  * `check_input` - User messages before model call
  * `check_output` - AI messages after model call
  * `check_tool_results` - Tool outputs before model call

  **Exit behaviors:**

  * `'end'` (default) - Stop execution with violation message
  * `'error'` - Raise exception for application handling
  * `'replace'` - Replace flagged content and continue

  ```python theme={"theme":{"light":"catppuccin-latte","dark":"catppuccin-mocha"}}
  from langchain_openai import ChatOpenAI
  from langchain_openai.middleware import OpenAIModerationMiddleware
  from langchain.agents import create_agent


  # Basic moderation
  agent = create_agent(
      model=ChatOpenAI(model="gpt-5.4"),
      tools=[search_tool, customer_data_tool],
      middleware=[
          OpenAIModerationMiddleware(
              model="omni-moderation-latest",
              check_input=True,
              check_output=True,
          ),
      ],
  )

  # Strict moderation with custom message
  agent_strict = create_agent(
      model=ChatOpenAI(model="gpt-5.4"),
      tools=[search_tool, customer_data_tool],
      middleware=[
          OpenAIModerationMiddleware(
              model="omni-moderation-latest",
              check_input=True,
              check_output=True,
              check_tool_results=True,
              exit_behavior="error",
              violation_message=(
                  "Content policy violation detected: {categories}. "
                  "Please rephrase your request."
              ),
          ),
      ],
  )

  # Moderation with replacement behavior
  agent_replace = create_agent(
      model=ChatOpenAI(model="gpt-5.4"),
      tools=[search_tool],
      middleware=[
          OpenAIModerationMiddleware(
              check_input=True,
              exit_behavior="replace",
              violation_message="[Content removed due to safety policies]",
          ),
      ],
  )
  ```
</Accordion>

***

<div className="source-links">
  <Callout icon="terminal-2">
    [Connect these docs](/use-these-docs) to Claude, VSCode, and more via MCP for real-time answers.
  </Callout>

  <Callout icon="edit">
    [Edit this page on GitHub](https://github.com/langchain-ai/docs/edit/main/src/oss/python/integrations/middleware/openai.mdx) or [file an issue](https://github.com/langchain-ai/docs/issues/new/choose).
  </Callout>
</div>
