agent-framework-azure-ai-py
par microsoft
Construisez des agents Azure AI Foundry à l’aide du SDK Python Microsoft Agent Framework (agent-framework-azure-ai). À utiliser lors de la création d’agents persistants avec…
npx skills add https://github.com/microsoft/skills --skill agent-framework-azure-ai-pyAgent Framework Azure Hosted Agents
Build persistent agents on Azure AI Foundry using the Microsoft Agent Framework Python SDK.
Architecture
User Query → AzureAIAgentsProvider → Azure AI Agent Service (Persistent)
↓
Agent.run() / Agent.run_stream()
↓
Tools: Functions | Hosted (Code/Search/Web) | MCP
↓
AgentThread (conversation persistence)
Installation
# Full framework (recommended)
pip install agent-framework --pre
# Or Azure-specific package only
pip install agent-framework-azure-ai --pre
Environment Variables
export AZURE_AI_PROJECT_ENDPOINT="https://<project>.services.ai.azure.com/api/projects/<project-id>" # Required for all auth methods
export AZURE_AI_MODEL_DEPLOYMENT_NAME="gpt-4o-mini" # Required for all auth methods
export BING_CONNECTION_ID="your-bing-connection-id" # For web search
export AZURE_TOKEN_CREDENTIALS=prod # Required only if DefaultAzureCredential is used in production
Authentication & Lifecycle
🔑 Two rules apply to every code sample below:
- Prefer
DefaultAzureCredential. It works locally (Azure CLI / VS Code / Developer CLI) and in Azure (managed identity, workload identity) with no code change. Avoid connection strings, account/API keys — they bypass Entra audit and rotation.
- Local dev:
DefaultAzureCredentialworks as-is.- Production: set
AZURE_TOKEN_CREDENTIALS=prod(orAZURE_TOKEN_CREDENTIALS=<specific_credential>) to constrain the credential chain to production-safe credentials.- Wrap every client in a context manager so HTTP transports, sockets, and token caches are released deterministically:
- Sync:
with <Client>(...) as client:- Async:
async with <Client>(...) as client:andasync with DefaultAzureCredential() as credential:(fromazure.identity.aio)Snippets may abbreviate this setup, but production code should always follow both rules.
from azure.identity.aio import AzureCliCredential, DefaultAzureCredential, ManagedIdentityCredential
# Development
credential = AzureCliCredential()
# Production
# Local dev: DefaultAzureCredential. Production: set AZURE_TOKEN_CREDENTIALS=prod or AZURE_TOKEN_CREDENTIALS=<specific_credential>
credential = DefaultAzureCredential(require_envvar=True)
# Or use a specific credential directly in production:
# See https://learn.microsoft.com/python/api/overview/azure/identity-readme?view=azure-python#credential-classes
# credential = ManagedIdentityCredential()
Core Workflow
Basic Agent
import asyncio
from agent_framework.azure import AzureAIAgentsProvider
from azure.identity.aio import AzureCliCredential
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="MyAgent",
instructions="You are a helpful assistant.",
)
result = await agent.run("Hello!")
print(result.text)
asyncio.run(main())
Agent with Function Tools
from typing import Annotated
from pydantic import Field
from agent_framework.azure import AzureAIAgentsProvider
from azure.identity.aio import AzureCliCredential
def get_weather(
location: Annotated[str, Field(description="City name to get weather for")],
) -> str:
"""Get the current weather for a location."""
return f"Weather in {location}: 72°F, sunny"
def get_current_time() -> str:
"""Get the current UTC time."""
from datetime import datetime, timezone
return datetime.now(timezone.utc).strftime("%Y-%m-%d %H:%M:%S UTC")
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="WeatherAgent",
instructions="You help with weather and time queries.",
tools=[get_weather, get_current_time], # Pass functions directly
)
result = await agent.run("What's the weather in Seattle?")
print(result.text)
Agent with Hosted Tools
from agent_framework import (
HostedCodeInterpreterTool,
HostedFileSearchTool,
HostedWebSearchTool,
)
from agent_framework.azure import AzureAIAgentsProvider
from azure.identity.aio import AzureCliCredential
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="MultiToolAgent",
instructions="You can execute code, search files, and search the web.",
tools=[
HostedCodeInterpreterTool(),
HostedWebSearchTool(name="Bing"),
],
)
result = await agent.run("Calculate the factorial of 20 in Python")
print(result.text)
Streaming Responses
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="StreamingAgent",
instructions="You are a helpful assistant.",
)
print("Agent: ", end="", flush=True)
async for chunk in agent.run_stream("Tell me a short story"):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
Conversation Threads
from agent_framework.azure import AzureAIAgentsProvider
from azure.identity.aio import AzureCliCredential
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="ChatAgent",
instructions="You are a helpful assistant.",
tools=[get_weather],
)
# Create thread for conversation persistence
thread = agent.get_new_thread()
# First turn
result1 = await agent.run("What's the weather in Seattle?", thread=thread)
print(f"Agent: {result1.text}")
# Second turn - context is maintained
result2 = await agent.run("What about Portland?", thread=thread)
print(f"Agent: {result2.text}")
# Save thread ID for later resumption
print(f"Conversation ID: {thread.conversation_id}")
Structured Outputs
from pydantic import BaseModel, ConfigDict
from agent_framework.azure import AzureAIAgentsProvider
from azure.identity.aio import AzureCliCredential
class WeatherResponse(BaseModel):
model_config = ConfigDict(extra="forbid")
location: str
temperature: float
unit: str
conditions: str
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="StructuredAgent",
instructions="Provide weather information in structured format.",
response_format=WeatherResponse,
)
result = await agent.run("Weather in Seattle?")
weather = WeatherResponse.model_validate_json(result.text)
print(f"{weather.location}: {weather.temperature}°{weather.unit}")
Provider Methods
| Method | Description |
|---|---|
create_agent() | Create new agent on Azure AI service |
get_agent(agent_id) | Retrieve existing agent by ID |
as_agent(sdk_agent) | Wrap SDK Agent object (no HTTP call) |
Hosted Tools Quick Reference
| Tool | Import | Purpose |
|---|---|---|
HostedCodeInterpreterTool | from agent_framework import HostedCodeInterpreterTool | Execute Python code |
HostedFileSearchTool | from agent_framework import HostedFileSearchTool | Search vector stores |
HostedWebSearchTool | from agent_framework import HostedWebSearchTool | Bing web search |
HostedMCPTool | from agent_framework import HostedMCPTool | Service-managed MCP |
MCPStreamableHTTPTool | from agent_framework import MCPStreamableHTTPTool | Client-managed MCP |
Complete Example
import asyncio
from typing import Annotated
from pydantic import BaseModel, Field
from agent_framework import (
HostedCodeInterpreterTool,
HostedWebSearchTool,
MCPStreamableHTTPTool,
)
from agent_framework.azure import AzureAIAgentsProvider
from azure.identity.aio import AzureCliCredential
def get_weather(
location: Annotated[str, Field(description="City name")],
) -> str:
"""Get weather for a location."""
return f"Weather in {location}: 72°F, sunny"
class AnalysisResult(BaseModel):
summary: str
key_findings: list[str]
confidence: float
async def main():
async with (
AzureCliCredential() as credential,
MCPStreamableHTTPTool(
name="Docs MCP",
url="https://learn.microsoft.com/api/mcp",
) as mcp_tool,
AzureAIAgentsProvider(credential=credential) as provider,
):
agent = await provider.create_agent(
name="ResearchAssistant",
instructions="You are a research assistant with multiple capabilities.",
tools=[
get_weather,
HostedCodeInterpreterTool(),
HostedWebSearchTool(name="Bing"),
mcp_tool,
],
)
thread = agent.get_new_thread()
# Non-streaming
result = await agent.run(
"Search for Python best practices and summarize",
thread=thread,
)
print(f"Response: {result.text}")
# Streaming
print("\nStreaming: ", end="")
async for chunk in agent.run_stream("Continue with examples", thread=thread):
if chunk.text:
print(chunk.text, end="", flush=True)
print()
# Structured output
result = await agent.run(
"Analyze findings",
thread=thread,
response_format=AnalysisResult,
)
analysis = AnalysisResult.model_validate_json(result.text)
print(f"\nConfidence: {analysis.confidence}")
if __name__ == "__main__":
asyncio.run(main())
Conventions
- Always use async context managers:
async with provider: - Pass functions directly to
tools=parameter (auto-converted to AIFunction) - Use
Annotated[type, Field(description=...)]for function parameters - Use
get_new_thread()for multi-turn conversations - Prefer
HostedMCPToolfor service-managed MCP,MCPStreamableHTTPToolfor client-managed
Best Practices
- This SDK is async-first — use
async defhandlers andasync withthroughout. - Always use context managers for clients and async credentials. Wrap every client in
with Client(...) as client:(sync) orasync with Client(...) as client:(async). For asyncDefaultAzureCredentialfromazure.identity.aio, also useasync with credential:so tokens and transports are cleaned up.
Reference Files
- references/tools.md: Detailed hosted tool patterns
- references/mcp.md: MCP integration (hosted + local)
- references/threads.md: Thread and conversation management
- references/advanced.md: OpenAPI, citations, structured outputs
Plus de skills de microsoft
oss-growth
microsoft
Persona de growth hacker OSS
official
microsoft-foundry
microsoft
Déployer, évaluer et gérer les agents Foundry de bout en bout : build Docker, push ACR, création d’agent hébergé/par prompt, démarrage de conteneur, évaluation par lots, évaluation continue, workflows d’optimisation de prompt, agent.yaml, curation de jeux de données à partir de traces. UTILISER POUR : déployer un agent vers Foundry, agent hébergé, créer un agent, invoquer un agent, évaluer un agent, exécuter une évaluation par lots, évaluation continue, surveillance continue, statut d’évaluation continue, optimiser un prompt, améliorer un prompt, optimiseur de prompt, optimiser les instructions d’un agent, améliorer un agent...
officialdevelopmentdevops
azure-ai
microsoft
Utiliser pour Azure AI : Recherche, Parole, OpenAI, Intelligence documentaire. Aide pour la recherche, la recherche vectorielle/hybride, la reconnaissance vocale, la synthèse vocale, la transcription, l'OCR. QUAND : Recherche AI, recherche par requête, recherche vectorielle, recherche hybride, recherche sémantique, reconnaissance vocale, synthèse vocale, transcrire, OCR, convertir du texte en parole.
officialdevelopmentapi
azure-deploy
microsoft
Exécutez les déploiements Azure pour les applications DÉJÀ PRÉPARÉES disposant de fichiers .azure/deployment-plan.md et d'infrastructure existants. N'utilisez PAS cette compétence lorsque l'utilisateur demande de CRÉER une nouvelle application — utilisez plutôt azure-prepare. Cette compétence exécute les commandes azd up, azd deploy, terraform apply et az deployment avec une récupération d'erreur intégrée. Nécessite .azure/deployment-plan.md de azure-prepare et un état validé de azure-validate. QUAND : "exécuter azd up", "exécuter azd deploy", "exécuter le déploiement",...
officialdevopsaws
azure-storage
microsoft
Services Azure Storage incluant Blob Storage, File Shares, Queue Storage, Table Storage et Data Lake. Répond aux questions sur les niveaux d'accès au stockage (chaud, froid, froid, archive), quand utiliser chaque niveau et comparaison des niveaux. Fournit du stockage d'objets, des partages de fichiers SMB, de la messagerie asynchrone, du NoSQL clé-valeur et de l'analyse de big data. Inclut la gestion du cycle de vie. À UTILISER POUR : stockage blob, partages de fichiers, stockage de files d'attente, stockage de tables, data lake, téléchargement de fichiers, téléchargement de blobs, comptes de stockage, niveaux d'accès,...
officialdevelopmentdatabase
azure-diagnostics
microsoft
Déboguer les problèmes de production Azure à l'aide d'AppLens, Azure Monitor, l'état des ressources et un triage sécurisé. QUAND : déboguer des problèmes de production, résoudre les problèmes d'App Service, CPU élevé d'App Service, échec de déploiement d'App Service, résoudre les problèmes de Container Apps, résoudre les problèmes de Functions, résoudre les problèmes d'AKS, kubectl ne peut pas se connecter, échecs kube-system/CoreDNS, pod en attente, crashloop, nœud non prêt, échecs de mise à niveau, analyser les logs, KQL, insights, échecs de pull d'image, problèmes de démarrage à froid, échecs de sonde de santé,...
officialdevopsdevelopment
azure-prepare
microsoft
Préparer les applications Azure pour le déploiement (infra Bicep/Terraform, azure.yaml, Dockerfiles). Utiliser pour créer/moderniser ou créer+déployer ; pas pour la migration cross-cloud (utiliser azure-cloud-migrate). NE PAS UTILISER POUR : les applications copilot-sdk (utiliser azure-hosted-copilot-sdk). QUAND : "créer une application", "construire une application web", "créer une API", "créer une API HTTP serverless", "créer un frontend", "créer un backend", "construire un service", "moderniser une application", "mettre à jour une application", "ajouter une authentification", "ajouter un cache", "héberger sur Azure", "créer et...
officialdevelopmentdevops
azure-validate
microsoft
Validation pré-déploiement pour la préparation Azure. Effectuez des vérifications approfondies sur la configuration, l'infrastructure (Bicep ou Terraform), les attributions de rôles RBAC, les autorisations d'identité managée et les prérequis avant le déploiement. QUAND : valider mon application, vérifier l'état de préparation au déploiement, exécuter des contrôles préalables, vérifier la configuration, vérifier si prêt à déployer, valider azure.yaml, valider Bicep, tester avant le déploiement, résoudre les erreurs de déploiement, valider Azure Functions, valider l'application de fonction, valider serverless...
officialdevopstesting