Skip to content

Commit 12ecdd0

Browse files
authored
use as* asyncio.gather is used to make function tool calls in paral… (#41311)
* use as* `asyncio.gather` is used to make function tool calls in parallel for `async` scenario. * Fixed version * update
1 parent be83f29 commit 12ecdd0

File tree

5 files changed

+33
-53
lines changed

5 files changed

+33
-53
lines changed

sdk/ai/azure-ai-agents/CHANGELOG.md

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
# Release History
22

3-
## 1.1.0b1 (2025-05-20)
3+
## 1.0.1 (2025-05-20)
44

55
### Bugs Fixed
66

7-
* Adding instrumentation for create_thread_and_run
7+
* `asyncio.gather` is used to make function tool calls in parallel for `async` scenario.
8+
9+
* Adding instrumentation for create_thread_and_run.
810

911
## 1.0.0 (2025-05-15)
1012

sdk/ai/azure-ai-agents/azure/ai/agents/_version.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@
66
# Changes may cause incorrect behavior and will be lost if the code is regenerated.
77
# --------------------------------------------------------------------------
88

9-
VERSION = "1.1.0b1"
9+
VERSION = "1.0.1"

sdk/ai/azure-ai-agents/azure/ai/agents/models/_patch.py

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1219,29 +1219,27 @@ def validate_tool_type(self, tool: Tool) -> None:
12191219
+ "Please use AsyncFunctionTool instead and provide sync and/or async function(s)."
12201220
)
12211221

1222+
async def _execute_single_tool_call(self, tool_call: Any):
1223+
try:
1224+
tool = self.get_tool(AsyncFunctionTool)
1225+
output = await tool.execute(tool_call)
1226+
return {"tool_call_id": tool_call.id, "output": str(output)}
1227+
except Exception as e: # pylint: disable=broad-exception-caught
1228+
return {"tool_call_id": tool_call.id, "output": str(e)}
1229+
12221230
async def execute_tool_calls(self, tool_calls: List[Any]) -> Any:
12231231
"""
1224-
Execute a tool of the specified type with the provided tool calls.
1232+
Execute a tool of the specified type with the provided tool calls concurrently.
12251233
12261234
:param List[Any] tool_calls: A list of tool calls to execute.
12271235
:return: The output of the tool operations.
12281236
:rtype: Any
12291237
"""
1230-
tool_outputs = []
12311238

1232-
for tool_call in tool_calls:
1233-
try:
1234-
if tool_call.type == "function":
1235-
tool = self.get_tool(AsyncFunctionTool)
1236-
output = await tool.execute(tool_call)
1237-
tool_output = {
1238-
"tool_call_id": tool_call.id,
1239-
"output": str(output),
1240-
}
1241-
tool_outputs.append(tool_output)
1242-
except Exception as e: # pylint: disable=broad-exception-caught
1243-
tool_output = {"tool_call_id": tool_call.id, "output": str(e)}
1244-
tool_outputs.append(tool_output)
1239+
# Execute all tool calls concurrently
1240+
tool_outputs = await asyncio.gather(
1241+
*[self._execute_single_tool_call(tc) for tc in tool_calls if tc.type == "function"]
1242+
)
12451243

12461244
return tool_outputs
12471245

sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_functions_async.py

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@
1818
pip install azure-ai-agents azure-identity aiohttp
1919
2020
Set these environment variables with your own values:
21-
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
21+
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
2222
page of your Azure AI Foundry portal.
23-
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
23+
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model, as found under the "Name" column in
2424
the "Models + endpoints" tab in your Azure AI Foundry project.
2525
"""
2626
import asyncio
@@ -29,6 +29,7 @@
2929
from azure.ai.agents.aio import AgentsClient
3030
from azure.ai.agents.models import (
3131
AsyncFunctionTool,
32+
AsyncToolSet,
3233
RequiredFunctionToolCall,
3334
SubmitToolOutputsAction,
3435
ToolOutput,
@@ -44,6 +45,8 @@ async def main() -> None:
4445
async with AgentsClient(endpoint=os.environ["PROJECT_ENDPOINT"], credential=creds) as agents_client:
4546
# Initialize agent functions
4647
functions = AsyncFunctionTool(functions=user_async_functions)
48+
toolset = AsyncToolSet()
49+
toolset.add(functions)
4750

4851
# Create agent
4952
agent = await agents_client.create_agent(
@@ -80,19 +83,7 @@ async def main() -> None:
8083
await agents_client.runs.cancel(thread_id=thread.id, run_id=run.id)
8184
break
8285

83-
tool_outputs = []
84-
for tool_call in tool_calls:
85-
if isinstance(tool_call, RequiredFunctionToolCall):
86-
try:
87-
output = await functions.execute(tool_call)
88-
tool_outputs.append(
89-
ToolOutput(
90-
tool_call_id=tool_call.id,
91-
output=output,
92-
)
93-
)
94-
except Exception as e:
95-
print(f"Error executing tool_call {tool_call.id}: {e}")
86+
tool_outputs = await toolset.execute_tool_calls(tool_calls)
9687

9788
print(f"Tool outputs: {tool_outputs}")
9889
if tool_outputs:

sdk/ai/azure-ai-agents/samples/agents_async/sample_agents_stream_eventhandler_with_functions_async.py

Lines changed: 9 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717
pip install azure-ai-agents azure-identity aiohttp
1818
1919
Set these environment variables with your own values:
20-
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
20+
1) PROJECT_ENDPOINT - The Azure AI Project endpoint, as found in the Overview
2121
page of your Azure AI Foundry portal.
2222
2) MODEL_DEPLOYMENT_NAME - The deployment name of the AI model.
2323
"""
@@ -29,6 +29,7 @@
2929
from azure.ai.agents.models import (
3030
AsyncAgentEventHandler,
3131
AsyncFunctionTool,
32+
AsyncToolSet,
3233
ListSortOrder,
3334
MessageTextContent,
3435
MessageDeltaChunk,
@@ -42,6 +43,11 @@
4243
from azure.identity.aio import DefaultAzureCredential
4344
from utils.user_async_functions import user_async_functions
4445

46+
# Initialize function tool with user functions
47+
functions = AsyncFunctionTool(functions=user_async_functions)
48+
toolset = AsyncToolSet()
49+
toolset.add(functions)
50+
4551

4652
class MyEventHandler(AsyncAgentEventHandler[str]):
4753

@@ -65,21 +71,8 @@ async def on_thread_run(self, run: "ThreadRun") -> None:
6571
if run.status == "requires_action" and isinstance(run.required_action, SubmitToolOutputsAction):
6672
tool_calls = run.required_action.submit_tool_outputs.tool_calls
6773

68-
tool_outputs = []
69-
for tool_call in tool_calls:
70-
if isinstance(tool_call, RequiredFunctionToolCall):
71-
try:
72-
output = await self.functions.execute(tool_call)
73-
tool_outputs.append(
74-
ToolOutput(
75-
tool_call_id=tool_call.id,
76-
output=output,
77-
)
78-
)
79-
except Exception as e:
80-
print(f"Error executing tool_call {tool_call.id}: {e}")
81-
82-
print(f"Tool outputs: {tool_outputs}")
74+
tool_outputs = await toolset.execute_tool_calls(tool_calls)
75+
8376
if tool_outputs:
8477
await self.agents_client.runs.submit_tool_outputs_stream(
8578
thread_id=run.thread_id, run_id=run.id, tool_outputs=tool_outputs, event_handler=self
@@ -105,16 +98,12 @@ async def main() -> None:
10598
credential=creds,
10699
) as agents_client:
107100

108-
# [START create_agent_with_function_tool]
109-
functions = AsyncFunctionTool(functions=user_async_functions)
110-
111101
agent = await agents_client.create_agent(
112102
model=os.environ["MODEL_DEPLOYMENT_NAME"],
113103
name="my-agent",
114104
instructions="You are a helpful agent",
115105
tools=functions.definitions,
116106
)
117-
# [END create_agent_with_function_tool]
118107
print(f"Created agent, ID: {agent.id}")
119108

120109
thread = await agents_client.threads.create()

0 commit comments

Comments
 (0)