Renamed async_credential to credential (#2648)

This commit is contained in:
Dmytro Struk
2025-12-07 17:21:18 -08:00
committed by GitHub
Unverified
parent eb06faea2d
commit cfcb71334a
64 changed files with 266 additions and 269 deletions
@@ -122,7 +122,7 @@ class AzureAIAgentClient(BaseChatClient):
thread_id: str | None = None,
project_endpoint: str | None = None,
model_deployment_name: str | None = None,
async_credential: AsyncTokenCredential | None = None,
credential: AsyncTokenCredential | None = None,
should_cleanup_agent: bool = True,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
@@ -144,7 +144,7 @@ class AzureAIAgentClient(BaseChatClient):
Ignored when a agents_client is passed.
model_deployment_name: The model deployment name to use for agent creation.
Can also be set via environment variable AZURE_AI_MODEL_DEPLOYMENT_NAME.
async_credential: Azure async credential to use for authentication.
credential: Azure async credential to use for authentication.
should_cleanup_agent: Whether to cleanup (delete) agents created by this client when
the client is closed or context is exited. Defaults to True. Only affects agents
created by this client instance; existing agents passed via agent_id are never deleted.
@@ -162,17 +162,17 @@ class AzureAIAgentClient(BaseChatClient):
# Set AZURE_AI_PROJECT_ENDPOINT=https://your-project.cognitiveservices.azure.com
# Set AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4
credential = DefaultAzureCredential()
client = AzureAIAgentClient(async_credential=credential)
client = AzureAIAgentClient(credential=credential)
# Or passing parameters directly
client = AzureAIAgentClient(
project_endpoint="https://your-project.cognitiveservices.azure.com",
model_deployment_name="gpt-4",
async_credential=credential,
credential=credential,
)
# Or loading from a .env file
client = AzureAIAgentClient(async_credential=credential, env_file_path="path/to/.env")
client = AzureAIAgentClient(credential=credential, env_file_path="path/to/.env")
"""
try:
azure_ai_settings = AzureAISettings(
@@ -200,11 +200,11 @@ class AzureAIAgentClient(BaseChatClient):
)
# Use provided credential
if not async_credential:
if not credential:
raise ServiceInitializationError("Azure credential is required when agents_client is not provided.")
agents_client = AgentsClient(
endpoint=azure_ai_settings.project_endpoint,
credential=async_credential,
credential=credential,
user_agent=AGENT_FRAMEWORK_USER_AGENT,
)
should_close_client = True
@@ -214,7 +214,7 @@ class AzureAIAgentClient(BaseChatClient):
# Initialize instance variables
self.agents_client = agents_client
self.credential = async_credential
self.credential = credential
self.agent_id = agent_id
self.agent_name = agent_name
self.agent_description = agent_description
@@ -66,7 +66,7 @@ class AzureAIClient(OpenAIBaseResponsesClient):
conversation_id: str | None = None,
project_endpoint: str | None = None,
model_deployment_name: str | None = None,
async_credential: AsyncTokenCredential | None = None,
credential: AsyncTokenCredential | None = None,
use_latest_version: bool | None = None,
env_file_path: str | None = None,
env_file_encoding: str | None = None,
@@ -86,7 +86,7 @@ class AzureAIClient(OpenAIBaseResponsesClient):
Ignored when a project_client is passed.
model_deployment_name: The model deployment name to use for agent creation.
Can also be set via environment variable AZURE_AI_MODEL_DEPLOYMENT_NAME.
async_credential: Azure async credential to use for authentication.
credential: Azure async credential to use for authentication.
use_latest_version: Boolean flag that indicates whether to use latest agent version
if it exists in the service.
env_file_path: Path to environment file for loading settings.
@@ -103,17 +103,17 @@ class AzureAIClient(OpenAIBaseResponsesClient):
# Set AZURE_AI_PROJECT_ENDPOINT=https://your-project.cognitiveservices.azure.com
# Set AZURE_AI_MODEL_DEPLOYMENT_NAME=gpt-4
credential = DefaultAzureCredential()
client = AzureAIClient(async_credential=credential)
client = AzureAIClient(credential=credential)
# Or passing parameters directly
client = AzureAIClient(
project_endpoint="https://your-project.cognitiveservices.azure.com",
model_deployment_name="gpt-4",
async_credential=credential,
credential=credential,
)
# Or loading from a .env file
client = AzureAIClient(async_credential=credential, env_file_path="path/to/.env")
client = AzureAIClient(credential=credential, env_file_path="path/to/.env")
"""
try:
azure_ai_settings = AzureAISettings(
@@ -135,11 +135,11 @@ class AzureAIClient(OpenAIBaseResponsesClient):
)
# Use provided credential
if not async_credential:
if not credential:
raise ServiceInitializationError("Azure credential is required when project_client is not provided.")
project_client = AIProjectClient(
endpoint=azure_ai_settings.project_endpoint,
credential=async_credential,
credential=credential,
user_agent=AGENT_FRAMEWORK_USER_AGENT,
)
should_close_client = True
@@ -155,7 +155,7 @@ class AzureAIClient(OpenAIBaseResponsesClient):
self.agent_description = agent_description
self.use_latest_version = use_latest_version
self.project_client = project_client
self.credential = async_credential
self.credential = credential
self.model_id = azure_ai_settings.model_deployment_name
self.conversation_id = conversation_id
@@ -170,7 +170,7 @@ def test_azure_ai_chat_client_init_missing_project_endpoint() -> None:
agent_id=None,
project_endpoint=None, # Missing endpoint
model_deployment_name="test-model",
async_credential=AsyncMock(spec=AsyncTokenCredential),
credential=AsyncMock(spec=AsyncTokenCredential),
)
@@ -190,7 +190,7 @@ def test_azure_ai_chat_client_init_missing_model_deployment_for_agent_creation()
agent_id=None, # No existing agent
project_endpoint="https://test.com",
model_deployment_name=None, # Missing for agent creation
async_credential=AsyncMock(spec=AsyncTokenCredential),
credential=AsyncMock(spec=AsyncTokenCredential),
)
@@ -223,7 +223,7 @@ def test_azure_ai_chat_client_from_dict(mock_agents_client: MagicMock) -> None:
def test_azure_ai_chat_client_init_missing_credential(azure_ai_unit_test_env: dict[str, str]) -> None:
"""Test AzureAIAgentClient.__init__ when async_credential is missing and no agents_client provided."""
"""Test AzureAIAgentClient.__init__ when credential is missing and no agents_client provided."""
with pytest.raises(
ServiceInitializationError, match="Azure credential is required when agents_client is not provided"
):
@@ -232,7 +232,7 @@ def test_azure_ai_chat_client_init_missing_credential(azure_ai_unit_test_env: di
agent_id="existing-agent",
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
model_deployment_name=azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
async_credential=None, # Missing credential
credential=None, # Missing credential
)
@@ -246,7 +246,7 @@ def test_azure_ai_chat_client_init_validation_error(mock_azure_credential: Magic
AzureAIAgentClient(
project_endpoint="https://test.com",
model_deployment_name="test-model",
async_credential=mock_azure_credential,
credential=mock_azure_credential,
)
@@ -1373,7 +1373,7 @@ def get_weather(
@skip_if_azure_ai_integration_tests_disabled
async def test_azure_ai_chat_client_get_response() -> None:
"""Test Azure AI Chat Client response."""
async with AzureAIAgentClient(async_credential=AzureCliCredential()) as azure_ai_chat_client:
async with AzureAIAgentClient(credential=AzureCliCredential()) as azure_ai_chat_client:
assert isinstance(azure_ai_chat_client, ChatClientProtocol)
messages: list[ChatMessage] = []
@@ -1398,7 +1398,7 @@ async def test_azure_ai_chat_client_get_response() -> None:
@skip_if_azure_ai_integration_tests_disabled
async def test_azure_ai_chat_client_get_response_tools() -> None:
"""Test Azure AI Chat Client response with tools."""
async with AzureAIAgentClient(async_credential=AzureCliCredential()) as azure_ai_chat_client:
async with AzureAIAgentClient(credential=AzureCliCredential()) as azure_ai_chat_client:
assert isinstance(azure_ai_chat_client, ChatClientProtocol)
messages: list[ChatMessage] = []
@@ -1420,7 +1420,7 @@ async def test_azure_ai_chat_client_get_response_tools() -> None:
@skip_if_azure_ai_integration_tests_disabled
async def test_azure_ai_chat_client_streaming() -> None:
"""Test Azure AI Chat Client streaming response."""
async with AzureAIAgentClient(async_credential=AzureCliCredential()) as azure_ai_chat_client:
async with AzureAIAgentClient(credential=AzureCliCredential()) as azure_ai_chat_client:
assert isinstance(azure_ai_chat_client, ChatClientProtocol)
messages: list[ChatMessage] = []
@@ -1451,7 +1451,7 @@ async def test_azure_ai_chat_client_streaming() -> None:
@skip_if_azure_ai_integration_tests_disabled
async def test_azure_ai_chat_client_streaming_tools() -> None:
"""Test Azure AI Chat Client streaming response with tools."""
async with AzureAIAgentClient(async_credential=AzureCliCredential()) as azure_ai_chat_client:
async with AzureAIAgentClient(credential=AzureCliCredential()) as azure_ai_chat_client:
assert isinstance(azure_ai_chat_client, ChatClientProtocol)
messages: list[ChatMessage] = []
@@ -1479,7 +1479,7 @@ async def test_azure_ai_chat_client_streaming_tools() -> None:
async def test_azure_ai_chat_client_agent_basic_run() -> None:
"""Test ChatAgent basic run functionality with AzureAIAgentClient."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
) as agent:
# Run a simple query
response = await agent.run("Hello! Please respond with 'Hello World' exactly.")
@@ -1496,7 +1496,7 @@ async def test_azure_ai_chat_client_agent_basic_run() -> None:
async def test_azure_ai_chat_client_agent_basic_run_streaming() -> None:
"""Test ChatAgent basic streaming functionality with AzureAIAgentClient."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
) as agent:
# Run streaming query
full_message: str = ""
@@ -1516,7 +1516,7 @@ async def test_azure_ai_chat_client_agent_basic_run_streaming() -> None:
async def test_azure_ai_chat_client_agent_thread_persistence() -> None:
"""Test ChatAgent thread persistence across runs with AzureAIAgentClient."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant with good memory.",
) as agent:
# Create a new thread that will be reused
@@ -1542,7 +1542,7 @@ async def test_azure_ai_chat_client_agent_thread_persistence() -> None:
async def test_azure_ai_chat_client_agent_existing_thread_id() -> None:
"""Test ChatAgent existing thread ID functionality with AzureAIAgentClient."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant with good memory.",
) as first_agent:
# Start a conversation and get the thread ID
@@ -1559,7 +1559,7 @@ async def test_azure_ai_chat_client_agent_existing_thread_id() -> None:
# Now continue with the same thread ID in a new agent instance
async with ChatAgent(
chat_client=AzureAIAgentClient(thread_id=existing_thread_id, async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(thread_id=existing_thread_id, credential=AzureCliCredential()),
instructions="You are a helpful assistant with good memory.",
) as second_agent:
# Create a thread with the existing ID
@@ -1581,7 +1581,7 @@ async def test_azure_ai_chat_client_agent_code_interpreter():
"""Test ChatAgent with code interpreter through AzureAIAgentClient."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant that can write and execute Python code.",
tools=[HostedCodeInterpreterTool()],
) as agent:
@@ -1600,7 +1600,7 @@ async def test_azure_ai_chat_client_agent_code_interpreter():
async def test_azure_ai_chat_client_agent_file_search():
"""Test ChatAgent with file search through AzureAIAgentClient."""
client = AzureAIAgentClient(async_credential=AzureCliCredential())
client = AzureAIAgentClient(credential=AzureCliCredential())
file: FileInfo | None = None
vector_store: VectorStore | None = None
@@ -1655,7 +1655,7 @@ async def test_azure_ai_chat_client_agent_hosted_mcp_tool() -> None:
)
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
tools=[mcp_tool],
) as agent:
@@ -1682,7 +1682,7 @@ async def test_azure_ai_chat_client_agent_hosted_mcp_tool() -> None:
async def test_azure_ai_chat_client_agent_level_tool_persistence():
"""Test that agent-level tools persist across multiple runs with AzureAIAgentClient."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant that uses available tools.",
tools=[get_weather],
) as agent:
@@ -1707,7 +1707,7 @@ async def test_azure_ai_chat_client_agent_level_tool_persistence():
async def test_azure_ai_chat_client_agent_chat_options_run_level() -> None:
"""Test ChatOptions parameter coverage at run level."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant.",
) as agent:
response = await agent.run(
@@ -1737,7 +1737,7 @@ async def test_azure_ai_chat_client_agent_chat_options_run_level() -> None:
async def test_azure_ai_chat_client_agent_chat_options_agent_level() -> None:
"""Test ChatOptions parameter coverage agent level."""
async with ChatAgent(
chat_client=AzureAIAgentClient(async_credential=AzureCliCredential()),
chat_client=AzureAIAgentClient(credential=AzureCliCredential()),
instructions="You are a helpful assistant.",
max_tokens=100,
temperature=0.7,
@@ -1963,7 +1963,7 @@ def test_azure_ai_chat_client_init_with_auto_created_agents_client(
agent_id="test-agent",
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
model_deployment_name=azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
async_credential=mock_azure_credential,
credential=mock_azure_credential,
)
# Verify AgentsClient was created with correct parameters
@@ -152,7 +152,7 @@ def test_azure_ai_client_init_auto_create_client(
client = AzureAIClient(
project_endpoint=azure_ai_unit_test_env["AZURE_AI_PROJECT_ENDPOINT"],
model_deployment_name=azure_ai_unit_test_env["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
async_credential=mock_azure_credential,
credential=mock_azure_credential,
agent_name="test-agent",
)
@@ -171,11 +171,11 @@ def test_azure_ai_client_init_missing_project_endpoint() -> None:
mock_settings.return_value.model_deployment_name = "test-model"
with pytest.raises(ServiceInitializationError, match="Azure AI project endpoint is required"):
AzureAIClient(async_credential=MagicMock())
AzureAIClient(credential=MagicMock())
def test_azure_ai_client_init_missing_credential(azure_ai_unit_test_env: dict[str, str]) -> None:
"""Test AzureAIClient.__init__ when async_credential is missing and no project_client provided."""
"""Test AzureAIClient.__init__ when credential is missing and no project_client provided."""
with pytest.raises(
ServiceInitializationError, match="Azure credential is required when project_client is not provided"
):
@@ -191,7 +191,7 @@ def test_azure_ai_client_init_validation_error(mock_azure_credential: MagicMock)
mock_settings.side_effect = ValidationError.from_exception_data("test", [])
with pytest.raises(ServiceInitializationError, match="Failed to create Azure AI settings"):
AzureAIClient(async_credential=mock_azure_credential)
AzureAIClient(credential=mock_azure_credential)
async def test_azure_ai_client_get_agent_reference_or_create_existing_version(
@@ -320,7 +320,7 @@ async def test_azure_ai_client_prepare_options_with_application_endpoint(
client = AzureAIClient(
project_endpoint=endpoint,
model_deployment_name="test-model",
async_credential=mock_azure_credential,
credential=mock_azure_credential,
agent_name="test-agent",
agent_version="1",
)
@@ -49,7 +49,7 @@ async def create_gaia_agent() -> AsyncIterator[ChatAgent]:
"""
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="GaiaAgent",
instructions="Solve tasks to your best ability. Use Bing Search to find "
"information and Code Interpreter to perform calculations and data analysis.",
@@ -21,7 +21,7 @@ WORKFLOW STRUCTURE (7 agents):
Agents:
1. Travel Agent - Main coordinator (no tools to avoid thread conflicts)
2. Hotel Search - Searches hotels with tools
3. Flight Search - Searches flights with tools
3. Flight Search - Searches flights with tools
4. Activity Search - Searches activities with tools
5. Booking Information Aggregation - Aggregates hotel & flight booking info
6. Booking Confirmation - Confirms bookings with tools
@@ -32,40 +32,37 @@ import asyncio
import os
from collections import defaultdict
from dotenv import load_dotenv
from agent_framework import (
AgentExecutorResponse,
AgentRunUpdateEvent,
AgentRunResponseUpdate,
ChatMessage,
Executor,
executor,
handler,
Role,
WorkflowContext,
WorkflowBuilder,
WorkflowOutputEvent,
)
from typing_extensions import Never
from agent_framework.azure import AzureAIClient
from azure.identity.aio import DefaultAzureCredential
from azure.ai.projects.aio import AIProjectClient
from _tools import (
check_flight_availability,
check_hotel_availability,
confirm_booking,
get_flight_details,
get_hotel_details,
process_payment,
search_activities,
search_flights,
# Travel planning tools
search_hotels,
get_hotel_details,
search_flights,
get_flight_details,
search_activities,
confirm_booking,
check_hotel_availability,
check_flight_availability,
process_payment,
validate_payment_method,
)
from agent_framework import (
AgentExecutorResponse,
AgentRunResponseUpdate,
AgentRunUpdateEvent,
ChatMessage,
Executor,
Role,
WorkflowBuilder,
WorkflowContext,
WorkflowOutputEvent,
executor,
handler,
)
from agent_framework.azure import AzureAIClient
from azure.ai.projects.aio import AIProjectClient
from azure.identity.aio import DefaultAzureCredential
from dotenv import load_dotenv
from typing_extensions import Never
load_dotenv()
@@ -78,7 +75,7 @@ async def start_executor(input: str, ctx: WorkflowContext[list[ChatMessage]]) ->
class ResearchLead(Executor):
"""Aggregates and summarizes travel planning findings from all specialized agents."""
def __init__(self, chat_client: AzureAIClient, id: str = "travel-planning-coordinator"):
# store=True to preserve conversation history for evaluation
self.agent = chat_client.create_agent(
@@ -92,58 +89,69 @@ class ResearchLead(Executor):
"Clearly indicate which information came from which agent. Do not use tools."
),
name="travel-planning-coordinator",
store=True
store=True,
)
super().__init__(id=id)
@handler
async def fan_in_handle(self, responses: list[AgentExecutorResponse], ctx: WorkflowContext[Never, str]) -> None:
user_query = responses[0].full_conversation[0].text
# Extract findings from all agent responses
agent_findings = self._extract_agent_findings(responses)
summary_text = "\n".join(agent_findings) if agent_findings else "No specific findings were provided by the agents."
summary_text = (
"\n".join(agent_findings) if agent_findings else "No specific findings were provided by the agents."
)
# Generate comprehensive travel plan summary
messages = [
ChatMessage(role=Role.SYSTEM, text="You are a travel planning coordinator. Summarize findings from multiple specialized travel agents and provide a clear, comprehensive travel plan based on the user's query."),
ChatMessage(role=Role.USER, text=f"Original query: {user_query}\n\nFindings from specialized travel agents:\n{summary_text}\n\nPlease provide a comprehensive travel plan based on these findings.")
ChatMessage(
role=Role.SYSTEM,
text="You are a travel planning coordinator. Summarize findings from multiple specialized travel agents and provide a clear, comprehensive travel plan based on the user's query.",
),
ChatMessage(
role=Role.USER,
text=f"Original query: {user_query}\n\nFindings from specialized travel agents:\n{summary_text}\n\nPlease provide a comprehensive travel plan based on these findings.",
),
]
try:
final_response = await self.agent.run(messages)
output_text = (final_response.messages[-1].text if final_response.messages and final_response.messages[-1].text
else f"Based on the available findings, here's your travel plan for '{user_query}': {summary_text}")
output_text = (
final_response.messages[-1].text
if final_response.messages and final_response.messages[-1].text
else f"Based on the available findings, here's your travel plan for '{user_query}': {summary_text}"
)
except Exception:
output_text = f"Based on the available findings, here's your travel plan for '{user_query}': {summary_text}"
await ctx.yield_output(output_text)
def _extract_agent_findings(self, responses: list[AgentExecutorResponse]) -> list[str]:
"""Extract findings from agent responses."""
agent_findings = []
for response in responses:
findings = []
if response.agent_run_response and response.agent_run_response.messages:
for msg in response.agent_run_response.messages:
if msg.role == Role.ASSISTANT and msg.text and msg.text.strip():
findings.append(msg.text.strip())
if findings:
combined_findings = " ".join(findings)
agent_findings.append(f"[{response.executor_id}]: {combined_findings}")
return agent_findings
async def run_workflow_with_response_tracking(query: str, chat_client: AzureAIClient | None = None) -> dict:
"""Run multi-agent workflow and track conversation IDs, response IDs, and interaction sequence.
Args:
query: The user query to process through the multi-agent workflow
chat_client: Optional AzureAIClient instance
Returns:
Dictionary containing interaction sequence, conversation/response IDs, and conversation analysis
"""
@@ -155,11 +163,11 @@ async def run_workflow_with_response_tracking(query: str, chat_client: AzureAICl
credential=credential,
api_version="2025-11-15-preview",
)
async with (
DefaultAzureCredential() as credential,
project_client,
AzureAIClient(project_client=project_client, async_credential=credential) as client
AzureAIClient(project_client=project_client, credential=credential) as client,
):
return await _run_workflow_with_client(query, client)
except Exception as e:
@@ -171,53 +179,46 @@ async def run_workflow_with_response_tracking(query: str, chat_client: AzureAICl
async def _run_workflow_with_client(query: str, chat_client: AzureAIClient) -> dict:
"""Execute workflow with given client and track all interactions."""
# Initialize tracking variables - use lists to track multiple responses per agent
conversation_ids = defaultdict(list)
response_ids = defaultdict(list)
workflow_output = None
# Create workflow components and keep agent references
# Pass project_client and credential to create separate client instances per agent
workflow, agent_map = await _create_workflow(
chat_client.project_client,
chat_client.credential
)
workflow, agent_map = await _create_workflow(chat_client.project_client, chat_client.credential)
# Process workflow events
events = workflow.run_stream(query)
workflow_output = await _process_workflow_events(events, conversation_ids, response_ids)
return {
"conversation_ids": dict(conversation_ids),
"response_ids": dict(response_ids),
"output": workflow_output,
"query": query
"query": query,
}
async def _create_workflow(project_client, credential):
"""Create the multi-agent travel planning workflow with specialized agents.
IMPORTANT: Each agent needs its own client instance because the V2 client stores
agent_name and agent_version as instance variables, causing all agents to share
the same agent identity if they share a client.
"""
# Create separate client for Final Coordinator
final_coordinator_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="final-coordinator"
project_client=project_client, credential=credential, agent_name="final-coordinator"
)
final_coordinator = ResearchLead(chat_client=final_coordinator_client, id="final-coordinator")
# Agent 1: Travel Request Handler (initial coordinator)
# Create separate client with unique agent_name
travel_request_handler_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="travel-request-handler"
project_client=project_client, credential=credential, agent_name="travel-request-handler"
)
travel_request_handler = travel_request_handler_client.create_agent(
id="travel-request-handler",
@@ -225,14 +226,12 @@ async def _create_workflow(project_client, credential):
"You receive user travel queries and relay them to specialized agents. Extract key information: destination, dates, budget, and preferences. Pass this information forward clearly to the next agents."
),
name="travel-request-handler",
store=True
store=True,
)
# Agent 2: Hotel Search Executor
hotel_search_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="hotel-search-agent"
project_client=project_client, credential=credential, agent_name="hotel-search-agent"
)
hotel_search_agent = hotel_search_client.create_agent(
id="hotel-search-agent",
@@ -241,14 +240,12 @@ async def _create_workflow(project_client, credential):
),
name="hotel-search-agent",
tools=[search_hotels, get_hotel_details, check_hotel_availability],
store=True
store=True,
)
# Agent 3: Flight Search Executor
flight_search_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="flight-search-agent"
project_client=project_client, credential=credential, agent_name="flight-search-agent"
)
flight_search_agent = flight_search_client.create_agent(
id="flight-search-agent",
@@ -257,14 +254,12 @@ async def _create_workflow(project_client, credential):
),
name="flight-search-agent",
tools=[search_flights, get_flight_details, check_flight_availability],
store=True
store=True,
)
# Agent 4: Activity Search Executor
activity_search_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="activity-search-agent"
project_client=project_client, credential=credential, agent_name="activity-search-agent"
)
activity_search_agent = activity_search_client.create_agent(
id="activity-search-agent",
@@ -273,14 +268,12 @@ async def _create_workflow(project_client, credential):
),
name="activity-search-agent",
tools=[search_activities],
store=True
store=True,
)
# Agent 5: Booking Confirmation Executor
booking_confirmation_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="booking-confirmation-agent"
project_client=project_client, credential=credential, agent_name="booking-confirmation-agent"
)
booking_confirmation_agent = booking_confirmation_client.create_agent(
id="booking-confirmation-agent",
@@ -289,14 +282,12 @@ async def _create_workflow(project_client, credential):
),
name="booking-confirmation-agent",
tools=[confirm_booking, check_hotel_availability, check_flight_availability],
store=True
store=True,
)
# Agent 6: Booking Payment Executor
booking_payment_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="booking-payment-agent"
project_client=project_client, credential=credential, agent_name="booking-payment-agent"
)
booking_payment_agent = booking_payment_client.create_agent(
id="booking-payment-agent",
@@ -305,14 +296,12 @@ async def _create_workflow(project_client, credential):
),
name="booking-payment-agent",
tools=[process_payment, validate_payment_method],
store=True
store=True,
)
# Agent 7: Booking Information Aggregation Executor
booking_info_client = AzureAIClient(
project_client=project_client,
async_credential=credential,
agent_name="booking-info-aggregation-agent"
project_client=project_client, credential=credential, agent_name="booking-info-aggregation-agent"
)
booking_info_aggregation_agent = booking_info_client.create_agent(
id="booking-info-aggregation-agent",
@@ -320,9 +309,9 @@ async def _create_workflow(project_client, credential):
"You aggregate hotel and flight search results. Receive options from search agents and organize them. Provide: top 2-3 hotel options with prices and top 2-3 flight options with prices in a structured format."
),
name="booking-info-aggregation-agent",
store=True
store=True,
)
# Build workflow with logical booking flow:
# 1. start_executor → travel_request_handler
# 2. travel_request_handler → hotel_search, flight_search, activity_search (fan-out)
@@ -331,19 +320,22 @@ async def _create_workflow(project_client, credential):
# 5. booking_info_aggregation → booking_confirmation
# 6. booking_confirmation → booking_payment
# 7. booking_info_aggregation, booking_payment, activity_search → final_coordinator (final aggregation, fan-in)
workflow = (WorkflowBuilder(name='Travel Planning Workflow')
.set_start_executor(start_executor)
.add_edge(start_executor, travel_request_handler)
.add_fan_out_edges(travel_request_handler, [hotel_search_agent, flight_search_agent, activity_search_agent])
.add_edge(hotel_search_agent, booking_info_aggregation_agent)
.add_edge(flight_search_agent, booking_info_aggregation_agent)
.add_edge(booking_info_aggregation_agent, booking_confirmation_agent)
.add_edge(booking_confirmation_agent, booking_payment_agent)
.add_fan_in_edges([booking_info_aggregation_agent, booking_payment_agent, activity_search_agent],
final_coordinator)
.build())
workflow = (
WorkflowBuilder(name="Travel Planning Workflow")
.set_start_executor(start_executor)
.add_edge(start_executor, travel_request_handler)
.add_fan_out_edges(travel_request_handler, [hotel_search_agent, flight_search_agent, activity_search_agent])
.add_edge(hotel_search_agent, booking_info_aggregation_agent)
.add_edge(flight_search_agent, booking_info_aggregation_agent)
.add_edge(booking_info_aggregation_agent, booking_confirmation_agent)
.add_edge(booking_confirmation_agent, booking_payment_agent)
.add_fan_in_edges(
[booking_info_aggregation_agent, booking_payment_agent, activity_search_agent], final_coordinator
)
.build()
)
# Return workflow and agent map for thread ID extraction
agent_map = {
"travel_request_handler": travel_request_handler,
@@ -355,14 +347,14 @@ async def _create_workflow(project_client, credential):
"booking-info-aggregation-agent": booking_info_aggregation_agent,
"final-coordinator": final_coordinator.agent,
}
return workflow, agent_map
async def _process_workflow_events(events, conversation_ids, response_ids):
"""Process workflow events and track interactions."""
workflow_output = None
async for event in events:
if isinstance(event, WorkflowOutputEvent):
workflow_output = event.data
@@ -370,12 +362,12 @@ async def _process_workflow_events(events, conversation_ids, response_ids):
try:
print(f"\nWorkflow Output: {event.data}\n")
except UnicodeEncodeError:
output_str = str(event.data).encode('ascii', 'replace').decode('ascii')
output_str = str(event.data).encode("ascii", "replace").decode("ascii")
print(f"\nWorkflow Output: {output_str}\n")
elif isinstance(event, AgentRunUpdateEvent):
_track_agent_ids(event, event.executor_id, response_ids, conversation_ids)
return workflow_output
@@ -384,21 +376,21 @@ def _track_agent_ids(event, agent, response_ids, conversation_ids):
if isinstance(event.data, AgentRunResponseUpdate):
# Check for conversation_id and response_id from raw_representation
# V2 API stores conversation_id directly on raw_representation (ChatResponseUpdate)
if hasattr(event.data, 'raw_representation') and event.data.raw_representation:
if hasattr(event.data, "raw_representation") and event.data.raw_representation:
raw = event.data.raw_representation
# Try conversation_id directly on raw representation
if hasattr(raw, 'conversation_id') and raw.conversation_id:
if hasattr(raw, "conversation_id") and raw.conversation_id:
# Only add if not already in the list
if raw.conversation_id not in conversation_ids[agent]:
conversation_ids[agent].append(raw.conversation_id)
# Extract response_id from the OpenAI event (available from first event)
if hasattr(raw, 'raw_representation') and raw.raw_representation:
if hasattr(raw, "raw_representation") and raw.raw_representation:
openai_event = raw.raw_representation
# Check if event has response object with id
if hasattr(openai_event, 'response') and hasattr(openai_event.response, 'id'):
if hasattr(openai_event, "response") and hasattr(openai_event.response, "id"):
# Only add if not already in the list
if openai_event.response.id not in response_ids[agent]:
response_ids[agent].append(openai_event.response.id)
@@ -406,7 +398,7 @@ def _track_agent_ids(event, agent, response_ids, conversation_ids):
async def create_and_run_workflow():
"""Run the workflow evaluation and display results.
Returns:
Dictionary containing agents data with conversation IDs, response IDs, and query information
"""
@@ -415,36 +407,32 @@ async def create_and_run_workflow():
"Find a budget hotel in Tokyo for January 5-10, 2026 under $150/night near Shibuya station, book activities including a sushi making class",
"Search for round-trip flights from Los Angeles to London departing March 20, 2026, returning March 27, 2026. Economy class, 2 passengers. Recommend tourist attractions and museums.",
]
query = example_queries[0]
print(f"Query: {query}\n")
result = await run_workflow_with_response_tracking(query)
# Create output data structure
output_data = {
"agents": {},
"query": result["query"],
"output": result.get("output", "")
}
output_data = {"agents": {}, "query": result["query"], "output": result.get("output", "")}
# Create agent-specific mappings - now with lists of IDs
all_agents = set(result["conversation_ids"].keys()) | set(result["response_ids"].keys())
for agent_name in all_agents:
output_data["agents"][agent_name] = {
"conversation_ids": result["conversation_ids"].get(agent_name, []),
"response_ids": result["response_ids"].get(agent_name, []),
"response_count": len(result["response_ids"].get(agent_name, []))
"response_count": len(result["response_ids"].get(agent_name, [])),
}
print(f"\nTotal agents tracked: {len(output_data['agents'])}")
# Print summary of multiple responses
print("\n=== Multi-Response Summary ===")
for agent_name, agent_data in output_data["agents"].items():
response_count = agent_data["response_count"]
print(f"{agent_name}: {response_count} response(s)")
return output_data
@@ -33,7 +33,7 @@ async def non_streaming_example() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="BasicWeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -54,7 +54,7 @@ async def streaming_example() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="BasicWeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -31,7 +31,7 @@ async def main() -> None:
async with AzureCliCredential() as credential:
async with (
AzureAIClient(
async_credential=credential,
credential=credential,
).create_agent(
name="MyWeatherAgent",
instructions="You are a helpful weather agent.",
@@ -47,7 +47,7 @@ async def main() -> None:
# Create a new agent instance
async with (
AzureAIClient(
async_credential=credential,
credential=credential,
# This parameter will allow to re-use latest agent version
# instead of creating a new one
use_latest_version=True,
@@ -21,7 +21,7 @@ Prerequisites:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyA2AAgent",
instructions="""You are a helpful assistant that can communicate with other agents.
Use the A2A tool when you need to interact with other agents to complete tasks
@@ -21,7 +21,7 @@ Prerequisites:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MySearchAgent",
instructions="""You are a helpful assistant. You must always provide citations for
answers using the tool and render them as: `[message_idx:search_idx†source]`.""",
@@ -21,7 +21,7 @@ Prerequisites:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyCustomSearchAgent",
instructions="""You are a helpful agent that can use Bing Custom Search tools to assist users.
Use the available Bing Custom Search tools to answer questions and perform tasks.""",
@@ -27,7 +27,7 @@ To get your Bing connection ID:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyBingGroundingAgent",
instructions="""You are a helpful assistant that can search the web for current information.
Use the Bing search tool to find up-to-date information and provide accurate, well-sourced answers.
@@ -21,7 +21,7 @@ Prerequisites:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyBrowserAutomationAgent",
instructions="""You are an Agent helping with browser automation tasks.
You can answer questions, provide information, and assist with various tasks
@@ -21,7 +21,7 @@ async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyCodeInterpreterAgent",
instructions="You are a helpful assistant that can write and execute Python code to solve problems.",
tools=HostedCodeInterpreterTool(),
@@ -36,7 +36,7 @@ async def main() -> None:
chat_client=AzureAIClient(
project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
model_deployment_name=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
async_credential=credential,
credential=credential,
agent_name="WeatherAgent",
),
instructions="You are a helpful weather agent.",
@@ -32,7 +32,7 @@ async def main() -> None:
async with (
AzureCliCredential() as credential,
AgentsClient(endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"], credential=credential) as agents_client,
AzureAIClient(async_credential=credential) as client,
AzureAIClient(credential=credential) as client,
):
try:
# 1. Upload file and create vector store
@@ -64,7 +64,7 @@ async def run_hosted_mcp_without_approval() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyLearnDocsAgent",
instructions="You are a helpful assistant that can help with Microsoft documentation questions.",
tools=HostedMCPTool(
@@ -89,7 +89,7 @@ async def run_hosted_mcp_with_approval_and_thread() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyApiSpecsAgent",
instructions="You are a helpful agent that can use MCP tools to assist users.",
tools=HostedMCPTool(
@@ -27,7 +27,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="ImageGenAgent",
instructions="Generate images based on user requirements.",
tools=[ImageGenTool(quality="low", size="1024x1024")],
@@ -24,7 +24,7 @@ async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="DocsAgent",
instructions="You are a helpful assistant that can help with Microsoft documentation questions.",
tools=MCPStreamableHTTPTool(
@@ -47,7 +47,7 @@ async def main() -> None:
print(f"Created memory store: {memory_store.name} ({memory_store.id}): {memory_store.description}")
# Then, create the agent using Agent Framework
async with AzureAIClient(async_credential=credential).create_agent(
async with AzureAIClient(credential=credential).create_agent(
name="MyMemoryAgent",
instructions="""You are a helpful assistant that remembers past conversations.
Use the memory search tool to recall relevant information from previous interactions.""",
@@ -21,7 +21,7 @@ Prerequisites:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyFabricAgent",
instructions="You are a helpful assistant.",
tools={
@@ -29,7 +29,7 @@ async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MyOpenAPIAgent",
instructions="""You are a helpful assistant that can use country APIs to provide information.
Use the available OpenAPI tools to answer questions about countries, currencies, and demographics.""",
@@ -29,7 +29,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="ProductMarketerAgent",
instructions="Return launch briefs as structured JSON.",
) as agent,
@@ -36,7 +36,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="ProductMarketerAgent",
instructions="Return launch briefs as structured JSON.",
) as agent,
@@ -21,7 +21,7 @@ Prerequisites:
async def main() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="MySharePointAgent",
instructions="""You are a helpful agent that can use SharePoint tools to assist users.
Use the available SharePoint tools to answer questions and perform tasks.""",
@@ -30,7 +30,7 @@ async def example_with_automatic_thread_creation() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="BasicWeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -59,7 +59,7 @@ async def example_with_thread_persistence_in_memory() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="BasicWeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -100,7 +100,7 @@ async def example_with_existing_thread_id() -> None:
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="BasicWeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -122,7 +122,7 @@ async def example_with_existing_thread_id() -> None:
print("\n--- Continuing with the same thread ID in a new agent instance ---")
async with (
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="BasicWeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -24,7 +24,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIClient(async_credential=credential).create_agent(
AzureAIClient(credential=credential).create_agent(
name="WebsearchAgent",
instructions="You are a helpful assistant that can search the web",
tools=[HostedWebSearchTool()],
@@ -34,7 +34,7 @@ async def non_streaming_example() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -56,7 +56,7 @@ async def streaming_example() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather agent.",
tools=get_weather,
@@ -39,7 +39,7 @@ async def main() -> None:
# 2. Use AzureAIAgentClient as async context manager for automatic cleanup
async with (
AzureAIAgentClient(async_credential=AzureCliCredential()) as client,
AzureAIAgentClient(credential=AzureCliCredential()) as client,
ChatAgent(
chat_client=client,
name="BingSearchAgent",
@@ -34,7 +34,7 @@ async def main() -> None:
# 2. Use AzureAIAgentClient as async context manager for automatic cleanup
async with (
AzureAIAgentClient(async_credential=AzureCliCredential()) as client,
AzureAIAgentClient(credential=AzureCliCredential()) as client,
ChatAgent(
chat_client=client,
name="BingSearchAgent",
@@ -39,7 +39,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential) as chat_client,
AzureAIAgentClient(credential=credential) as chat_client,
):
agent = chat_client.create_agent(
name="CodingAgent",
@@ -39,7 +39,7 @@ async def main() -> None:
chat_client=AzureAIAgentClient(
project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
model_deployment_name=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
async_credential=credential,
credential=credential,
agent_name="WeatherAgent",
should_cleanup_agent=True, # Set to False if you want to disable automatic agent cleanup
),
@@ -24,7 +24,7 @@ USER_INPUTS = [
async def main() -> None:
"""Main function demonstrating Azure AI agent with file search capabilities."""
client = AzureAIAgentClient(async_credential=AzureCliCredential())
client = AzureAIAgentClient(credential=AzureCliCredential())
file: FileInfo | None = None
vector_store: VectorStore | None = None
@@ -74,7 +74,7 @@ async def main() -> None:
# 6. Cleanup: Delete the vector store and file in case of earlier failure to prevent orphaned resources.
# Refreshing the client is required since chat agent closes it
client = AzureAIAgentClient(async_credential=AzureCliCredential())
client = AzureAIAgentClient(credential=AzureCliCredential())
try:
if vector_store:
await client.agents_client.vector_stores.delete(vector_store.id)
@@ -43,7 +43,7 @@ async def tools_on_agent_level() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
instructions="You are a helpful assistant that can provide weather and time information.",
tools=[get_weather, get_time], # Tools defined at agent creation
) as agent,
@@ -77,7 +77,7 @@ async def tools_on_run_level() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
instructions="You are a helpful assistant.",
# No tools defined here
) as agent,
@@ -111,7 +111,7 @@ async def mixed_tools_example() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
instructions="You are a comprehensive assistant that can help with various information requests.",
tools=[get_weather], # Base tool available for all queries
) as agent,
@@ -42,7 +42,7 @@ async def main() -> None:
"""Example showing Hosted MCP tools for a Azure AI Agent."""
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential) as chat_client,
AzureAIAgentClient(credential=credential) as chat_client,
):
agent = chat_client.create_agent(
name="DocsAgent",
@@ -28,7 +28,7 @@ async def mcp_tools_on_run_level() -> None:
url="https://learn.microsoft.com/api/mcp",
) as mcp_server,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
name="DocsAgent",
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
) as agent,
@@ -55,7 +55,7 @@ async def mcp_tools_on_agent_level() -> None:
# The agent will connect to the MCP server through its context manager.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="DocsAgent",
instructions="You are a helpful assistant that can help with microsoft documentation questions.",
tools=MCPStreamableHTTPTool( # Tools defined at agent creation
@@ -67,7 +67,7 @@ async def main() -> None:
"""Example showing Hosted MCP tools for a Azure AI Agent."""
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential) as chat_client,
AzureAIAgentClient(credential=credential) as chat_client,
):
agent = chat_client.create_agent(
name="DocsAgent",
@@ -41,7 +41,7 @@ async def main() -> None:
weather_openapi_spec, countries_openapi_spec = load_openapi_specs()
# 2. Use AzureAIAgentClient as async context manager for automatic cleanup
async with AzureAIAgentClient(async_credential=AzureCliCredential()) as client:
async with AzureAIAgentClient(credential=AzureCliCredential()) as client:
# 3. Create OpenAPI tools using Azure AI's OpenApiTool
auth = OpenApiAnonymousAuthDetails()
@@ -34,7 +34,7 @@ async def example_with_automatic_thread_creation() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent,
@@ -63,7 +63,7 @@ async def example_with_thread_persistence() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent,
@@ -104,7 +104,7 @@ async def example_with_existing_thread_id() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(async_credential=credential),
chat_client=AzureAIAgentClient(credential=credential),
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent,
@@ -127,7 +127,7 @@ async def example_with_existing_thread_id() -> None:
async with (
AzureCliCredential() as credential,
ChatAgent(
chat_client=AzureAIAgentClient(thread_id=existing_thread_id, async_credential=credential),
chat_client=AzureAIAgentClient(thread_id=existing_thread_id, credential=credential),
instructions="You are a helpful weather agent.",
tools=get_weather,
) as agent,
@@ -27,7 +27,7 @@ def get_weather(
async def main() -> None:
# For authentication, run `az login` command in terminal or replace AzureCliCredential with preferred
# authentication option.
async with AzureAIAgentClient(async_credential=AzureCliCredential()) as client:
async with AzureAIAgentClient(credential=AzureCliCredential()) as client:
message = "What's the weather in Amsterdam and in Paris?"
stream = False
print(f"User: {message}")
@@ -140,7 +140,7 @@ search_provider = AzureAISearchContextProvider(
)
# Create agent with search context
async with AzureAIAgentClient(async_credential=DefaultAzureCredential()) as client:
async with AzureAIAgentClient(credential=DefaultAzureCredential()) as client:
async with ChatAgent(
chat_client=client,
model=model_deployment,
@@ -110,7 +110,7 @@ async def main() -> None:
AzureAIAgentClient(
project_endpoint=project_endpoint,
model_deployment_name=model_deployment,
async_credential=AzureCliCredential(),
credential=AzureCliCredential(),
) as client,
ChatAgent(
chat_client=client,
@@ -67,7 +67,7 @@ async def main() -> None:
AzureAIAgentClient(
project_endpoint=project_endpoint,
model_deployment_name=model_deployment,
async_credential=AzureCliCredential(),
credential=AzureCliCredential(),
) as client,
ChatAgent(
chat_client=client,
@@ -32,7 +32,7 @@ async def main() -> None:
# For Mem0 authentication, set Mem0 API key via "api_key" parameter or MEM0_API_KEY environment variable.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="FriendlyAssistant",
instructions="You are a friendly assistant.",
tools=retrieve_company_report,
@@ -35,7 +35,7 @@ async def main() -> None:
local_mem0_client = AsyncMemory()
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="FriendlyAssistant",
instructions="You are a friendly assistant.",
tools=retrieve_company_report,
@@ -27,7 +27,7 @@ async def example_global_thread_scope() -> None:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="GlobalMemoryAssistant",
instructions="You are an assistant that remembers user preferences across conversations.",
tools=get_user_preferences,
@@ -65,7 +65,7 @@ async def example_per_operation_thread_scope() -> None:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="ScopedMemoryAssistant",
instructions="You are an assistant with thread-scoped memory.",
tools=get_user_preferences,
@@ -113,14 +113,14 @@ async def example_multiple_agents() -> None:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="PersonalAssistant",
instructions="You are a personal assistant that helps with personal tasks.",
context_providers=Mem0Provider(
agent_id=agent_id_1,
),
) as personal_agent,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WorkAssistant",
instructions="You are a work assistant that helps with professional tasks.",
context_providers=Mem0Provider(
@@ -91,7 +91,7 @@ class UserInfoMemory(ContextProvider):
async def main():
async with AzureCliCredential() as credential:
chat_client = AzureAIClient(async_credential=credential)
chat_client = AzureAIClient(credential=credential)
# Create the memory provider
memory_provider = UserInfoMemory(chat_client)
@@ -34,7 +34,7 @@ model:
# create the agent from the yaml
async with (
AzureCliCredential() as credential,
AgentFactory(client_kwargs={"async_credential": credential}).create_agent_from_yaml(yaml_definition) as agent,
AgentFactory(client_kwargs={"credential": credential}).create_agent_from_yaml(yaml_definition) as agent,
):
response = await agent.run("What can you do for me?")
print("Agent response:", response.text)
@@ -15,7 +15,7 @@ async def main():
# create the agent from the yaml
async with (
AzureCliCredential() as credential,
AgentFactory(client_kwargs={"async_credential": credential}).create_agent_from_yaml_path(yaml_path) as agent,
AgentFactory(client_kwargs={"credential": credential}).create_agent_from_yaml_path(yaml_path) as agent,
):
response = await agent.run("How do I create a storage account with private endpoint using bicep?")
print("Agent response:", response.text)
@@ -45,7 +45,7 @@ agent = ChatAgent(
chat_client=AzureAIAgentClient(
project_endpoint=os.environ.get("AZURE_AI_PROJECT_ENDPOINT"),
model_deployment_name=os.environ.get("FOUNDRY_MODEL_DEPLOYMENT_NAME"),
async_credential=AzureCliCredential(),
credential=AzureCliCredential(),
),
instructions="""
You are a weather assistant using Azure AI Foundry models. You can provide
@@ -166,7 +166,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather assistant.",
tools=get_weather,
@@ -142,7 +142,7 @@ async def class_based_chat_middleware() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="EnhancedChatAgent",
instructions="You are a helpful AI assistant.",
# Register class-based middleware at agent level (applies to all runs)
@@ -164,7 +164,7 @@ async def function_based_chat_middleware() -> None:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="FunctionMiddlewareAgent",
instructions="You are a helpful AI assistant.",
# Register function-based middleware at agent level
@@ -194,7 +194,7 @@ async def run_level_middleware() -> None:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="RunLevelAgent",
instructions="You are a helpful AI assistant.",
tools=get_weather,
@@ -99,7 +99,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather assistant.",
tools=get_weather,
@@ -70,7 +70,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="TimeAgent",
instructions="You are a helpful time assistant. Call get_current_time when asked about time.",
tools=get_current_time,
@@ -58,7 +58,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="DataAgent",
instructions="You are a helpful data assistant. Use the data service tool to fetch information for users.",
tools=unstable_data_service,
@@ -83,7 +83,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather assistant.",
tools=get_weather,
@@ -110,7 +110,7 @@ async def pre_termination_middleware() -> None:
print("\n--- Example 1: Pre-termination Middleware ---")
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather assistant.",
tools=get_weather,
@@ -137,7 +137,7 @@ async def post_termination_middleware() -> None:
print("\n--- Example 2: Post-termination Middleware ---")
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather assistant.",
tools=get_weather,
@@ -83,7 +83,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="WeatherAgent",
instructions="You are a helpful weather assistant. Use the weather tool to get current conditions.",
tools=get_weather,
@@ -93,7 +93,7 @@ async def main() -> None:
# authentication option.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="UtilityAgent",
instructions="You are a helpful assistant that can provide weather information and current time.",
tools=[get_weather, get_time],
@@ -22,7 +22,7 @@ async def suspend_resume_service_managed_thread() -> None:
# AzureAIAgentClient supports service-managed threads.
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential).create_agent(
AzureAIAgentClient(credential=credential).create_agent(
name="MemoryBot", instructions="You are a helpful assistant that remembers our conversation."
) as agent,
):
@@ -92,7 +92,7 @@ def create_coding_agent(client: AzureAIAgentClient) -> ChatAgent:
async def main():
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(async_credential=credential) as chat_client,
AzureAIAgentClient(credential=credential) as chat_client,
):
# Build a workflow: Agent generates code -> Evaluator assesses results
# The agent will be wrapped in a special agent executor which produces AgentExecutorResponse
@@ -30,10 +30,13 @@ async def run_agent_framework() -> None:
from agent_framework.azure import AzureAIAgentClient
from azure.identity.aio import AzureCliCredential
async with AzureCliCredential() as credential, AzureAIAgentClient(async_credential=credential).create_agent(
name="Support",
instructions="Answer customer questions in one paragraph.",
) as agent:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(credential=credential).create_agent(
name="Support",
instructions="Answer customer questions in one paragraph.",
) as agent,
):
# AF client returns an asynchronous context manager for remote agents.
reply = await agent.run("How do I upgrade my plan?")
print("[AF]", reply.text)
@@ -33,11 +33,14 @@ async def run_agent_framework() -> None:
from agent_framework.azure import AzureAIAgentClient, HostedCodeInterpreterTool
from azure.identity.aio import AzureCliCredential
async with AzureCliCredential() as credential, AzureAIAgentClient(async_credential=credential).create_agent(
name="Analyst",
instructions="Use the code interpreter for numeric work.",
tools=[HostedCodeInterpreterTool()],
) as agent:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(credential=credential).create_agent(
name="Analyst",
instructions="Use the code interpreter for numeric work.",
tools=[HostedCodeInterpreterTool()],
) as agent,
):
# HostedCodeInterpreterTool mirrors the built-in Azure AI capability.
reply = await agent.run(
"Use Python to compute 42 ** 2 and explain the result.",
@@ -36,10 +36,13 @@ async def run_agent_framework() -> None:
from agent_framework.azure import AzureAIAgentClient
from azure.identity.aio import AzureCliCredential
async with AzureCliCredential() as credential, AzureAIAgentClient(async_credential=credential).create_agent(
name="Planner",
instructions="Track follow-up questions within the same thread.",
) as agent:
async with (
AzureCliCredential() as credential,
AzureAIAgentClient(credential=credential).create_agent(
name="Planner",
instructions="Track follow-up questions within the same thread.",
) as agent,
):
thread = agent.get_new_thread()
# AF threads are explicit and can be serialized for external storage.
first = await agent.run("Outline the onboarding checklist.", thread=thread)