openai_moderation.py — langchain Source File
Architecture documentation for openai_moderation.py, a python file in the langchain codebase. 8 imports, 0 dependents.
Entity Profile
Dependency Diagram
graph LR e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b["openai_moderation.py"] 7025b240_fdc3_cf68_b72f_f41dac94566b["json"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> 7025b240_fdc3_cf68_b72f_f41dac94566b cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7["collections.abc"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> cfe2bde5_180e_e3b0_df2b_55b3ebaca8e7 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3["typing"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> 8e2034b7_ceb8_963f_29fc_2ea6b50ef9b3 50acc543_e5f0_2162_cf07_c2bf50723e0c["langchain.agents.middleware.types"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> 50acc543_e5f0_2162_cf07_c2bf50723e0c d758344f_537f_649e_f467_b9d7442e86df["langchain_core.messages"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> d758344f_537f_649e_f467_b9d7442e86df 45fc8fd3_e815_b442_a6d1_0dedc9327b62["openai"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> 45fc8fd3_e815_b442_a6d1_0dedc9327b62 a61e7e70_a284_5bb8_84cf_97f8cd40e89f["openai.types"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> a61e7e70_a284_5bb8_84cf_97f8cd40e89f 5dbfd558_f2f2_9663_3a3f_c317926ac1c1["langgraph.runtime"] e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b --> 5dbfd558_f2f2_9663_3a3f_c317926ac1c1 style e98c0351_7e9b_e4a2_88d2_d7ba4aa4bc4b fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
"""Agent middleware that integrates OpenAI's moderation endpoint."""
from __future__ import annotations
import json
from collections.abc import Sequence
from typing import TYPE_CHECKING, Any, Literal, cast
from langchain.agents.middleware.types import AgentMiddleware, AgentState, hook_config
from langchain_core.messages import AIMessage, BaseMessage, HumanMessage, ToolMessage
from openai import AsyncOpenAI, OpenAI
from openai.types import Moderation, ModerationModel
if TYPE_CHECKING: # pragma: no cover
from langgraph.runtime import Runtime
ViolationStage = Literal["input", "output", "tool"]
DEFAULT_VIOLATION_TEMPLATE = (
"I'm sorry, but I can't comply with that request. It was flagged for {categories}."
)
class OpenAIModerationError(RuntimeError):
"""Raised when OpenAI flags content and `exit_behavior` is set to ``"error"``."""
def __init__(
self,
*,
content: str,
stage: ViolationStage,
result: Moderation,
message: str,
) -> None:
"""Initialize the error with violation details.
Args:
content: The content that was flagged.
stage: The stage where the violation occurred.
result: The moderation result from OpenAI.
message: The error message.
"""
super().__init__(message)
self.content = content
self.stage = stage
self.result = result
class OpenAIModerationMiddleware(AgentMiddleware[AgentState[Any], Any]):
"""Moderate agent traffic using OpenAI's moderation endpoint."""
def __init__(
self,
*,
model: ModerationModel = "omni-moderation-latest",
check_input: bool = True,
check_output: bool = True,
check_tool_results: bool = False,
exit_behavior: Literal["error", "end", "replace"] = "end",
violation_message: str | None = None,
// ... (425 more lines)
Domain
Subdomains
Functions
Dependencies
- collections.abc
- json
- langchain.agents.middleware.types
- langchain_core.messages
- langgraph.runtime
- openai
- openai.types
- typing
Source
Frequently Asked Questions
What does openai_moderation.py do?
openai_moderation.py is a source file in the langchain codebase, written in python. It belongs to the CoreAbstractions domain, RunnableInterface subdomain.
What functions are defined in openai_moderation.py?
openai_moderation.py defines 1 function(s): langgraph.
What does openai_moderation.py depend on?
openai_moderation.py imports 8 module(s): collections.abc, json, langchain.agents.middleware.types, langchain_core.messages, langgraph.runtime, openai, openai.types, typing.
Where is openai_moderation.py in the architecture?
openai_moderation.py is located at libs/partners/openai/langchain_openai/middleware/openai_moderation.py (domain: CoreAbstractions, subdomain: RunnableInterface, directory: libs/partners/openai/langchain_openai/middleware).
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free