Python: Fix samples (#4980)

* First samples 1st batch

* Fix sample paths

* Fix workflow samples

* Fix workflow dependency

* Correct env vars

* Increase idle timeout

* Fix workflows HIL sample

* Fix more workflow samples
This commit is contained in:
Tao Chen
2026-03-31 08:20:35 -07:00
committed by GitHub
Unverified
parent 0f81c277d9
commit 016daf3b98
69 changed files with 234 additions and 306 deletions
@@ -18,7 +18,7 @@ This sample shows how to create agents backed by Azure OpenAI Responses and use
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- AZURE_AI_MODEL_DEPLOYMENT_NAME must be set to your Azure OpenAI model deployment name.
- FOUNDRY_MODEL must be the deployment name of a model in your Foundry project.
- Authentication via azure-identity. Use AzureCliCredential and run az login before executing the sample.
- Basic familiarity with WorkflowBuilder, edges, events, and streaming runs.
"""
@@ -27,7 +27,7 @@ Prerequisites:
async def main() -> None:
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -39,7 +39,7 @@ Demonstrate:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- AZURE_AI_MODEL_DEPLOYMENT_NAME must be set to your Azure OpenAI model deployment name.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Authentication via azure-identity. Use AzureCliCredential and run az login before executing the sample.
- Basic familiarity with agents, workflows, and executors in the agent framework.
"""
@@ -60,7 +60,7 @@ async def intercept_agent_response(
async def main() -> None:
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -37,7 +37,7 @@ Demonstrates:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Azure OpenAI configured for FoundryChatClient with required environment variables.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Authentication via azure-identity. Run `az login` before executing.
"""
@@ -104,7 +104,7 @@ async def main() -> None:
research_agent = Agent(
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
name="research_agent",
@@ -116,7 +116,7 @@ async def main() -> None:
final_editor_agent = Agent(
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
name="final_editor_agent",
@@ -18,7 +18,7 @@ This sample shows how to create AzureOpenAI Chat Agents and use them in a workfl
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Azure OpenAI configured for FoundryChatClient with required environment variables.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Authentication via azure-identity. Use AzureCliCredential and run az login before executing the sample.
- Basic familiarity with WorkflowBuilder, edges, events, and streaming runs.
"""
@@ -29,7 +29,7 @@ async def main():
# Create the agents
_writer_client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
writer_agent = Agent(
@@ -42,7 +42,7 @@ async def main():
_reviewer_client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
reviewer_agent = Agent(
@@ -49,7 +49,7 @@ Demonstrates:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Azure OpenAI configured for FoundryChatClient with required environment variables.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Authentication via azure-identity. Run `az login` before executing.
"""
@@ -122,11 +122,7 @@ class Coordinator(Executor):
# Writer agent response; request human feedback.
# Preserve the full conversation so the final editor
# can see tool traces and the initial prompt.
conversation: list[Message]
if draft.full_conversation is not None:
conversation = list(draft.full_conversation)
else:
conversation = list(draft.agent_response.messages)
conversation = list(draft.full_conversation)
draft_text = draft.agent_response.text.strip()
if not draft_text:
draft_text = "No draft text was produced."
@@ -178,7 +174,7 @@ def create_writer_agent() -> Agent:
return Agent(
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
name="writer_agent",
@@ -188,7 +184,9 @@ def create_writer_agent() -> Agent:
"produce a 3-sentence draft."
),
tools=[fetch_product_brief, get_brand_voice_profile],
tool_choice="required",
default_options={
"tool_choice": "required",
},
)
@@ -197,7 +195,7 @@ def create_final_editor_agent() -> Agent:
return Agent(
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
name="final_editor_agent",
@@ -25,7 +25,7 @@ Demonstrates:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Azure OpenAI access configured for FoundryChatClient (use az login + env vars)
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Familiarity with Workflow events (WorkflowEvent with type "output")
"""
@@ -34,7 +34,7 @@ async def main() -> None:
# 1) Create three domain agents using FoundryChatClient
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -69,7 +69,7 @@ async def main() -> None:
workflow = ConcurrentBuilder(participants=[researcher, marketer, legal]).build()
# 3) Expose the concurrent workflow as an agent for easy reuse
agent = Agent(client=workflow, name="ConcurrentWorkflowAgent")
agent = workflow.as_agent()
prompt = "We are launching a new budget-friendly electric bike for urban commuters."
agent_response = await agent.run(prompt)
@@ -33,7 +33,7 @@ Note: When an agent is passed to a workflow, the workflow wraps the agent in a m
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Azure OpenAI configured for FoundryChatClient with required environment variables.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Authentication via azure-identity. Use AzureCliCredential and run az login before executing the sample.
- Basic familiarity with WorkflowBuilder, executors, edges, events, and streaming or non streaming runs.
"""
@@ -54,7 +54,7 @@ class Writer(Executor):
self.agent = Agent(
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
instructions=(
@@ -101,7 +101,7 @@ class Reviewer(Executor):
self.agent = Agent(
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
instructions=(
@@ -32,7 +32,7 @@ async def main() -> None:
instructions="Gather concise facts that help a teammate answer the question.",
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
@@ -43,14 +43,14 @@ async def main() -> None:
instructions="Compose clear and structured answers using any notes provided.",
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
_orch_client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -72,7 +72,7 @@ async def main() -> None:
print(f"Input: {task}\n")
try:
workflow_agent = Agent(client=workflow, name="GroupChatWorkflowAgent")
workflow_agent = workflow.as_agent()
agent_result = await workflow_agent.run(task)
if agent_result.messages:
@@ -31,7 +31,7 @@ them to transfer control to each other based on the conversation context.
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- `az login` (Azure CLI authentication)
- Environment variables configured for FoundryChatClient (AZURE_AI_MODEL_DEPLOYMENT_NAME)
- Environment variables configured for FoundryChatClient (FOUNDRY_MODEL)
Key Concepts:
- Auto-registered handoff tools: HandoffBuilder automatically creates handoff tools
@@ -159,7 +159,7 @@ async def main() -> None:
# Initialize the Azure OpenAI chat client
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -174,21 +174,20 @@ async def main() -> None:
# Without this, the default behavior continues requesting user input until max_turns
# is reached. Here we use a custom condition that checks if the conversation has ended
# naturally (when one of the agents says something like "you're welcome").
agent = Agent(
client=(
HandoffBuilder(
name="customer_support_handoff",
participants=[triage, refund, order, support],
# Custom termination: Check if one of the agents has provided a closing message.
# This looks for the last message containing "welcome", which indicates the
# conversation has concluded naturally.
termination_condition=lambda conversation: (
len(conversation) > 0 and "welcome" in conversation[-1].text.lower()
),
)
.with_start_agent(triage)
.build()
),
agent = (
HandoffBuilder(
name="customer_support_handoff",
participants=[triage, refund, order, support],
# Custom termination: Check if one of the agents has provided a closing message.
# This looks for the last message containing "welcome", which indicates the
# conversation has concluded naturally.
termination_condition=lambda conversation: (
len(conversation) > 0 and "welcome" in conversation[-1].text.lower()
),
)
.with_start_agent(triage)
.build()
.as_agent()
)
# Scripted user responses for reproducible demo
@@ -226,7 +225,7 @@ async def main() -> None:
responses = {req_id: HandoffAgentUserRequest.create_response(user_response) for req_id in pending_requests}
function_results = [
Content.from_function_result(call_id=req_id, result=response) for req_id, response in responses.items()
Content("function_result", call_id=req_id, result=response) for req_id, response in responses.items()
]
response = await agent.run(Message("tool", function_results))
pending_requests = handle_response_and_requests(response)
@@ -23,7 +23,7 @@ like any other agent while still emitting callback telemetry.
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- OpenAI credentials configured for `FoundryChatClient` and `FoundryChatClient`.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
"""
@@ -37,7 +37,7 @@ async def main() -> None:
# This agent requires the gpt-4o-search-preview model to perform web searches.
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
@@ -45,7 +45,7 @@ async def main() -> None:
# Create code interpreter tool using instance method
coder_client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
code_interpreter_tool = coder_client.get_code_interpreter_tool()
@@ -65,7 +65,7 @@ async def main() -> None:
instructions="You coordinate a team to complete complex tasks efficiently.",
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
@@ -98,7 +98,7 @@ async def main() -> None:
try:
# Wrap the workflow as an agent for composition scenarios
print("\nWrapping workflow as an agent and running...")
workflow_agent = Agent(client=workflow, name="MagenticWorkflowAgent")
workflow_agent = workflow.as_agent()
last_response_id: str | None = None
async for update in workflow_agent.run(task, stream=True):
@@ -27,7 +27,7 @@ Note on internal adapters:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Azure OpenAI access configured for FoundryChatClient (use az login + env vars)
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
"""
@@ -35,7 +35,7 @@ async def main() -> None:
# 1) Create agents
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -55,7 +55,7 @@ async def main() -> None:
workflow = SequentialBuilder(participants=[writer, reviewer]).build()
# 3) Treat the workflow itself as an agent for follow-up invocations
agent = Agent(client=workflow, name="SequentialWorkflowAgent")
agent = workflow.as_agent()
prompt = "Write a tagline for a budget-friendly eBike."
agent_response = await agent.run(prompt)
@@ -8,7 +8,6 @@ from dataclasses import dataclass
from pathlib import Path
from typing import Any
from agent_framework import Agent
from agent_framework.foundry import FoundryChatClient
from azure.identity import AzureCliCredential
from dotenv import load_dotenv
@@ -50,7 +49,7 @@ to the Worker. The workflow completes when idle.
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- OpenAI account configured and accessible for FoundryChatClient.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Familiarity with WorkflowBuilder, Executor, and WorkflowContext from agent_framework.
- Understanding of request-response message handling in executors.
- (Optional) Review of reflection and escalation patterns, such as those in
@@ -113,14 +112,14 @@ async def main() -> None:
id="worker",
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
reviewer = ReviewerWithHumanInTheLoop(worker_id="worker")
agent = Agent(
client=(WorkflowBuilder(start_executor=worker).add_edge(worker, reviewer).add_edge(reviewer, worker).build()),
agent = (
WorkflowBuilder(start_executor=worker).add_edge(worker, reviewer).add_edge(reviewer, worker).build().as_agent()
)
print("Running workflow agent with user query...")
@@ -165,7 +164,8 @@ async def main() -> None:
human_response = ReviewResponse(request_id=request_id, feedback="", approved=True)
# Create the function call result object to send back to the agent.
human_review_function_result = Content.from_function_result(
human_review_function_result = Content(
"function_result",
call_id=human_review_function_call.call_id, # type: ignore
result=human_response,
)
@@ -35,7 +35,7 @@ When to use Agent(client=workflow,):
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Environment variables configured
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
"""
@@ -89,7 +89,7 @@ async def main() -> None:
# Create chat client
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -109,7 +109,7 @@ async def main() -> None:
workflow = SequentialBuilder(participants=[agent]).build()
# Expose the workflow as an agent Agent(client=using,)
workflow_agent = Agent(client=workflow, name="WorkflowAgent")
workflow_agent = workflow.as_agent()
# Define custom context that will flow to tools via kwargs
custom_data = {
@@ -6,7 +6,6 @@ from dataclasses import dataclass
from uuid import uuid4
from agent_framework import (
Agent,
AgentResponse,
Executor,
Message,
@@ -41,7 +40,7 @@ Key Concepts Demonstrated:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- OpenAI account configured and accessible for FoundryChatClient.
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
- Familiarity with WorkflowBuilder, Executor, WorkflowContext, and event handling.
- Understanding of how agent messages are generated, reviewed, and re-submitted.
"""
@@ -198,7 +197,7 @@ async def main() -> None:
id="worker",
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
@@ -206,13 +205,13 @@ async def main() -> None:
id="reviewer",
client=FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
),
)
agent = Agent(
client=(WorkflowBuilder(start_executor=worker).add_edge(worker, reviewer).add_edge(reviewer, worker).build()),
agent = (
WorkflowBuilder(start_executor=worker).add_edge(worker, reviewer).add_edge(reviewer, worker).build().as_agent()
)
print("Running workflow agent with user query...")
@@ -38,7 +38,7 @@ Use cases:
Prerequisites:
- FOUNDRY_PROJECT_ENDPOINT must be your Azure AI Foundry Agent Service (V2) project endpoint.
- Environment variables configured for FoundryChatClient
- FOUNDRY_MODEL must be set to your Azure OpenAI model deployment name.
"""
@@ -46,7 +46,7 @@ async def main() -> None:
# Create a chat client
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -72,7 +72,7 @@ async def main() -> None:
workflow = SequentialBuilder(participants=[assistant, summarizer]).build()
# Wrap the workflow as an agent
agent = Agent(client=workflow, name="ConversationalWorkflowAgent")
agent = workflow.as_agent()
# Create a session to maintain history
session = agent.create_session()
@@ -133,7 +133,7 @@ async def demonstrate_session_serialization() -> None:
"""
client = FoundryChatClient(
project_endpoint=os.environ["FOUNDRY_PROJECT_ENDPOINT"],
model=os.environ["AZURE_AI_MODEL_DEPLOYMENT_NAME"],
model=os.environ["FOUNDRY_MODEL"],
credential=AzureCliCredential(),
)
@@ -144,7 +144,7 @@ async def demonstrate_session_serialization() -> None:
)
workflow = SequentialBuilder(participants=[memory_assistant]).build()
agent = Agent(client=workflow, name="MemoryWorkflowAgent")
agent = workflow.as_agent()
# Create initial session and have a conversation
session = agent.create_session()