# AzureProtectedMaterialMiddleware

> **Class** in `langchain_azure_ai`

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

AgentMiddleware that detects protected material using Azure AI Content Safety.

Protected material detection checks whether text contains copyright-protected
content such as song lyrics, news articles, book passages, or other protected
intellectual property.  Use this middleware to prevent agents from reproducing
or accepting copyrighted content.

When protected material is 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
AzureProtectedMaterialMiddleware(
    self,
    endpoint: Optional[str] = None,
    credential: Optional[Any] = None,
    *,
    project_endpoint: Optional[str] = None,
    type: Literal['code', 'text'] = 'text',
    exit_behavior: Literal['error', 'continue', 'replace'] = 'error',
    violation_message: Optional[str] = None,
    apply_to_input: bool = True,
    apply_to_output: bool = True,
    name: str = 'azure_protected_material',
    context_extractor: Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[TextModerationInput]]] = 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`) |
| `exit_behavior` | `Literal['error', 'continue', 'replace']` | No | What to do when protected material 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 the agent's input (last ``HumanMessage``).  Defaults to ``True``. (default: `True`) |
| `apply_to_output` | `bool` | No | Whether to screen the agent's output (last ``AIMessage``).  Defaults to ``True``. (default: `True`) |
| `name` | `str` | No | Node-name prefix used when wiring this middleware into a LangGraph.  Defaults to ``"azure_protected_material"``. (default: `'azure_protected_material'`) |
| `context_extractor` | `Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[TextModerationInput]]]` | No | Optional callable with signature ``(state, runtime) -> Optional[TextModerationInput]`` that receives the current graph state and the LangGraph :class:`~langchain.agents.middleware.Runtime` execution context, and returns the text 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,
    type: Literal['code', 'text'] = 'text',
    exit_behavior: Literal['error', 'continue', 'replace'] = 'error',
    violation_message: Optional[str] = None,
    apply_to_input: bool = True,
    apply_to_output: bool = True,
    name: str = 'azure_protected_material',
    context_extractor: Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[TextModerationInput]]] = None,
) -> None
```

| Name | Type |
|------|------|
| `endpoint` | `Optional[str]` |
| `credential` | `Optional[Any]` |
| `project_endpoint` | `Optional[str]` |
| `type` | `Literal['code', 'text']` |
| `exit_behavior` | `Literal['error', 'continue', 'replace']` |
| `violation_message` | `Optional[str]` |
| `apply_to_input` | `bool` |
| `apply_to_output` | `bool` |
| `name` | `str` |
| `context_extractor` | `Optional[Callable[[AgentState[Any], Runtime[Any]], Optional[TextModerationInput]]]` |


## Properties

- `type`

## Methods

- [`get_annotation_from_evaluations()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_protected_material/AzureProtectedMaterialMiddleware/get_annotation_from_evaluations)
- [`get_evaluation_response()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_protected_material/AzureProtectedMaterialMiddleware/get_evaluation_response)
- [`before_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_protected_material/AzureProtectedMaterialMiddleware/before_agent)
- [`after_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_protected_material/AzureProtectedMaterialMiddleware/after_agent)
- [`abefore_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_protected_material/AzureProtectedMaterialMiddleware/abefore_agent)
- [`aafter_agent()`](https://reference.langchain.com/python/langchain-azure-ai/agents/middleware/content_safety/_protected_material/AzureProtectedMaterialMiddleware/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/_protected_material.py#L36)