Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
# Copyright (c) Microsoft. All rights reserved.

import asyncio
import os
from contextlib import asynccontextmanager

from agent_framework import Agent, WorkflowBuilder
from agent_framework.foundry import FoundryChatClient
from azure.ai.agentserver.agentframework import from_agent_framework
from azure.identity.aio import AzureCliCredential, ManagedIdentityCredential
Comment on lines +7 to +10
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

agent_framework.foundry/FoundryChatClient doesn't exist in this repo's Python packages (there is no agent_framework/foundry module), so this sample will fail at import time. Please switch to a supported client (e.g., agent_framework.azure.AzureOpenAIResponsesClient using project_endpoint=... for Foundry, or another existing chat client) and update the env var names accordingly.

Copilot uses AI. Check for mistakes.
from dotenv import load_dotenv

load_dotenv(override=True)

# Configure these for your Foundry project
# Read the explicit variables present in the .env file
FOUNDRY_PROJECT_ENDPOINT = os.getenv(
"FOUNDRY_PROJECT_ENDPOINT"
) # e.g., "https://<project>.services.ai.azure.com/api/projects/<project-name>"
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FOUNDRY_PROJECT_ENDPOINT is required, but os.getenv(...) allows it to be None and will pass None into the client constructor, leading to a harder-to-diagnose failure later. Prefer os.environ["..."] or validate and raise a clear error when it's missing.

Suggested change
) # e.g., "https://<project>.services.ai.azure.com/api/projects/<project-name>"
) # e.g., "https://<project>.services.ai.azure.com/api/projects/<project-name>"
if not FOUNDRY_PROJECT_ENDPOINT:
raise RuntimeError(
"FOUNDRY_PROJECT_ENDPOINT environment variable is required. "
'Set it to your Foundry project endpoint, for example: '
'"https://<project>.services.ai.azure.com/api/projects/<project-name>".'
)

Copilot uses AI. Check for mistakes.
FOUNDRY_MODEL = os.getenv("FOUNDRY_MODEL", "gpt-4.1-mini") # Your model deployment name e.g., "gpt-4.1-mini"


def get_credential():
"""Will use Managed Identity when running in Azure, otherwise falls back to Azure CLI Credential."""
return ManagedIdentityCredential() if os.getenv("MSI_ENDPOINT") else AzureCliCredential()


@asynccontextmanager
async def create_agents():
async with get_credential() as credential:
client = FoundryChatClient(
project_endpoint=FOUNDRY_PROJECT_ENDPOINT,
model=FOUNDRY_MODEL,
credential=credential,
)
writer = Agent(
client=client,
name="Writer",
instructions="You are an excellent content writer. You create new content and edit contents based on the feedback.",
)
reviewer = Agent(
client=client,
name="Reviewer",
instructions="You are an excellent content reviewer. Provide actionable feedback to the writer about the provided content in the most concise manner possible.",
)
yield writer, reviewer


def create_workflow(writer, reviewer):
workflow = WorkflowBuilder(start_executor=writer).add_edge(writer, reviewer).build()
return Agent(
client=workflow,
)
Comment on lines +51 to +53
Copy link

Copilot AI Apr 10, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

create_workflow() wraps a Workflow inside Agent(client=workflow), but Workflow is not a chat client and doesn't implement the get_response API expected by Agent. Use workflow.as_agent(...) (returns WorkflowAgent) to expose a workflow as an agent for from_agent_framework(...).

Suggested change
return Agent(
client=workflow,
)
return workflow.as_agent(name="WriterReviewerWorkflow")

Copilot uses AI. Check for mistakes.


async def main() -> None:
"""
The writer and reviewer multi-agent workflow.

Environment variables required:
- FOUNDRY_PROJECT_ENDPOINT: Your Microsoft Foundry project endpoint
- FOUNDRY_MODEL: Your Microsoft Foundry model deployment name
"""

async with create_agents() as (writer, reviewer):
# Use a factory lambda so each incoming request gets a fresh Workflow
# instance, avoiding RuntimeError from concurrent executions (#4766).
await from_agent_framework(lambda: create_workflow(writer, reviewer)).run_async()


if __name__ == "__main__":
asyncio.run(main())
Loading