# AzureContentModerationForImagesMiddleware

> **Class** in `langchain_azure_ai`

📖 [View in docs](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware)

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:

* **Base64 data URLs** – ``data:image/png;base64,<data>``
* **HTTP(S) URLs** – publicly accessible image URLs

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.

## Signature

```python
AzureContentModerationForImagesMiddleware(
    self,
    endpoint: Optional[str] = None,
    credential: Optional[Any] = None,
    *,
    project_endpoint: Optional[str] = None,
    categories: Optional[List[str]] = None,
    severity_threshold: int = 4,
    exit_behavior: Literal['error', 'continue'] = 'error',
    violation_message: Optional[str] = None,
    apply_to_input: bool = True,
    apply_to_output: bool = False,
    name: str = 'azure_content_safety_image',
    context_extractor: Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[ImageModerationInput]]] = None,
)
```

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `endpoint` | `Optional[str]` | No | Azure Content Safety resource endpoint URL.  Falls back to the ``AZURE_CONTENT_SAFETY_ENDPOINT`` environment variable. Mutually exclusive with ``project_endpoint``. (default: `None`) |
| `credential` | `Optional[Any]` | No | Azure credential.  Accepts a :class:`~azure.core.credentials.TokenCredential`, :class:`~azure.core.credentials.AzureKeyCredential`, or a plain API-key string.  Defaults to :class:`~azure.identity.DefaultAzureCredential` when ``None``. (default: `None`) |
| `project_endpoint` | `Optional[str]` | No | 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``. (default: `None`) |
| `categories` | `Optional[List[str]]` | No | Image harm categories to analyse.  Valid values are ``"Hate"``, ``"SelfHarm"``, ``"Sexual"``, and ``"Violence"``. Defaults to all four. (default: `None`) |
| `severity_threshold` | `int` | No | Minimum severity score (0–6) that triggers the configured exit behaviour.  Defaults to ``4`` (medium). (default: `4`) |
| `exit_behavior` | `Literal['error', 'continue']` | No | What to do when a violation is detected.  One of ``"error"`` (default) or ``"continue"``. (default: `'error'`) |
| `violation_message` | `Optional[str]` | No | Custom text used to replace the offending message when ``exit_behavior="continue"``.  Defaults to a message built from the service response. (default: `None`) |
| `apply_to_input` | `bool` | No | Whether to screen images in the last ``HumanMessage``. Defaults to ``True``. (default: `True`) |
| `apply_to_output` | `bool` | No | Whether to screen images in the last ``AIMessage``. Defaults to ``False`` (agents rarely produce images directly). (default: `False`) |
| `name` | `str` | No | Node-name prefix used when wiring this middleware into a LangGraph.  Defaults to ``"azure_content_safety_image"``. (default: `'azure_content_safety_image'`) |
| `context_extractor` | `Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[ImageModerationInput]]]` | No | 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. (default: `None`) |

## Extends

- `_AzureContentSafetyBaseMiddleware`

## Constructors

```python
__init__(
    self,
    endpoint: Optional[str] = None,
    credential: Optional[Any] = None,
    *,
    project_endpoint: Optional[str] = None,
    categories: Optional[List[str]] = None,
    severity_threshold: int = 4,
    exit_behavior: Literal['error', 'continue'] = 'error',
    violation_message: Optional[str] = None,
    apply_to_input: bool = True,
    apply_to_output: bool = False,
    name: str = 'azure_content_safety_image',
    context_extractor: Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[ImageModerationInput]]] = None,
) -> 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]]]` |


## Methods

- [`get_annotation_from_evaluations()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware/get_annotation_from_evaluations)
- [`get_evaluation_response()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware/get_evaluation_response)
- [`before_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware/before_agent)
- [`after_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware/after_agent)
- [`abefore_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware/abefore_agent)
- [`aafter_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_image/AzureContentModerationForImagesMiddleware/aafter_agent)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-azure/blob/7a6a664d617264df8dcb12fcfb1bacc09f21d504/libs/azure-ai/langchain_azure_ai/agents/middleware/content_safety/_image.py#L42)