Middleware for Azure AI LangChain/LangGraph agent integrations.
This module provides middleware classes for powered by Microsoft
Foundry. Pass them via the middleware parameter of any
LangChain create_agent factory:
.. code-block:: python
from langchain.agents import create_agent
from langchain_azure_ai.agents.middleware import (
AzureContentModerationMiddleware,
AzureContentModerationForImagesMiddleware,
AzureGroundednessMiddleware,
AzureProtectedMaterialMiddleware,
AzurePromptShieldMiddleware,
)
agent = create_agent(
model="azure_ai:gpt-4.1",
middleware=[
# Block harmful text in both input and output
AzureContentModerationMiddleware(
exit_behavior="error",
),
# Block harmful images in user input
AzureContentModerationForImagesMiddleware(
exit_behavior="error",
),
# Block protected/copyrighted content in AI output
AzureProtectedMaterialMiddleware(
exit_behavior="continue",
apply_to_input=False,
apply_to_output=True,
),
# Block prompt injection attacks in user input and tool outputs
AzurePromptShieldMiddleware(
exit_behavior="error",
),
],
)