_AzureContentSafetyBaseMiddleware| Name | Type | Description |
|---|---|---|
policy_id* | str | ID of a guardrail policy configured on the Content Safety resource. Must be nonempty. |
endpoint | Optional[str] | Default: NoneContent Safety resource URL; falls back to
|
credential | Optional[Any] | Default: None |
project_endpoint | Optional[str] | Default: None |
context | Optional[UnifiedModerateContext] | Default: None |
name | str | Default: 'azure_unified_moderation' |
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.
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", ) ], )
Token credential, Azure key credential, or key string.
Defaults to DefaultAzureCredential.
Foundry project endpoint; falls back to
AZURE_AI_PROJECT_ENDPOINT when no resource URL is available.
Optional SDK context (agent/session/correlation metadata) included with each moderation request.
Middleware node-name prefix.