# AzureContentSafetyPolicyMiddleware

> **Class** in `langchain_azure_ai`

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

Enforce a Content Safety guardrail on a LangChain ``create_agent`` agent.

Calls the Azure Content Safety Unified Moderate preview operation at the
agent and model input/output hooks and around tool calls. All six hooks
are submitted to the service, which controls the policy at each
intervention point. A message may be evaluated at both the agent and
model boundaries. Text-only message content is screened at these
boundaries, while tool arguments and results are submitted as
JSON-encoded content.

A ``blocked`` service verdict raises :class:`ContentSafetyViolationError`
before the content can reach the next stage. An ``allowed`` verdict applies
returned content (including policy transforms). Non-blocking findings and
transforms are attached as content-safety annotations to the corresponding
message. Requests and malformed service responses fail explicitly.

## Signature

```python
AzureContentSafetyPolicyMiddleware(
    self,
    policy_id: str,
    endpoint: Optional[str] = None,
    credential: Optional[Any] = None,
    *,
    project_endpoint: Optional[str] = None,
    context: Optional[UnifiedModerateContext] = None,
    name: str = 'azure_unified_moderation',
)
```

## Description

**Example:**

.. code-block:: python

from langchain.agents import create_agent
from langchain_azure_ai.agents.middleware import (
    AzureContentSafetyPolicyMiddleware,
)

agent = create_agent(
    model=my_model,
    tools=my_tools,
    middleware=[
        AzureContentSafetyPolicyMiddleware(
            policy_id="my-guardrail",
            endpoint="https://my-resource.cognitiveservices.azure.com",
        )
    ],
)

## Parameters

| Name | Type | Required | Description |
|------|------|----------|-------------|
| `policy_id` | `str` | Yes | ID of a guardrail policy configured on the Content Safety resource. Must be nonempty. |
| `endpoint` | `Optional[str]` | No | Content Safety resource URL; falls back to ``AZURE_CONTENT_SAFETY_ENDPOINT``. Mutually exclusive with ``project_endpoint``. (default: `None`) |
| `credential` | `Optional[Any]` | No | Token credential, Azure key credential, or key string. Defaults to ``DefaultAzureCredential``. (default: `None`) |
| `project_endpoint` | `Optional[str]` | No | Foundry project endpoint; falls back to ``AZURE_AI_PROJECT_ENDPOINT`` when no resource URL is available. (default: `None`) |
| `context` | `Optional[UnifiedModerateContext]` | No | Optional SDK context (agent/session/correlation metadata) included with each moderation request. (default: `None`) |
| `name` | `str` | No | Middleware node-name prefix. (default: `'azure_unified_moderation'`) |

## Extends

- `_AzureContentSafetyBaseMiddleware`

## Constructors

```python
__init__(
    self,
    policy_id: str,
    endpoint: Optional[str] = None,
    credential: Optional[Any] = None,
    *,
    project_endpoint: Optional[str] = None,
    context: Optional[UnifiedModerateContext] = None,
    name: str = 'azure_unified_moderation',
) -> None
```

| Name | Type |
|------|------|
| `policy_id` | `str` |
| `endpoint` | `Optional[str]` |
| `credential` | `Optional[Any]` |
| `project_endpoint` | `Optional[str]` |
| `context` | `Optional[UnifiedModerateContext]` |
| `name` | `str` |


## Properties

- `policy_id`
- `context`

## Methods

- [`get_annotation_from_evaluations()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/get_annotation_from_evaluations)
- [`get_evaluation_response()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/get_evaluation_response)
- [`before_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/before_agent)
- [`abefore_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/abefore_agent)
- [`after_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/after_agent)
- [`aafter_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/aafter_agent)
- [`before_model()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/before_model)
- [`abefore_model()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/abefore_model)
- [`after_model()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/after_model)
- [`aafter_model()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/aafter_model)
- [`wrap_tool_call()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/wrap_tool_call)
- [`awrap_tool_call()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_unified_moderation/AzureContentSafetyPolicyMiddleware/awrap_tool_call)

---

[View source on GitHub](https://github.com/langchain-ai/langchain-azure/blob/b3723d62c412c24d2d57566023c05c545b652677/libs/azure-ai/langchain_azure_ai/agents/middleware/content_safety/_unified_moderation.py#L52)