Azure Agentic AI- unable to register and run custom function call?

Umesh Narayanan AV 0 Reputation points
2025-05-17T14:50:24.7666667+00:00

Hello,

i am trying to build a agenti ai using custom call as per the azure document.

i followed the exact code in my code base, however when running the agent, i am getting below error.

https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/function-calling?tabs=python&pivots=code-example



```
import json
import datetime
from typing import Any, Callable, Set, Dict, List, Optional

def fetch_weather(location: str) -> str:
    """
    Fetches the weather information for the specified location.

    :param location (str): The location to fetch weather for.
    :return: Weather information as a JSON string.
    :rtype: str
    """
    # In a real-world scenario, you'd integrate with a weather API.
    # Here, we'll mock the response.
    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 for this location.")
    weather_json = json.dumps({"weather": weather})
    return weather_json

# Statically defined user functions for fast reference
user_functions: Set[Callable[..., Any]] = {
    fetch_weather,
}

functions = FunctionTool(user_functions)
## Create a function tool to bind the function
toolset = ToolSet()
# 
toolset.add(functions)

ERROR:root:Error executing function 'fetch_weather': Function 'fetch_weather' not found.
WARNING:root:Tool outputs contain errors - retrying
ERROR:root:Error executing function 'fetch_weather': Function 'fetch_weather' not found.
```

Azure AI services
Azure AI services
A group of Azure services, SDKs, and APIs designed to make apps more intelligent, engaging, and discoverable.
3,602 questions
{count} votes

2 answers

Sort by: Most helpful
  1. Jerald Felix 1,630 Reputation points
    2025-05-17T16:30:00.45+00:00

    Hello Umesh,

    You're close, but the issue stems from how the function is registered and used in Azure Agentic AI's FunctionTool

    ERROR:root:Error executing function 'fetch_weather': Function 'fetch_weather' not found.

    This error means the function name is not being correctly matched or not registered in the internal function registry that FunctionTool uses.

    You must ensure the function is wrapped correctly and registered with its name string so the agent can look it up.

    Import the required Azure classes

    Make sure you're importing the right classes from azure.ai.services.agents.tools.

    from azure.ai.services.agents.tools import FunctionTool, ToolSet

    Use a FunctionTool with proper function name mapping

    Update your code like this:
    import json

    from azure.ai.services.agents.tools import FunctionTool, ToolSet

    Step 1: Define the function

    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 for this location.")
    
    return json.dumps({"weather": weather})
    

    Step 2: Create a FunctionTool instance

    weather_tool = FunctionTool(fetch_weather)

    Step 3: Create a ToolSet and register the tool

    toolset = ToolSet()

    toolset. Add(weather_tool)

    This ensures:

    The function fetch_weather is registered by its actual name.

    • FunctionTool wraps the function correctly for the Azure Agent runtime.

    Problem in Your Current Code

    user_functions: Set[Callable[..., Any]] = {

    fetch_weather,
    

    }

    functions = FunctionTool(user_functions)

    This incorrectly passes a set of functions instead of individual ones. FunctionTool() expects one function at a time, not a set.

    Make sure the tool is referenced with the exact name the function defines (fetch_weather in this case) — Agentic AI resolves the name via introspection.

    Best Regards,

    Jerald Felix


  2. Sina Salam 22,031 Reputation points Volunteer Moderator
    2025-05-17T19:49:35.11+00:00

    Hello Umesh Narayanan AV,

    Welcome to the Microsoft Q&A and thank you for posting your questions here.

    I understand that your Azure Agentic AI- unable to register and run custom function call.

    For permanent solution:

    1. Add a JSON schema to ensure compatibility (required for some SDK versions):
         from azure.ai.services.agents.tools import FunctionTool, ToolSet
         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})
          Define the function schema explicitly (required for some SDK versions)
         fetch_weather_schema = {
             "name": "fetch_weather",
             "description": "Fetches weather data for a location.",
             "parameters": {
                 "type": "object",
                 "properties": {
                     "location": {"type": "string", "description": "The city name."}
                 },
                 "required": ["location"]
             }
         }
      
      You can use official Microsoft Docs - https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/tools/function-callingdefining-function-schemas
    2. Use FunctionTool with the schema and add it to the ToolSet:
         weather_tool = FunctionTool(
             func=fetch_weather,
             name="fetch_weather",   Explicitly set the name (avoids introspection issues)
             schema=fetch_weather_schema   Required for SDKs with strict validation
         )
         toolset = ToolSet()
         toolset.add(weather_tool) 
      
      https://learn.microsoft.com/en-us/python/api/azure-ai-agents/azure.ai.agents.tools.functiontool
    3. To initialize the Agent with the ToolSet, make sure the agent uses the registered tools:
         from azure.ai.services.agents import AzureAgent
         agent = AzureAgent(
             tools=toolset,   Pass the ToolSet here
              Include other required parameters (e.g., API key, endpoint)
         ) 
      
      Read more on Azure Agent Initialization - https://learn.microsoft.com/en-us/azure/ai-services/agents/how-to/create-agent
    4. Check compatibility with the latest SDK:
         pip install --upgrade azure-ai-agents
      
      https://learn.microsoft.com/en-us/azure/ai-services/agents/get-started
    5. If the error persists:
      • Ensure name="fetch_weather" matches the function’s actual name.
      • Print registered tools with print(toolset.list_tools()).
      • Look for initialization errors or schema mismatches.

    I hope this is helpful! Do not hesitate to let me know if you have any other questions or clarifications.


    Please don't forget to close up the thread here by upvoting and accept it as an answer if it is helpful.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.