Skip to content

Commit e563849

Browse files
Align Python hosting get-started sample with Azure Functions (microsoft#3922)
1 parent 9506fb2 commit e563849

File tree

2 files changed

+24
-42
lines changed

2 files changed

+24
-42
lines changed
Lines changed: 23 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,42 @@
11
# Copyright (c) Microsoft. All rights reserved.
22

3-
import asyncio
4-
import os
3+
"""Host your agent with Azure Functions.
54
6-
from agent_framework.azure import AzureOpenAIResponsesClient
7-
from azure.identity import AzureCliCredential
8-
9-
"""
10-
Host Your Agent — Minimal A2A hosting stub
11-
12-
This sample shows the pattern for exposing an agent via the Agent-to-Agent
13-
(A2A) protocol. It creates the agent and demonstrates how to wrap it with
14-
the A2A hosting layer.
5+
This sample shows the Python hosting pattern used in docs:
6+
- Create an agent with `AzureOpenAIChatClient`
7+
- Register it with `AgentFunctionApp`
8+
- Run with Azure Functions Core Tools (`func start`)
159
1610
Prerequisites:
17-
pip install agent-framework[a2a] --pre
11+
pip install agent-framework-azurefunctions --pre
1812
1913
Environment variables:
20-
AZURE_AI_PROJECT_ENDPOINT — Your Azure AI Foundry project endpoint
21-
AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME — Model deployment name (e.g. gpt-4o)
22-
23-
To run a full A2A server, see samples/04-hosting/a2a/ for a complete example.
14+
AZURE_OPENAI_ENDPOINT
15+
AZURE_OPENAI_CHAT_DEPLOYMENT_NAME
2416
"""
2517

18+
from typing import Any
19+
20+
from agent_framework.azure import AgentFunctionApp, AzureOpenAIChatClient
21+
from azure.identity import AzureCliCredential
2622

27-
async def main() -> None:
28-
# <create_agent>
29-
credential = AzureCliCredential()
30-
client = AzureOpenAIResponsesClient(
31-
project_endpoint=os.environ["AZURE_AI_PROJECT_ENDPOINT"],
32-
deployment_name=os.environ["AZURE_OPENAI_RESPONSES_DEPLOYMENT_NAME"],
33-
credential=credential,
34-
)
3523

36-
agent = client.as_agent(
24+
# <create_agent>
25+
def _create_agent() -> Any:
26+
"""Create a hosted agent backed by Azure OpenAI."""
27+
return AzureOpenAIChatClient(credential=AzureCliCredential()).as_agent(
3728
name="HostedAgent",
38-
instructions="You are a helpful assistant exposed via A2A.",
29+
instructions="You are a helpful assistant hosted in Azure Functions.",
3930
)
40-
# </create_agent>
4131

42-
# <host_agent>
43-
# The A2A hosting integration wraps your agent behind an HTTP endpoint.
44-
# Import is gated so this sample can run without the a2a extra installed.
45-
try:
46-
from agent_framework.a2a import A2AAgent # noqa: F401
4732

48-
print("A2A support is available.")
49-
print("See samples/04-hosting/a2a/ for a runnable A2A server example.")
50-
except ImportError:
51-
print("Install a2a extras: pip install agent-framework[a2a] --pre")
33+
# </create_agent>
5234

53-
# Quick smoke-test: run the agent locally to verify it works
54-
result = await agent.run("Hello! What can you do?")
55-
print(f"Agent: {result}")
56-
# </host_agent>
35+
# <host_agent>
36+
app = AgentFunctionApp(agents=[_create_agent()], enable_health_check=True, max_poll_retries=50)
37+
# </host_agent>
5738

5839

5940
if __name__ == "__main__":
60-
asyncio.run(main())
41+
print("Start the Functions host with: func start")
42+
print("Then call: POST /api/agents/HostedAgent/run")

python/samples/01-get-started/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export OPENAI_RESPONSES_MODEL_ID="gpt-4o" # optional, defaults to gpt-4o
2525
| 3 | [03_multi_turn.py](03_multi_turn.py) | Keep conversation history across turns with `AgentThread`. |
2626
| 4 | [04_memory.py](04_memory.py) | Add dynamic context with a custom `ContextProvider`. |
2727
| 5 | [05_first_workflow.py](05_first_workflow.py) | Chain executors into a workflow with edges. |
28-
| 6 | [06_host_your_agent.py](06_host_your_agent.py) | Prepare your agent for A2A hosting. |
28+
| 6 | [06_host_your_agent.py](06_host_your_agent.py) | Host a single agent with Azure Functions. |
2929

3030
Run any sample with:
3131

0 commit comments

Comments
 (0)