
在 Azure Agentic AI 中,即使正确注册了自定义函数工具,调用仍会报“Function not found”错误——根本原因是未启用自动函数调用机制;需显式调用 enable_auto_function_calls() 并传入工具定义。
在 azure agentic ai 中,即使正确注册了自定义函数工具,调用仍会报“function not found”错误——根本原因是未启用自动函数调用机制;需显式调用 `enable_auto_function_calls()` 并传入工具定义。
在 Azure AI Agents 服务中,函数工具(FunctionTool)的注册与实际执行是两个独立阶段。仅将函数封装为 FunctionTool 并添加至 ToolSet,并不等于该函数已对 Agent 启用调用能力。Agent 默认不会主动解析和触发函数调用,必须通过 enable_auto_function_calls() 显式开启自动工具调用支持,并确保传入的是与 Agent 创建时一致的工具定义(即 functions.definitions)。
以下是完整、可运行的修复后代码示例(基于 Python SDK):
import json
import os
from typing import Any, Callable, Set
from azure.ai.agents import AgentClient
from azure.ai.agents.tools import FunctionTool
def fetch_weather(location: str) -> str:
mock_weather_data = {"New York": "Sunny, 25°C", "London": "Cloudy, 18°C", "Tokyo": "Rainy, 22°C"}
weather = mock_weather_data.get(location, "Weather data not available.")
return json.dumps({"weather": weather})
# ✅ 正确:定义函数集合并创建 FunctionTool
user_functions: Set[Callable[..., Any]] = {fetch_weather}
functions = FunctionTool(user_functions) # 自动生成 OpenAI 兼容的 function definition
# ✅ 关键修复:启用自动函数调用(必须在 agent 创建后、使用前调用)
with AgentClient.from_connection_string(os.environ["AZURE_AI_PROJECT_CONNECTION_STRING"]) as project_client:
agent = project_client.agents.create_agent(
model=os.environ["MODEL_DEPLOYMENT_NAME"],
name="weather-assistant",
instructions="You are a helpful weather assistant. Use fetch_weather() when location is mentioned.",
tools=functions.definitions # 注意:此处传入 definitions,非 functions 对象本身
)
# ? 必须调用此方法,否则函数无法被识别和执行
project_client.agents.enable_auto_function_calls(tools=functions)
print(f"✅ Created agent, ID: {agent.id}")⚠️ 关键注意事项:
-
enable_auto_function_calls()必须在create_agent()之后、首次调用create_thread()或invoke()之前执行; -
tools参数必须传入FunctionTool实例(如functions),而非其.definitions字典或原始函数集合; - 确保
functions.definitions与create_agent(..., tools=...)中传入的定义完全一致(推荐复用同一FunctionTool实例); - 函数签名中的类型注解(如
str)会影响 OpenAPI schema 生成,建议保持简洁明确,避免复杂嵌套类型; - 若使用多线程或多 Agent 场景,每个 Agent 实例均需单独调用
enable_auto_function_calls()。
完成上述配置后,当用户提问 “What’s the weather in London?”,Agent 将自动识别需调用 fetch_weather,正确解析参数并返回结构化结果,不再出现 Function 'fetch_weather' not found 错误。


















