|
| 1 | +--- |
| 2 | +title: Use Microsoft Purview SDK with Agent Framework |
| 3 | +description: Learn how to integrate Microsoft Purview SDK for data security and governance in your Agent Framework project |
| 4 | +zone_pivot_groups: programming-languages |
| 5 | +author: reezaali149 |
| 6 | +ms.topic: conceptual |
| 7 | +ms.author: v-reezaali |
| 8 | +ms.date: 10/28/2025 |
| 9 | +ms.service: purview |
| 10 | +--- |
| 11 | + |
| 12 | +# Use Microsoft Purview SDK with Agent Framework |
| 13 | + |
| 14 | +Microsoft Purview provides enterprise-grade data security, compliance, and governance capabilities for AI applications. By integrating Purview APIs within the Agent Framework SDK, developers can build intelligent agents that are secure by design, while ensuring sensitive data in prompts and responses are protected and compliant with organizational policies. |
| 15 | + |
| 16 | +## Why integrate Purview with Agent Framework? |
| 17 | + |
| 18 | +- **Prevent sensitive data leaks**: Inline blocking of sensitive content based on Data Loss Prevention (DLP) policies. |
| 19 | +- **Enable governance**: Log AI interactions in Purview for Audit, Communication Compliance, Insider Risk Management, eDiscovery, and Data Lifecycle Management. |
| 20 | +- **Accelerate adoption**: Enterprise customers require compliance for AI apps. Purview integration unblocks deployment. |
| 21 | + |
| 22 | +## Prerequisites |
| 23 | + |
| 24 | +Before you begin, ensure you have: |
| 25 | + |
| 26 | +- Microsoft Azure subscription with Microsoft Purview configured. |
| 27 | +- Microsoft 365 subscription with an E5 license and pay-as-you-go billing setup. |
| 28 | + - For testing, you can use a Microsoft 365 Developer Program tenant. For more information, see [Join the Microsoft 365 Developer Program](https://developer.microsoft.com/en-us/microsoft-365/dev-program). |
| 29 | +- Agent Framework SDK: To install the Agent Framework SDK: |
| 30 | + - Python: Run `pip install agent-framework`. |
| 31 | + - .NET: Install from NuGet. |
| 32 | + |
| 33 | +## How to integrate Microsoft Purview into your agent |
| 34 | + |
| 35 | +In your agent's workflow middleware pipeline, you can add Microsoft Purview policy middleware to intercept prompts and responses to determine if they meet the policies set up in Microsoft Purview. The Agent Framework SDK is capable of intercepting agent-to-agent or end-user chat client prompts and responses. |
| 36 | + |
| 37 | +The following code sample demonstrates how to add the Microsoft Purview policy middleware to your agent code. If you're new to Agent Framework, see [Create and run an agent with Agent Framework](/agent-framework/tutorials/agents/run-agent?pivots=programming-language-python). |
| 38 | + |
| 39 | +::: zone pivot="programming-language-csharp" |
| 40 | + |
| 41 | +```csharp |
| 42 | + |
| 43 | +using Azure.AI.OpenAI; |
| 44 | +using Azure.Core; |
| 45 | +using Azure.Identity; |
| 46 | +using Microsoft.Agents.AI; |
| 47 | +using Microsoft.Agents.AI.Purview; |
| 48 | +using Microsoft.Extensions.AI; |
| 49 | +using OpenAI; |
| 50 | + |
| 51 | +string endpoint = Environment.GetEnvironmentVariable("AZURE_OPENAI_ENDPOINT") ?? throw new InvalidOperationException("AZURE_OPENAI_ENDPOINT is not set."); |
| 52 | +string deploymentName = Environment.GetEnvironmentVariable("AZURE_OPENAI_DEPLOYMENT_NAME") ?? "gpt-4o-mini"; |
| 53 | +string purviewClientAppId = Environment.GetEnvironmentVariable("PURVIEW_CLIENT_APP_ID") ?? throw new InvalidOperationException("PURVIEW_CLIENT_APP_ID is not set."); |
| 54 | + |
| 55 | +TokenCredential browserCredential = new InteractiveBrowserCredential( |
| 56 | + new InteractiveBrowserCredentialOptions |
| 57 | + { |
| 58 | + ClientId = purviewClientAppId |
| 59 | + }); |
| 60 | + |
| 61 | +AIAgent agent = new AzureOpenAIClient( |
| 62 | + new Uri(endpoint), |
| 63 | + new AzureCliCredential()) |
| 64 | + .GetChatClient(deploymentName) |
| 65 | + .CreateAIAgent("You are a secure assistant.") |
| 66 | + .AsBuilder() |
| 67 | + .WithPurview(browserCredential, new PurviewSettings("My Secure Agent")) |
| 68 | + .Build(); |
| 69 | + |
| 70 | +AgentRunResponse response = await agent.RunAsync("Summarize zero trust in one sentence.").ConfigureAwait(false); |
| 71 | +Console.WriteLine(response); |
| 72 | + |
| 73 | +``` |
| 74 | + |
| 75 | +::: zone-end |
| 76 | +::: zone pivot="programming-language-python" |
| 77 | + |
| 78 | +```python |
| 79 | +import asyncio |
| 80 | +import os |
| 81 | +from agent_framework import ChatAgent, ChatMessage, Role |
| 82 | +from agent_framework.azure import AzureOpenAIChatClient |
| 83 | +from agent_framework.microsoft import PurviewPolicyMiddleware, PurviewSettings |
| 84 | +from azure.identity import AzureCliCredential, InteractiveBrowserCredential |
| 85 | + |
| 86 | +# Set default environment variables if not already set |
| 87 | +os.environ.setdefault("AZURE_OPENAI_ENDPOINT", "<azureOpenAIEndpoint>") |
| 88 | +os.environ.setdefault("AZURE_OPENAI_CHAT_DEPLOYMENT_NAME", "<azureOpenAIChatDeploymentName>") |
| 89 | + |
| 90 | +async def main(): |
| 91 | + chat_client = AzureOpenAIChatClient(credential=AzureCliCredential()) |
| 92 | + purview_middleware = PurviewPolicyMiddleware( |
| 93 | + credential=InteractiveBrowserCredential( |
| 94 | + client_id="<clientId>", |
| 95 | + ), |
| 96 | + settings=PurviewSettings(app_name="My Secure Agent") |
| 97 | + ) |
| 98 | + agent = ChatAgent( |
| 99 | + chat_client=chat_client, |
| 100 | + instructions="You are a secure assistant.", |
| 101 | + middleware=[purview_middleware] |
| 102 | + ) |
| 103 | + response = await agent.run(ChatMessage(role=Role.USER, text="Summarize zero trust in one sentence.")) |
| 104 | + print(response) |
| 105 | + |
| 106 | + if __name__ == "__main__": |
| 107 | + asyncio.run(main()) |
| 108 | +``` |
| 109 | + |
| 110 | +::: zone-end |
| 111 | + |
| 112 | +--- |
| 113 | + |
| 114 | +## Next steps |
| 115 | + |
| 116 | +Now that you added the above code to your agent, perform the following steps to test the integration of Microsoft Purview into your code: |
| 117 | + |
| 118 | +1. **Entra registration**: Register your agent and add the required Microsoft Graph permissions ([ProtectionScopes.Compute.All](/graph/api/userprotectionscopecontainer-compute), [ContentActivity.Write](/graph/api/activitiescontainer-post-contentactivities), [Content.Process.All](/graph/api/userdatasecurityandgovernance-processcontent)) to the Service Principal. For more information, see [Register an application in Microsoft Entra ID](/entra/identity-platform/quickstart-register-app) and [dataSecurityAndGovernance resource type](/graph/api/resources/datasecurityandgovernance). You'll need the Microsoft Entra app ID in the next step. |
| 119 | +1. **Purview policies**: Configure Purview policies using the Microsoft Entra app ID to enable agent communications data to flow into Purview. For more information, see [Configure Microsoft Purview](/purview/developer/configurepurview). |
| 120 | + |
| 121 | +## Resources |
| 122 | + |
| 123 | +::: zone pivot="programming-language-csharp" |
| 124 | + |
| 125 | +- Nuget: [Microsoft.Agents.AI.Purview](https://www.nuget.org/packages/Microsoft.Agents.AI.Purview/) |
| 126 | +- Github: [Microsoft.Agents.AI.Purview](https://github.com/microsoft/agent-framework/tree/main/dotnet/src/Microsoft.Agents.AI.Purview) |
| 127 | +- Sample: [AgentWithPurview](https://github.com/microsoft/agent-framework/tree/main/dotnet/samples/Purview/AgentWithPurview) |
| 128 | + |
| 129 | +::: zone-end |
| 130 | +::: zone pivot="programming-language-python" |
| 131 | + |
| 132 | +- [PyPI Package: Microsoft Agent Framework - Purview Integration (Python)](https://pypi.org/project/agent-framework-purview/). |
| 133 | +- [GitHub: Microsoft Agent Framework – Purview Integration (Python) source code](https://github.com/microsoft/agent-framework/tree/main/python/packages/purview). |
| 134 | +- [Code Sample: Purview Policy Enforcement Sample (Python)](https://github.com/microsoft/agent-framework/tree/main/python/samples/getting_started/purview_agent). |
| 135 | + |
| 136 | +::: zone-end |
0 commit comments