| Name | Type | Description |
|---|---|---|
endpoint | Optional[str] | Default: NoneAzure Content Safety resource endpoint URL. Falls back to
the |
credential | Optional[Any] | Default: NoneAzure credential. Accepts a
:class: |
project_endpoint | Optional[str] | Default: None |
categories | Optional[List[str]] | Default: None |
severity_threshold | int | Default: 4 |
exit_behavior | Literal['error', 'continue'] | Default: 'error' |
violation_message | Optional[str] | Default: None |
apply_to_input | bool | Default: True |
apply_to_output | bool | Default: False |
name | str | Default: 'azure_content_safety_image' |
context_extractor | Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[ImageModerationInput]]] | Default: None |
| Name | Type |
|---|---|
| endpoint | Optional[str] |
| credential | Optional[Any] |
| project_endpoint | Optional[str] |
| categories | Optional[List[str]] |
| severity_threshold | int |
| exit_behavior | Literal['error', 'continue'] |
| violation_message | Optional[str] |
| apply_to_input | bool |
| apply_to_output | bool |
| name | str |
| context_extractor | Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[ImageModerationInput]]] |
AgentMiddleware that screens image content with Azure AI Content Safety.
Use this middleware alongside :class:AzureContentModerationMiddleware when
your agent handles visual content. Because image analysis uses a separate
API endpoint (analyze_image) and different category enumerations, a
dedicated class keeps each concern focused and composable.
The middleware extracts images from the most recent HumanMessage (input)
and, optionally, from the most recent AIMessage (output). It supports:
data:image/png;base64,<data>Content is analyzed using the Azure AI Content Safety image analysis API. When violations are detected, the middleware takes one of two actions:
"error" – raises :exc:ContentSafetyViolationError, halting the graph."continue" – replaces the offending message with a violation notice
(either a service-derived description or a custom violation_message)
and lets execution proceed.Both synchronous (before_agent / after_agent) and asynchronous
(abefore_agent / aafter_agent) hooks are implemented.
Azure AI Foundry project endpoint URL (e.g.
https://<resource>.services.ai.azure.com/api/projects/<project>).
Falls back to the AZURE_AI_PROJECT_ENDPOINT environment variable.
Mutually exclusive with endpoint.
Image harm categories to analyse. Valid values are
"Hate", "SelfHarm", "Sexual", and "Violence".
Defaults to all four.
Minimum severity score (0–6) that triggers the
configured exit behaviour. Defaults to 4 (medium).
What to do when a violation is detected. One of
"error" (default) or "continue".
Custom text used to replace the offending message
when exit_behavior="continue". Defaults to a message built
from the service response.
Whether to screen images in the last HumanMessage.
Defaults to True.
Whether to screen images in the last AIMessage.
Defaults to False (agents rarely produce images directly).
Node-name prefix used when wiring this middleware into a
LangGraph. Defaults to "azure_content_safety_image".
Optional callable with signature
(state, runtime) -> Optional[ImageModerationInput]
that receives the current graph state and the LangGraph
:class:~langchain.agents.middleware.Runtime execution context,
and returns the images to screen, or None to skip evaluation
entirely. When None (default) the middleware uses its
built-in extraction logic.