_format_violation_message() — langchain Function Reference
Architecture documentation for the _format_violation_message() function in openai_moderation.py from the langchain codebase.
Entity Profile
Dependency Diagram
graph TD 033a3f7e_bdfd_a614_a8dc_e4e3a07e50fd["_format_violation_message()"] 48713c67_3a9a_ec9e_ec74_46e6955f07bd["OpenAIModerationMiddleware"] 033a3f7e_bdfd_a614_a8dc_e4e3a07e50fd -->|defined in| 48713c67_3a9a_ec9e_ec74_46e6955f07bd 59081f65_8455_8937_22b3_f7febac7b501["_apply_violation()"] 59081f65_8455_8937_22b3_f7febac7b501 -->|calls| 033a3f7e_bdfd_a614_a8dc_e4e3a07e50fd style 033a3f7e_bdfd_a614_a8dc_e4e3a07e50fd fill:#6366f1,stroke:#818cf8,color:#fff
Relationship Graph
Source Code
libs/partners/openai/langchain_openai/middleware/openai_moderation.py lines 440–461
def _format_violation_message(self, content: str, result: Moderation) -> str:
# Convert categories to dict and filter for flagged items
categories_dict = result.categories.model_dump()
categories = [
name.replace("_", " ")
for name, flagged in categories_dict.items()
if flagged
]
category_label = (
", ".join(categories) if categories else "OpenAI's safety policies"
)
template = self.violation_message or DEFAULT_VIOLATION_TEMPLATE
scores_json = json.dumps(result.category_scores.model_dump(), sort_keys=True)
try:
message = template.format(
categories=category_label,
category_scores=scores_json,
original_content=content,
)
except KeyError:
message = template
return message
Domain
Subdomains
Called By
Source
Frequently Asked Questions
What does _format_violation_message() do?
_format_violation_message() is a function in the langchain codebase, defined in libs/partners/openai/langchain_openai/middleware/openai_moderation.py.
Where is _format_violation_message() defined?
_format_violation_message() is defined in libs/partners/openai/langchain_openai/middleware/openai_moderation.py at line 440.
What calls _format_violation_message()?
_format_violation_message() is called by 1 function(s): _apply_violation.
Analyze Your Own Codebase
Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.
Try Supermodel Free